Files
Chronos/src/Titano.Server/Api/NodeEndpoints.cs
T
Claude c78747053e feat(server): API v1 with auth, uploads, sync, jobs, render nodes and events
- Security: PBKDF2-SHA512 (210,000 iterations) hashes, opaque bearer tokens
  stored as SHA-256 with sliding expiry and revocation, HttpOnly/Secure/
  SameSite=Strict admin cookie with a session-bound CSRF token, sign-in rate
  limit and lockout, role/permission metadata on every endpoint, audit log,
  first-run administrator bootstrap.
- Sequences with keyset paging, ETags and settings_version conflicts; sync
  manifest; frames, proxies, thumbnails and exports streamed with Range.
- Chunked resumable uploads with hash short-circuit, quota and disk checks,
  staging on the archive volume, SHA-256 verification and IArchiveIngest.
- Job engine host, jobs API, render nodes with heartbeat timeout and
  reassignment, WebSocket events, LAN discovery beacon, upload cleanup.
- Windows service host over advapi32 (--service, --install-service,
  --uninstall-service); fails fast without the archive module services.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
2026-10-02 11:32:19 +00:00

58 lines
4.1 KiB
C#

using Titano.Core.Model;
using Titano.Data;
using Titano.Protocol;
using Titano.Server.Events;
using Titano.Server.Jobs;
using Titano.Server.Security;
namespace Titano.Server.Api;
/// <summary><c>/api/v1/nodes</c> (BRIEF §14.7): render node registration, heartbeat, claim, progress, failure and output upload.</summary>
public static class NodeEndpoints
{
public static void Map(RouteGroupBuilder api)
{
api.MapGet("/nodes", ListAsync).Requires(Permissions.Read | Permissions.RenderNode);
api.MapDelete("/nodes/{id:guid}", DeleteAsync).Requires(Permissions.RenderNode | Permissions.ManageArchive);
var nodes = api.MapGroup("/nodes").Requires(Permissions.RenderNode);
nodes.MapPost("/register", async (HttpContext c, Caller caller, RegisterNodeRequest request, RenderNodeService service) =>
(await service.RegisterAsync(caller, request, c.RequestAborted)).ToResult(c, n => Results.Json(n, ProtocolJson.Default.RenderNodeDto)));
nodes.MapPost("/{id:guid}/heartbeat", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) =>
(await service.HeartbeatAsync(caller, id, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
nodes.MapPost("/{id:guid}/claim", async (HttpContext c, Caller caller, Guid id, RenderNodeService service) =>
(await service.ClaimAsync(caller, id, c.RequestAborted)).ToResult(c, job => job is null ? Results.NoContent() : Results.Json(job, ProtocolJson.Default.ClaimedJob)));
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/progress", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeProgress progress, RenderNodeService service) =>
(await service.ProgressAsync(caller, id, jobId, progress, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/failed", async (HttpContext c, Caller caller, Guid id, Guid jobId, NodeJobFailure failure, RenderNodeService service) =>
(await service.FailAsync(caller, id, jobId, failure, c.RequestAborted)).ToResult(c, _ => Results.NoContent()));
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderResultRequest request, RenderNodeService service) =>
(await service.StartResultAsync(caller, id, jobId, request, c.RequestAborted))
.ToResult(c, r => Results.Json(r, ProtocolJson.Default.StartUploadResponse, statusCode: StatusCodes.Status201Created)));
nodes.MapPost("/{id:guid}/jobs/{jobId:guid}/result/complete", async (HttpContext c, Caller caller, Guid id, Guid jobId, RenderNodeService service,
IHostApplicationLifetime lifetime) =>
// The commit runs to the end even if the node disconnects meanwhile.
(await service.CompleteResultAsync(caller, id, jobId, lifetime.ApplicationStopping)).ToResult(c, r => Results.Json(r, ProtocolJson.Default.RenderDto)));
}
private static async Task<IResult> ListAsync(HttpContext context, TitanoDatabase db)
{
var ct = context.RequestAborted;
var users = (await db.Users.ListAsync(ct)).ToDictionary(u => u.Id, u => u.Username);
var nodes = await db.RenderNodes.ListAsync(ct);
return Results.Json(nodes.Select(n => Dto.Node(n, users.GetValueOrDefault(n.UserId, string.Empty))).ToList(), ProtocolJson.Default.ListRenderNodeDto);
}
/// <summary>Forgets a node (its owner or an archive admin); its running jobs go back to the queue.</summary>
private static async Task<IResult> DeleteAsync(HttpContext context, Caller caller, Guid id, TitanoDatabase db, EventHub events)
{
var ct = context.RequestAborted;
if (await db.RenderNodes.GetAsync(id, ct) is not { } node) return ApiErrors.NotFound(context, "node");
if (node.UserId != caller.Id && !caller.Has(Permissions.ManageArchive)) return ApiErrors.Forbidden(context);
await db.Jobs.RequeueRunningAsync(RenderNodeService.NodeKey(node), ct);
await db.RenderNodes.DeleteAsync(id, ct);
events.Publish(new NodeStateEvent(node.Id, node.ClientName, RenderNodeService.Offline));
return Results.NoContent();
}
}