Owner review 2, R6: the web panel manages the library without editing it, so the engine and the server must delete single photos safely, keep them out of sync afterwards, and let an administrator restore or finally erase the trash. - Migration 0008: frame_removals (tombstones by sequence and SHA-256, reason, replacement); the sidecar gains "removed" (omitted when empty) and frames a reserved "source"; the indexer writes the tombstones on every sync, so a rebuild keeps them, and drops a hero frame that is gone. - FrameTrash: dry run (busy, frames changed, offline, would empty), delete under the sequence lease with a write-ahead journal that the next writer undoes after a crash, files to trash/photos/<seq>-<time>/originals with a record, sidecar without the frames plus tombstones, index sync clearing analyses, thumbnails and proxies, proxies renamed to follow their frames, follow-up jobs. Restore re-hashes every file, keeps mismatches in the trash, puts the same frame records back and drops their tombstones, or files the photos into a new sequence through the ingest when theirs is gone. - Project positions on vanished photos are translated by photo and wait as pending frames, so they come back with a restore. - Trash entries have a kind (sequence, photos, replaced); emptying is a dry run with a fingerprint and a new EmptyTrash job (18) that erases exactly the planned entries, refuses a stale plan and erases replaced originals only while their replacements still hash right. - Sync honours tombstones: nothing one side deleted is offered back, local files are never deleted, replaced photos count as their replacement; the server answers an upload of a tombstoned SHA-256 as already there. - Server: sequence thumbnail, photo delete, bulk delete/retag/jobs, trash list/restore/empty endpoints with their error messages; a verify of sequences now targets their archive (POST /jobs failed before). - Desktop trash dialog lists photo entries and empties the trash after a typed confirmation through the same job. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 35bb56690a1d69eae57153d036e1409201cd73f7)
204 lines
11 KiB
C#
204 lines
11 KiB
C#
using Microsoft.AspNetCore.HttpOverrides;
|
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
using Microsoft.AspNetCore.Server.Kestrel.Core;
|
|
using Chronos.Core.Diagnostics;
|
|
using Chronos.Data;
|
|
using Chronos.Data.Archiving;
|
|
using Chronos.Data.Housekeeping;
|
|
using Chronos.Protocol;
|
|
using Chronos.Server.Api;
|
|
using Chronos.Server.Configuration;
|
|
using Chronos.Server.Events;
|
|
using Chronos.Server.Jobs;
|
|
using Chronos.Server.Security;
|
|
using Chronos.Server.Storage;
|
|
|
|
namespace Chronos.Server.Hosting;
|
|
|
|
/// <summary>
|
|
/// Builds the Chronos Server web application from <see cref="ServerOptions"/> (used by Program, the Windows service
|
|
/// host and tests). The archive module plugs in through the <c>configureServices</c> callback of <see cref="Build"/>,
|
|
/// which runs before the server's own registrations: it must register <see cref="IArchiveIngest"/> and
|
|
/// <see cref="IFrameAssetLocator"/> (the server refuses to start without them), may add
|
|
/// <see cref="Chronos.Data.Jobs.IJobHandler"/>s, and may resolve <see cref="ChronosDatabase"/>, <see cref="ServerArchive"/>
|
|
/// and <see cref="ServerOptions"/> from DI in its factories.
|
|
/// </summary>
|
|
public static class ServerHost
|
|
{
|
|
/// <summary>Request bodies other than upload chunks stay small (BRIEF §14.2 limits).</summary>
|
|
public const long JsonBodyLimit = 1024 * 1024;
|
|
|
|
public static WebApplication Build(ServerOptions options, string[] args, Action<IServiceCollection>? configureServices = null)
|
|
{
|
|
// Before anything is written there: the folder holds the database, the TLS key and the first admin password.
|
|
DataFolderAccess.Restrict(options.DataPath);
|
|
Directory.CreateDirectory(options.LogPath);
|
|
Directory.CreateDirectory(options.UploadsPath);
|
|
var archiveOpenToOthers = DataFolderAccess.PrepareArchive(options.ArchivePath);
|
|
|
|
var builder = WebApplication.CreateSlimBuilder(new WebApplicationOptions { Args = args, ContentRootPath = AppContext.BaseDirectory });
|
|
// The console and the files each follow their own level, which the admin panel changes while the server runs.
|
|
var logging = new ServerLogging(options);
|
|
logging.AddTo(builder.Logging);
|
|
builder.Services.AddSingleton(logging);
|
|
|
|
// The archive module registers first, so a missing piece stops the server before it touches the database.
|
|
configureServices?.Invoke(builder.Services);
|
|
RequireArchiveModule(builder.Services);
|
|
|
|
using var bootLoggerFactory = LoggerFactory.Create(l => l.AddSimpleConsole(c => c.SingleLine = true).SetMinimumLevel(options.ConsoleLogLevel));
|
|
var bootLogger = bootLoggerFactory.CreateLogger("Chronos.Server");
|
|
var certificate = CertificateStore.Load(options, bootLogger);
|
|
var database = ServerDatabase.OpenAsync(options, bootLogger, CancellationToken.None).GetAwaiter().GetResult();
|
|
try
|
|
{
|
|
var archive = ServerArchive.OpenAsync(database, options, CancellationToken.None).GetAwaiter().GetResult();
|
|
AdminBootstrap.RunAsync(database, options, TimeProvider.System, bootLogger, CancellationToken.None).GetAwaiter().GetResult();
|
|
var state = new ServerState
|
|
{
|
|
TlsFingerprint = certificate is null ? null : CertificateStore.Fingerprint(certificate),
|
|
Database = ServerDatabase.DescribeAsync(database, CancellationToken.None).GetAwaiter().GetResult(),
|
|
};
|
|
|
|
builder.WebHost.ConfigureKestrel(k =>
|
|
{
|
|
k.AddServerHeader = false;
|
|
k.Limits.MaxRequestBodySize = JsonBodyLimit;
|
|
k.Limits.RequestHeadersTimeout = TimeSpan.FromSeconds(30);
|
|
k.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(2);
|
|
k.ListenAnyIP(options.HttpPort);
|
|
if (certificate is not null) k.ListenAnyIP(options.HttpsPort, l =>
|
|
{
|
|
l.Protocols = HttpProtocols.Http1AndHttp2;
|
|
l.UseHttps(certificate);
|
|
});
|
|
});
|
|
ConfigureServices(builder.Services, options, state, database, archive);
|
|
|
|
var app = builder.Build();
|
|
Log.SetSink(new LoggerSink(app.Services.GetRequiredService<ILoggerFactory>()));
|
|
ConfigurePipeline(app, options);
|
|
app.Logger.LogInformation("Chronos Server {Version} · archive {Archive} · data {Data} · {Database} · TLS {Tls}{Fingerprint}",
|
|
SystemEndpoints.Version, options.ArchivePath, options.DataPath, state.Database, options.TlsMode,
|
|
state.TlsFingerprint is null ? string.Empty : " · SHA-256 " + state.TlsFingerprint);
|
|
if (archiveOpenToOthers)
|
|
{
|
|
app.Logger.LogWarning("Accounts other than SYSTEM, Administrators and the one Chronos Server runs as can change the archive {Archive}: "
|
|
+ "they could put a link in place of a folder and redirect the files the server moves. Let only those accounts change it (others may read it).",
|
|
options.ArchivePath);
|
|
}
|
|
// A space path that cannot be measured is warned about at start, not first when someone opens the dashboard.
|
|
if (!string.IsNullOrWhiteSpace(options.SpacePath)) app.Services.GetRequiredService<VolumeSpace>().Report(archive.Root);
|
|
if (options.FormerWorkersVariable)
|
|
{
|
|
app.Logger.LogWarning("CHRONOS_WORKERS is the former name of CHRONOS_PARALLELISM: jobs now run one at a time, and the value is how many "
|
|
+ "photos the running job processes at once. Rename the variable.");
|
|
}
|
|
app.Lifetime.ApplicationStopping.Register(() => app.Logger.LogInformation("Chronos Server stopping"));
|
|
app.Lifetime.ApplicationStopped.Register(() => database.DisposeAsync().AsTask().GetAwaiter().GetResult());
|
|
return app;
|
|
}
|
|
catch
|
|
{
|
|
database.DisposeAsync().AsTask().GetAwaiter().GetResult();
|
|
throw;
|
|
}
|
|
}
|
|
|
|
/// <summary>The single composition root of the server's services.</summary>
|
|
public static void ConfigureServices(IServiceCollection services, ServerOptions options, ServerState state, ChronosDatabase database, ServerArchive archive)
|
|
{
|
|
services.AddSingleton(options);
|
|
services.AddSingleton(state);
|
|
services.AddSingleton(database);
|
|
services.AddSingleton(archive);
|
|
services.TryAddSingleton(TimeProvider.System);
|
|
// Tests register their own first, to simulate a full volume.
|
|
services.TryAddSingleton<VolumeSpace>();
|
|
services.ConfigureHttpJsonOptions(j => j.SerializerOptions.TypeInfoResolverChain.Insert(0, ProtocolJson.Default));
|
|
services.Configure<RouteHandlerOptions>(o => o.ThrowOnBadRequest = true);
|
|
services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));
|
|
if (options.TlsMode == TlsMode.Proxy)
|
|
{
|
|
// Only the configured proxies may say who the client is: a client that reaches the HTTP port directly would
|
|
// otherwise pick its own address for the sign-in rate limit and the audit log, and claim HTTPS.
|
|
var trusted = options.TrustedProxyNetworks();
|
|
services.Configure<ForwardedHeadersOptions>(f =>
|
|
{
|
|
f.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto | ForwardedHeaders.XForwardedHost;
|
|
f.KnownIPNetworks.Clear();
|
|
f.KnownProxies.Clear();
|
|
foreach (var network in trusted) f.KnownIPNetworks.Add(network);
|
|
});
|
|
}
|
|
|
|
services.AddSingleton<EventHub>();
|
|
services.AddSingleton<TokenService>();
|
|
services.AddSingleton<LoginGuard>();
|
|
services.AddSingleton<AuditLog>();
|
|
services.AddSingleton<ServerSettingsStore>();
|
|
services.AddSingleton<UploadStore>();
|
|
services.AddSingleton<UploadService>();
|
|
services.AddSingleton<RenderStore>();
|
|
services.AddSingleton<RenderUploads>();
|
|
services.AddSingleton<SequenceJobs>();
|
|
services.AddSingleton<RenderNodeService>();
|
|
services.AddSingleton<JobEngineHost>();
|
|
services.AddHostedService(sp => sp.GetRequiredService<JobEngineHost>());
|
|
// After the engine's host: the scheduler queues on its engine, starts after it and stops before it (owner review 2, R10).
|
|
services.AddSingleton<ServerScheduleHost>();
|
|
services.AddSingleton<SchedulerHost>();
|
|
services.AddHostedService(sp => sp.GetRequiredService<SchedulerHost>());
|
|
services.AddSingleton<RenderNodeMonitor>();
|
|
services.AddHostedService(sp => sp.GetRequiredService<RenderNodeMonitor>());
|
|
// Recovers interrupted commits at start; its sweep is the clean-up step of the upload-cleanup schedule.
|
|
services.AddSingleton<UploadMaintenance>();
|
|
services.AddHostedService(sp => sp.GetRequiredService<UploadMaintenance>());
|
|
services.AddSingleton<ICleanupStep, UploadCleanupStep>();
|
|
services.AddSingleton<DiscoveryBeaconService>();
|
|
services.AddHostedService(sp => sp.GetRequiredService<DiscoveryBeaconService>());
|
|
}
|
|
|
|
private static void ConfigurePipeline(WebApplication app, ServerOptions options)
|
|
{
|
|
if (options.TlsMode == TlsMode.Proxy) app.UseForwardedHeaders();
|
|
app.UseMiddleware<ResponseMiddleware>();
|
|
app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromSeconds(30) });
|
|
app.UseRouting();
|
|
app.UseMiddleware<AuthenticationMiddleware>();
|
|
|
|
AdminPanel.Map(app, options);
|
|
var api = app.MapGroup("/api/v1");
|
|
SystemEndpoints.Map(app, api);
|
|
AuthEndpoints.Map(api);
|
|
UserEndpoints.Map(api);
|
|
SequenceEndpoints.Map(api);
|
|
LibraryEndpoints.Map(api);
|
|
FileEndpoints.Map(api);
|
|
UploadEndpoints.Map(api);
|
|
JobEndpoints.Map(api);
|
|
ScheduleEndpoints.Map(api);
|
|
NodeEndpoints.Map(api);
|
|
ArchiveEndpoints.Map(api);
|
|
TrashEndpoints.Map(api);
|
|
AdminEndpoints.Map(api);
|
|
DiagnosticsEndpoints.Map(api);
|
|
EventEndpoints.Map(api);
|
|
app.MapFallback("/api/{**path}", (HttpContext context) => ApiErrors.NotFound(context)).AllowAnonymousCaller();
|
|
}
|
|
|
|
/// <summary>Fails fast when the archive module's services are missing (the server would accept uploads it cannot commit).</summary>
|
|
private static void RequireArchiveModule(IServiceCollection services)
|
|
{
|
|
var missing = new List<string>();
|
|
if (!services.Any(d => d.ServiceType == typeof(IArchiveIngest))) missing.Add($"{typeof(IArchiveIngest).FullName} (commits verified uploads into the archive layout)");
|
|
if (!services.Any(d => d.ServiceType == typeof(IFrameAssetLocator))) missing.Add($"{typeof(IFrameAssetLocator).FullName} (finds proxies and thumbnails)");
|
|
if (missing.Count > 0)
|
|
{
|
|
throw new InvalidOperationException(
|
|
"Chronos Server cannot start: no implementation is registered for " + string.Join(" and ", missing) +
|
|
". Register them in the services callback of ServerHost.Build.");
|
|
}
|
|
}
|
|
}
|