using Nuuru.Server.Services; namespace Nuuru.Server.Middleware { public class PanicMiddleware { private readonly RequestDelegate _next; public PanicMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context, ISiteSettingsService settingsService, IHostApplicationLifetime lifetime) { if (await settingsService.IsPanicActiveAsync()) { // Kill the process — Docker will restart it, but the flag // persists in the DB so it will keep dying on first request. context.Response.StatusCode = StatusCodes.Status503ServiceUnavailable; lifetime.StopApplication(); // Hard kill fallback in case graceful shutdown hangs _ = Task.Run(async () => { await Task.Delay(5000); Environment.Exit(1); }); return; } await _next(context); } } }