using Anubis.Core;
using Anubis.Platform;
namespace Anubis.Server;
///
/// What the server process was asked to be, from its arguments and environment: Docker or desktop, which data
/// folder, demo mode. Tests pass a fake environment and clock through .
///
internal sealed record ServerOptions(PlatformKind Platform, string? DataDir, bool Demo, string[] AspNetArgs)
{
public static ServerOptions Parse(string[] args, IReadOnlyDictionary environment)
{
var rest = new List();
PlatformKind? platform = null;
string? dataDir = null;
var demo = false;
for (var i = 0; i < args.Length; i++)
{
switch (args[i])
{
case "--platform" when i + 1 < args.Length:
platform = args[++i].Equals("docker", StringComparison.OrdinalIgnoreCase) ? PlatformKind.Docker : PlatformKind.Desktop;
break;
case "--data-dir" when i + 1 < args.Length:
dataDir = Path.GetFullPath(args[++i]);
break;
case "--demo":
demo = true;
break;
default:
rest.Add(args[i]);
break;
}
}
platform ??= (environment.GetValueOrDefault("ANUBIS_PLATFORM") ?? "").ToLowerInvariant() switch
{
"docker" => PlatformKind.Docker,
"desktop" => PlatformKind.Desktop,
_ => environment.GetValueOrDefault("DOTNET_RUNNING_IN_CONTAINER") == "true" ? PlatformKind.Docker : PlatformKind.Desktop,
};
return new ServerOptions(platform.Value, dataDir, demo, [.. rest]);
}
/// Reads the startup configuration of the chosen platform.
public StartupConfig LoadConfig(IReadOnlyDictionary environment)
{
if (Platform == PlatformKind.Docker)
{
return DockerPlatform.Load(environment, DataDir ?? "/data");
}
var data = DataDir is null ? DataDirectory.ForCurrentUser() : new DataDirectory(DataDir);
return DesktopPlatform.Load(data, ProtectedSecretKey.DefaultStore(data));
}
}
/// Test hooks: a fake environment, a manual clock, a cheaper password hash.
internal sealed record ServerOverrides
{
public IReadOnlyDictionary? Environment { get; init; }
public IClock? Clock { get; init; }
public int? PasswordIterations { get; init; }
/// How often the database is checked while running and retried while it is missing.
public TimeSpan? DatabaseCheckInterval { get; init; }
}