mirror of
https://github.com/LD-Reborn/Berufsschule_HAM.git
synced 2025-12-20 06:51:55 +00:00
116 lines
3.2 KiB
C#
116 lines
3.2 KiB
C#
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<LdapConfig>(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<XmlFileErrorLog>(Options =>
|
|
{
|
|
Options.LogPath = builder.Configuration.GetValue<string>("Elmah:LogFolder") ?? "~/logs";
|
|
});
|
|
|
|
builder.Services.AddSingleton<LdapService>();
|
|
builder.Services.AddSingleton<MigrationService>();
|
|
|
|
builder.Services.AddHealthChecks()
|
|
.AddCheck<DatabaseHealthCheck>("DatabaseHealthCheck");
|
|
|
|
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
.AddCookie(options =>
|
|
{
|
|
options.LoginPath = "/Home/Login";
|
|
options.LogoutPath = "/Home/Login/Logout";
|
|
options.AccessDeniedPath = "/Home/AccessDenied";
|
|
});
|
|
|
|
builder.Services.AddResponseCompression(options =>
|
|
{
|
|
options.EnableForHttps = true;
|
|
options.MimeTypes = new[]
|
|
{
|
|
"text/plain",
|
|
"text/css",
|
|
"application/javascript",
|
|
"text/html",
|
|
"application/xml",
|
|
"text/xml",
|
|
"application/json",
|
|
"image/svg+xml"
|
|
};
|
|
});
|
|
|
|
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();
|
|
app.UseResponseCompression();
|
|
|
|
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<MigrationService>();
|
|
|
|
app.Run();
|