📄 MatDenDagen/Program.cs
using System.Threading;
using MatDenDagen;
using MatDenDagen.Components;
using MatDenDagen.Infrastructure.Storage;
using MatDenDagen.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.ConfigureOpenTelemetry();

builder.Services.AddStorageServices().AddAdminService().AddDateService().AddExportService();

builder.Services.AddRazorComponents();

builder
    .Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.LoginPath = "/admin/login";
        options.LogoutPath = "/admin/logout";
        options.AccessDeniedPath = "/admin/login";
        options.Cookie.Name = "AdminAuthCookie";
    });
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseAuthentication();
app.UseAuthorization();

app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);
app.MapStaticAssets();
app.MapRazorComponents<App>().DisableAntiforgery();

app.MapGet(
        "/admin/export.zip",
        async (
            [FromServices] ExportService exportService,
            HttpContext httpContext,
            HttpResponse httpResponse,
            CancellationToken cancellationToken
        ) =>
        {
            httpContext.Features.Get<IHttpBodyControlFeature>()?.AllowSynchronousIO = true;
            httpResponse.Headers.ContentType = "application/zip";
            httpResponse.Headers.ContentDisposition = "attachment; filename=\"export.zip\"";
            await exportService.ExportAllSubmissions(httpResponse.Body, cancellationToken);
        }
    )
    .RequireAuthorization(p => p.RequireRole("Admin"));

app.Run();