using ElmahCore; using ElmahCore.Mvc; using Serilog; using Microsoft.AspNetCore.Authentication.Cookies; using Berufsschule_HAM.Services; using Berufsschule_HAM.Models; using Berufsschule_HAM.HealthChecks; using System.Text.Json.Serialization; var builder = WebApplication.CreateBuilder(args); // Bind options builder.Services.Configure(builder.Configuration.GetSection("Ldap")); builder.Services.AddLocalization(options => options.ResourcesPath = "Resources"); builder.Services.AddControllersWithViews() .AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix) .AddDataAnnotationsLocalization() .AddJsonOptions(options => options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter())); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(builder.Configuration) .CreateLogger(); builder.Services.AddSerilog(); builder.Services.AddElmah(Options => { Options.LogPath = builder.Configuration.GetValue("Elmah:LogFolder") ?? "~/logs"; }); builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddHealthChecks() .AddCheck("DatabaseHealthCheck"); builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/Home/Login"; options.LogoutPath = "/Home/Login/Logout"; options.AccessDeniedPath = "/Home/AccessDenied"; }); var app = builder.Build(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Home/Error"); } app.Use(async (HttpContext context, RequestDelegate next) => { if (context.Request.Path.StartsWithSegments("/elmah") || context.Request.Path.StartsWithSegments("/swagger")) { if (!(context.User?.Identity?.IsAuthenticated ?? false)) { context.Response.Redirect("/Home/Login"); return; } } await next(context); }); app.UseElmah(); if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); string[] supportedCultures = ["de", "en"]; var localizationOptions = new RequestLocalizationOptions() .SetDefaultCulture(supportedCultures.First()) .AddSupportedCultures(supportedCultures) .AddSupportedUICultures(supportedCultures); app.UseRequestLocalization(localizationOptions); app.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); app.MapHealthChecks("/healthz") .RequireAuthorization(); // Run migrations using var scope = app.Services.CreateScope(); var migrationService = scope.ServiceProvider.GetRequiredService(); app.Run();