Path: redb/redb.Templates/templates/redb-bff/RedbBff.Backend/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-bff/RedbBff.Backend/ModuleSettings.cs
----
using System.Globalization;
using redb.Route.Abstractions;
namespace RedbBff.Backend;
///
/// The module's settings, read from the properties of its route context. Both hosts fill them the same
/// way: RedbBff.Backend.config.json first, then the Tsak:Contexts:{context}:Override section of the
/// host configuration, which wins. The service key belongs only in the Override layer (RedbBff.Host
/// appsettings.json, or Tsak__Contexts__redbbff__Override__Api__ServiceKey), never in the module's
/// config file.
///
public sealed record ModuleSettings(string ApiHost, int ApiPort, string ServiceKey)
{
public static ModuleSettings FromContext(IRouteContext context)
{
var api = context.GetProperty>("Api") ?? throw Missing(context, "Api");
var serviceKey = Required(context, api, "ServiceKey");
if (serviceKey.Length < 32)
throw new InvalidOperationException($"Api:ServiceKey of context '{context.ContextId}' must be at least 32 characters.");
return new(
// 127.0.0.1 by default: only the web server of the same machine calls the backend. A container
// sets 0.0.0.0 and keeps the port inside the compose network (deploy/*.yml).
ApiHost: Required(context, api, "Host"),
ApiPort: int.Parse(Required(context, api, "Port"), CultureInfo.InvariantCulture),
ServiceKey: serviceKey);
}
private static string Required(IRouteContext context, IDictionary section, string key) =>
section.TryGetValue(key, out var value) && Convert.ToString(value, CultureInfo.InvariantCulture) is { Length: > 0 } text
? text
: throw Missing(context, $"Api:{key}");
private static InvalidOperationException Missing(IRouteContext context, string key) => new(
$"{key} is not configured for context '{context.ContextId}': set it in RedbBff.Backend.config.json " +
$"or in Tsak:Contexts:{context.ContextId}:Override.");
}