using Backend.Common; using Backend.Data; using Backend.Endpoints; using Backend.Security; using Microsoft.EntityFrameworkCore; namespace Backend.Extensions; public static class WebApplicationExtensions { public static void UseApplicationPipeline(this WebApplication app) { app.UseExceptionHandler(); if (!app.Environment.IsDevelopment()) { app.UseHsts(); } if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseCors(ApplicationDefaults.FrontendCorsPolicy); app.UseMiddleware(); app.UseRateLimiter(); if (!app.Environment.IsDevelopment()) { app.UseHttpsRedirection(); } } public static async Task InitializeDatabaseAsync(this WebApplication app) { using var scope = app.Services.CreateScope(); var db = scope.ServiceProvider.GetRequiredService(); var logger = scope.ServiceProvider.GetRequiredService() .CreateLogger("DatabaseInitialization"); try { if (app.Environment.IsDevelopment()) { await db.Database.MigrateAsync(); } await SessionBootstrapper.EnsureAsync(db); await OperationalTablesBootstrapper.EnsureAsync(db); await TeamAccountBootstrapper.EnsureAsync(db, app.Configuration); if (ShouldSeedPresentationData(app)) { await SeedDataBootstrapper.EnsureAsync(db); } } catch (Exception error) { logger.LogError(error, "Database initialization failed. Check the PostgreSQL connection, migrations, and seed data."); throw; } } public static void MapApplicationEndpoints(this WebApplication app) { app.MapSystemEndpoints(); app.MapAuthEndpoints(); app.MapPublicEndpoints(); app.MapAdminEndpoints(); } private static bool ShouldSeedPresentationData(WebApplication app) { var mode = app.Configuration["VTSA_SEED_MODE"] ?? app.Configuration["SeedData:Mode"]; if (string.IsNullOrWhiteSpace(mode)) { return app.Environment.IsDevelopment(); } return mode.Trim().ToLowerInvariant() is "demo" or "presentation" or "sample"; } }