Wp2ghost/GhostImageManager/Program.cs

161 lines
5.3 KiB
C#
Raw Normal View History

2026-09-24 19:23:54 +02:00
using GhostImageManager.Components;
2026-09-28 19:09:09 +02:00
using GhostImageManager.Data;
2026-09-24 19:23:54 +02:00
using GhostImageManager.Models;
using GhostImageManager.Services;
using Microsoft.AspNetCore.Authentication.Cookies;
2026-09-28 19:09:09 +02:00
using Microsoft.EntityFrameworkCore;
using Serilog;
2026-09-24 19:23:54 +02:00
var builder = WebApplication.CreateBuilder(args);
2026-09-28 19:09:09 +02:00
// Logging konfigurieren
var logsDir = Path.Combine(builder.Environment.ContentRootPath, "logs");
if (!Directory.Exists(logsDir))
Directory.CreateDirectory(logsDir);
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Information()
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
.MinimumLevel.Override("Microsoft.AspNetCore", Serilog.Events.LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.Console()
.WriteTo.File(
Path.Combine(logsDir, "ghost-image-manager-.log"),
rollingInterval: RollingInterval.Day,
retainedFileCountLimit: 30,
outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}{Exception}")
.CreateLogger();
builder.Host.UseSerilog();
2026-09-24 19:23:54 +02:00
// Add services to the container.
builder.Services.AddRazorComponents()
.AddInteractiveServerComponents();
2026-09-28 19:09:09 +02:00
// Datenbank
var dbPath = Path.Combine(builder.Environment.ContentRootPath, "ghost-image-manager.db");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlite($"Data Source={dbPath}"));
2026-09-24 19:23:54 +02:00
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>();
2026-09-28 19:09:09 +02:00
builder.Services.AddScoped<ImageDatabaseService>();
builder.Services.AddScoped<ExifService>();
2026-09-24 19:23:54 +02:00
builder.Services.AddScoped<AuthService>();
2026-09-28 19:09:09 +02:00
builder.Services.AddSingleton<LogService>();
2026-09-24 19:23:54 +02:00
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();
2026-09-28 19:09:09 +02:00
// Datenbank sicherstellen
using (var scope = app.Services.CreateScope())
{
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
2026-10-14 19:12:29 +02:00
// EnsureCreated ergänzt keine Spalten in einer bestehenden DB – später hinzugekommene nachziehen
var existingColumns = db.Database.SqlQueryRaw<string>("SELECT name AS Value FROM pragma_table_info('ManagedImages')").ToList();
foreach (var (column, type) in new[]
{
("Category", "TEXT NULL"),
("CategorySecondary", "TEXT NULL"),
("HasMultipleCategories", "INTEGER NOT NULL DEFAULT 0"),
("Keywords", "TEXT NULL")
})
{
if (!existingColumns.Contains(column, StringComparer.OrdinalIgnoreCase))
db.Database.ExecuteSqlRaw($"ALTER TABLE ManagedImages ADD COLUMN {column} {type}");
}
2026-09-28 19:09:09 +02:00
}
2026-09-24 19:23:54 +02:00
// 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
2026-09-28 19:09:09 +02:00
app.MapPost("/auth/login", async (HttpContext context, AuthService authService, ILogger<Program> logger) =>
2026-09-24 19:23:54 +02:00
{
var form = await context.Request.ReadFormAsync();
var password = form["password"].ToString();
var returnUrl = form["returnUrl"].ToString();
if (string.IsNullOrEmpty(returnUrl))
returnUrl = "/";
try
{
2026-09-28 19:09:09 +02:00
logger.LogInformation("Benutzer meldet sich an");
2026-09-24 19:23:54 +02:00
await authService.SignInAsync(context, password);
2026-09-28 19:09:09 +02:00
logger.LogInformation("Anmeldung erfolgreich");
2026-09-24 19:23:54 +02:00
return Results.Redirect(returnUrl);
}
catch (UnauthorizedAccessException)
{
2026-09-28 19:09:09 +02:00
logger.LogWarning("Fehlgeschlagene Anmeldung");
2026-09-24 19:23:54 +02:00
return Results.Redirect($"/login?errorMessage={Uri.EscapeDataString("Ungültiges Passwort.")}&returnUrl={Uri.EscapeDataString(returnUrl)}");
}
});
2026-09-28 19:09:09 +02:00
app.MapPost("/auth/logout", async (HttpContext context, AuthService authService, ILogger<Program> logger) =>
2026-09-24 19:23:54 +02:00
{
2026-09-28 19:09:09 +02:00
logger.LogInformation("Benutzer meldet sich ab");
2026-09-24 19:23:54 +02:00
await authService.SignOutAsync(context);
return Results.Redirect("/login");
});
app.Run();
2026-09-28 19:09:09 +02:00
Log.CloseAndFlush();