111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using Microsoft.EntityFrameworkCore;
|
||
using Microsoft.OpenApi.Models;
|
||
|
||
using Serilog;
|
||
|
||
using watcher_monitoring.Data;
|
||
|
||
var builder = WebApplication.CreateBuilder(args);
|
||
|
||
// Serilog konfigurieren – nur Logs, die nicht von Microsoft stammen
|
||
Log.Logger = new LoggerConfiguration()
|
||
.MinimumLevel.Information()
|
||
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
|
||
.Enrich.FromLogContext()
|
||
.WriteTo.File(
|
||
"logs/watcher-.log",
|
||
outputTemplate: "[{Timestamp:yyyy-MM-dd HH:mm:ss}] [{Level:u3}] {Message:lj}{NewLine}{Exception}",
|
||
rollingInterval: RollingInterval.Day
|
||
)
|
||
.CreateLogger();
|
||
|
||
builder.Host.UseSerilog();
|
||
|
||
// ---------- Konfiguration ----------
|
||
DotNetEnv.Env.Load();
|
||
builder.Configuration.AddEnvironmentVariables();
|
||
|
||
// Konfiguration laden
|
||
var configuration = builder.Configuration;
|
||
|
||
// DbContext - Nur SQLite wird unterstützt
|
||
var sqliteConnectionString = configuration.GetConnectionString("Sqlite")
|
||
?? configuration["Database:ConnectionStrings:Sqlite"]
|
||
?? "Data Source=./persistence/watcher.db";
|
||
|
||
builder.Services.AddDbContext<WatcherDbContext>((serviceProvider, options) =>
|
||
{
|
||
options.UseSqlite(sqliteConnectionString);
|
||
});
|
||
|
||
// Add services to the container.
|
||
builder.Services.AddControllersWithViews();
|
||
|
||
// Health Checks
|
||
builder.Services.AddHealthChecks();
|
||
|
||
// Swagger API-Dokumentation
|
||
builder.Services.AddSwaggerGen(options =>
|
||
{
|
||
options.SwaggerDoc("v1", new OpenApiInfo { Title = "Watcher-Server API", Version = "v1" });
|
||
});
|
||
|
||
var app = builder.Build();
|
||
|
||
// Stelle sicher, dass das persistence-Verzeichnis existiert
|
||
var persistenceDir = Path.Combine(Directory.GetCurrentDirectory(), "persistence");
|
||
if (!Directory.Exists(persistenceDir))
|
||
{
|
||
Log.Information("Erstelle persistence-Verzeichnis: {PersistenceDir}", persistenceDir);
|
||
Directory.CreateDirectory(persistenceDir);
|
||
}
|
||
|
||
// Datenbank-Migration beim Start ausführen
|
||
using (var scope = app.Services.CreateScope())
|
||
{
|
||
var dbContext = scope.ServiceProvider.GetRequiredService<WatcherDbContext>();
|
||
try
|
||
{
|
||
Log.Information("Führe Datenbank-Migrationen aus...");
|
||
dbContext.Database.Migrate();
|
||
Log.Information("Datenbank-Migrationen erfolgreich angewendet");
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Log.Error(ex, "Fehler beim Ausführen der Datenbank-Migrationen");
|
||
throw;
|
||
}
|
||
}
|
||
|
||
// Configure the HTTP request pipeline.
|
||
if (!app.Environment.IsDevelopment())
|
||
{
|
||
app.UseExceptionHandler("/Home/Error");
|
||
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
|
||
app.UseHsts();
|
||
}
|
||
|
||
app.UseHttpsRedirection();
|
||
app.UseStaticFiles();
|
||
|
||
app.UseRouting();
|
||
|
||
// 🔹 Swagger aktivieren
|
||
app.UseSwagger();
|
||
app.UseSwaggerUI(options =>
|
||
{
|
||
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Watcher-Server API v1");
|
||
options.RoutePrefix = "api/v1/swagger";
|
||
});
|
||
|
||
app.UseAuthorization();
|
||
|
||
// Health Check Endpoint
|
||
app.MapHealthChecks("/health");
|
||
|
||
app.MapControllerRoute(
|
||
name: "default",
|
||
pattern: "{controller=Home}/{action=Index}/{id?}");
|
||
|
||
app.Run();
|