Wp2ghost/GhostImageManager/Program.cs

103 lines
3 KiB
C#
Raw Normal View History

2026-09-24 19:23:54 +02:00
using GhostImageManager.Components;
using GhostImageManager.Models;
using GhostImageManager.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));
builder.Services.AddSingleton<GhostConfigService>();
builder.Services.AddScoped<GhostApiService>();
builder.Services.AddScoped<ImageAnalyzerService>();
builder.Services.AddScoped<ImageReplacementService>();
builder.Services.AddScoped<ImageDeletionService>();
builder.Services.AddScoped<ImageExportService>();
builder.Services.AddScoped<AuthService>();
builder.Services.AddHttpClient<GhostApiService>();
// Authentication
builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
options.LoginPath = "/login";
options.LogoutPath = "/logout";
options.AccessDeniedPath = "/access-denied";
options.ExpireTimeSpan = TimeSpan.FromMinutes(480);
options.SlidingExpiration = true;
});
builder.Services.AddAuthorization();
builder.Services.AddCascadingAuthenticationState();
// Upload-Limit erhöhen (für Bildtausch)
builder.WebHost.ConfigureKestrel(options =>
{
options.Limits.MaxRequestBodySize = 104857600; // 100 MB
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error", createScopeForErrors: true);
app.UseHsts();
}
app.UseHttpsRedirection();
// Standard wwwroot
app.UseStaticFiles();
// Zusätzlich: start.jpg aus dem App-Root ausliefern
var rootImagePath = Path.Combine(app.Environment.ContentRootPath, "start.jpg");
if (File.Exists(rootImagePath))
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new Microsoft.Extensions.FileProviders.PhysicalFileProvider(app.Environment.ContentRootPath),
RequestPath = "/start-image"
});
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseAntiforgery();
app.MapRazorComponents<App>()
.AddInteractiveServerRenderMode();
// Authentication API endpoints
app.MapPost("/auth/login", async (HttpContext context, AuthService authService) =>
{
var form = await context.Request.ReadFormAsync();
var password = form["password"].ToString();
var returnUrl = form["returnUrl"].ToString();
if (string.IsNullOrEmpty(returnUrl))
returnUrl = "/";
try
{
await authService.SignInAsync(context, password);
return Results.Redirect(returnUrl);
}
catch (UnauthorizedAccessException)
{
return Results.Redirect($"/login?errorMessage={Uri.EscapeDataString("Ungültiges Passwort.")}&returnUrl={Uri.EscapeDataString(returnUrl)}");
}
});
app.MapPost("/auth/logout", async (HttpContext context, AuthService authService) =>
{
await authService.SignOutAsync(context);
return Results.Redirect("/login");
});
app.Run();