Files
Berufsschule_HAM/src/Program.cs
2025-11-01 23:11:26 +01:00

137 lines
3.9 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.AddHostedService<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 =
[
"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(new StaticFileOptions
{
OnPrepareResponse = ctx =>
{
string requestPath = ctx.Context.Request.Path.ToString();
if (requestPath.EndsWith(".css") || requestPath.EndsWith(".js") || requestPath.EndsWith(".png") || requestPath.EndsWith(".ico"))
{
ctx.Context.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromDays(365)
};
} else if (requestPath.Contains("UserPhoto"))
{
ctx.Context.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromMinutes(5)
};
}
}
});
app.UseRouting();
app.UseAuthorization();
app.UseResponseCaching();
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();
app.Run();