feat: add application preloader, identity roles, and resilient database initialization with automated seeding

This commit is contained in:
2026-05-01 09:07:26 +02:00
parent 0210611edf
commit 47bffd629f
11 changed files with 262 additions and 21 deletions
+31 -3
View File
@@ -81,6 +81,7 @@ builder.Services.AddAuthentication(options =>
});
builder.Services.AddIdentityApiEndpoints<NexusUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<AppDbContext>();
builder.Services.ConfigureApplicationCookie(options =>
@@ -113,11 +114,38 @@ builder.Services.Configure<IdentityOptions>(options =>
var app = builder.Build();
// Ensure Database is initialized
// Ensure Database is initialized and seeded
using (var scope = app.Services.CreateScope())
{
var dbContext = scope.ServiceProvider.GetRequiredService<NexusReader.Infrastructure.Persistence.AppDbContext>();
await dbContext.Database.MigrateAsync();
var services = scope.ServiceProvider;
var logger = services.GetRequiredService<ILogger<Program>>();
var dbContext = services.GetRequiredService<NexusReader.Infrastructure.Persistence.AppDbContext>();
int maxRetries = 5;
int delayMs = 2000;
for (int i = 0; i < maxRetries; i++)
{
try
{
logger.LogInformation("Próba połączenia z bazą danych (próba {Attempt}/{MaxRetries})...", i + 1, maxRetries);
await dbContext.Database.MigrateAsync();
await DbInitializer.SeedAsync(services);
logger.LogInformation("Baza danych zainicjowana pomyślnie.");
break;
}
catch (Npgsql.NpgsqlException ex) when (i < maxRetries - 1)
{
logger.LogWarning("Błąd połączenia z bazą danych: {Message}. Ponowna próba za {Delay}ms...", ex.Message, delayMs);
await Task.Delay(delayMs);
delayMs *= 2; // Exponential backoff
}
catch (Exception ex)
{
logger.LogCritical(ex, "Krytyczny błąd podczas inicjalizacji bazy danych.");
throw;
}
}
}
// Configure the HTTP request pipeline.