Files
OpenNest/OpenNest.Server/ServerOptions.cs
T
ajandClaude Opus 5.5 5dff83cfb8 fix(server): reject duplicate nest ids, invalid metadata and bare --database
- POST /api/nests with an existing id (active or deleted) now returns 409 with
  the current record instead of a 500 from the UNIQUE constraint.
- Uploaded nest metadata must have string text fields and a defined status;
  null fields and undefined statuses (e.g. 99) are 400 instead of a 500 or a
  stored invalid value.
- --database <path> is accepted alongside --database=<path>; a missing or
  repeated path fails startup instead of silently using OPENNEST_DB/default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 01:30:37 -04:00

44 lines
1.6 KiB
C#

namespace OpenNest.Server;
/// <summary>Startup settings read from the command line and environment.</summary>
public static class ServerOptions
{
public const string DatabaseVariable = "OPENNEST_DB";
/// <summary>
/// The database path: <c>--database=&lt;path&gt;</c> or <c>--database &lt;path&gt;</c>, then
/// <c>OPENNEST_DB</c>, then <c>./data/nests.db</c>. A <c>--database</c> without a path,
/// or given twice, fails startup instead of silently storing nests elsewhere.
/// </summary>
public static string DatabasePath(IReadOnlyList<string> args, string? environmentPath)
{
string? path = null;
for (var i = 0; i < args.Count; i++)
{
string value;
if (args[i] == "--database")
{
if (i + 1 >= args.Count || args[i + 1].StartsWith("--", StringComparison.Ordinal))
throw new ArgumentException("--database requires a path.", nameof(args));
value = args[++i];
}
else if (args[i].StartsWith("--database=", StringComparison.Ordinal))
{
value = args[i]["--database=".Length..];
}
else
{
continue;
}
if (string.IsNullOrWhiteSpace(value))
throw new ArgumentException("--database requires a path.", nameof(args));
if (path is not null)
throw new ArgumentException("--database was given more than once.", nameof(args));
path = value;
}
return path ?? environmentPath ?? Path.Combine("data", "nests.db");
}
}