Path: redb/redb.Templates/templates/redb-app/RedbApp.Api/ModuleSettings.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Templates/templates/redb-app/RedbApp.Api/ModuleSettings.cs
----
using System.Globalization;
using redb.Route.Abstractions;
namespace RedbApp.Api;
///
/// The module's settings, read from the properties of its route context. Both hosts fill them the same
/// way: RedbApp.Api.config.json first, then the Tsak:Contexts:{context}:Override section of the host
/// configuration, which wins. The signing key and the users belong only in the Override layer
/// (RedbApp.Host appsettings.json, or environment variables such as
/// Tsak__Contexts__redbapp__Override__Auth__SigningKey), never in the module's config file.
///
public sealed record ModuleSettings(
string ApiHost,
int ApiPort,
string CorsOrigins,
string Issuer,
int TokenLifetimeMinutes,
string SigningKey,
IReadOnlyDictionary Users)
{
public static ModuleSettings FromContext(IRouteContext context)
{
var api = Section(context, "Api");
var auth = Section(context, "Auth");
var signingKey = Required(context, auth, "Auth", "SigningKey");
// HMAC-SHA256 needs a key of at least 256 bits.
if (System.Text.Encoding.UTF8.GetByteCount(signingKey) < 32)
throw new InvalidOperationException($"Auth:SigningKey of context '{context.ContextId}' must be at least 32 characters.");
var users = auth.TryGetValue("Users", out var raw) && raw is IDictionary section
? section.ToDictionary(
u => u.Key,
u => AppUser.From(context, u.Key, u.Value as IDictionary),
StringComparer.OrdinalIgnoreCase)
: throw Missing(context, "Auth:Users");
return new(
ApiHost: Required(context, api, "Api", "Host"),
ApiPort: int.Parse(Required(context, api, "Api", "Port"), CultureInfo.InvariantCulture),
// The origin of the Blazor client when it is served from another port or host. Behind one
// reverse proxy (deploy/nginx.conf) the browser sees one origin and CORS is not used.
CorsOrigins: Required(context, api, "Api", "CorsOrigins"),
Issuer: Required(context, auth, "Auth", "Issuer"),
TokenLifetimeMinutes: int.Parse(Required(context, auth, "Auth", "TokenLifetimeMinutes"), CultureInfo.InvariantCulture),
SigningKey: signingKey,
Users: users);
}
internal static IDictionary Section(IRouteContext context, string name) =>
context.GetProperty>(name) ?? throw Missing(context, name);
internal static string Required(IRouteContext context, IDictionary? section, string sectionName, string key) =>
section is not null && section.TryGetValue(key, out var value)
&& Convert.ToString(value, CultureInfo.InvariantCulture) is { Length: > 0 } text
? text
: throw Missing(context, $"{sectionName}:{key}");
internal static InvalidOperationException Missing(IRouteContext context, string key) => new(
$"{key} is not configured for context '{context.ContextId}': set it in RedbApp.Api.config.json " +
$"or in Tsak:Contexts:{context.ContextId}:Override.");
}
///
/// A user who can sign in. The users come from configuration: a stand-in that keeps the template small.
/// A real application keeps its users in a store of its own, for example redb.Identity.
///
public sealed record AppUser(string Login, string Password, string Role)
{
internal static AppUser From(IRouteContext context, string login, IDictionary? section) => new(
login,
ModuleSettings.Required(context, section, $"Auth:Users:{login}", "Password"),
ModuleSettings.Required(context, section, $"Auth:Users:{login}", "Role"));
}