feat: add application preloader, identity roles, and resilient database initialization with automated seeding
This commit is contained in:
@@ -0,0 +1,44 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using NexusReader.Infrastructure.Persistence;
|
||||||
|
using Microsoft.Extensions.Configuration;
|
||||||
|
using NexusReader.Domain.Entities;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
var configuration = new ConfigurationBuilder()
|
||||||
|
.AddJsonFile("src/NexusReader.Web.New/appsettings.json")
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var services = new ServiceCollection();
|
||||||
|
var pgConnectionString = configuration.GetConnectionString("PostgresConnection");
|
||||||
|
if (!string.IsNullOrEmpty(pgConnectionString))
|
||||||
|
{
|
||||||
|
services.AddDbContext<AppDbContext>(options => options.UseNpgsql(pgConnectionString));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
services.AddDbContext<AppDbContext>(options => options.UseSqlite(configuration.GetConnectionString("SqliteConnection")));
|
||||||
|
}
|
||||||
|
|
||||||
|
var serviceProvider = services.BuildServiceProvider();
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var user = await dbContext.Users.FirstOrDefaultAsync(u => u.Email == "admin@nexus.com");
|
||||||
|
if (user == null)
|
||||||
|
{
|
||||||
|
Console.WriteLine("User admin@nexus.com NOT FOUND in database.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine($"User found: {user.Email}, Id: {user.Id}, EmailConfirmed: {user.EmailConfirmed}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"Error accessing database: {ex.Message}");
|
||||||
|
}
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
using Microsoft.AspNetCore.Identity;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using NexusReader.Domain.Entities;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace NexusReader.Infrastructure.Persistence;
|
||||||
|
|
||||||
|
public static class DbInitializer
|
||||||
|
{
|
||||||
|
public static async Task SeedAsync(IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
using var scope = serviceProvider.CreateScope();
|
||||||
|
var userManager = scope.ServiceProvider.GetRequiredService<UserManager<NexusUser>>();
|
||||||
|
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Console.WriteLine("[Seeder] Starting database seeding...");
|
||||||
|
|
||||||
|
// Seed Roles
|
||||||
|
string[] roleNames = { "Admin", "User" };
|
||||||
|
foreach (var roleName in roleNames)
|
||||||
|
{
|
||||||
|
var roleExist = await roleManager.RoleExistsAsync(roleName);
|
||||||
|
if (!roleExist)
|
||||||
|
{
|
||||||
|
await roleManager.CreateAsync(new IdentityRole(roleName));
|
||||||
|
Console.WriteLine($"[Seeder] Created role: {roleName}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed Admin User
|
||||||
|
var adminEmail = "admin@nexus.com";
|
||||||
|
var adminUser = await userManager.FindByEmailAsync(adminEmail);
|
||||||
|
|
||||||
|
if (adminUser == null)
|
||||||
|
{
|
||||||
|
adminUser = new NexusUser
|
||||||
|
{
|
||||||
|
UserName = adminEmail,
|
||||||
|
Email = adminEmail,
|
||||||
|
EmailConfirmed = true,
|
||||||
|
CurrentPlan = "Enterprise",
|
||||||
|
AITokenLimit = 1000000,
|
||||||
|
TenantId = Guid.NewGuid()
|
||||||
|
};
|
||||||
|
|
||||||
|
var createPowerUser = await userManager.CreateAsync(adminUser, "Admin123!");
|
||||||
|
if (createPowerUser.Succeeded)
|
||||||
|
{
|
||||||
|
await userManager.AddToRoleAsync(adminUser, "Admin");
|
||||||
|
Console.WriteLine($"[Seeder] Admin user created successfully: {adminEmail}");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var errors = string.Join(", ", createPowerUser.Errors.Select(e => e.Description));
|
||||||
|
Console.WriteLine($"[Seeder] Failed to create admin user: {errors}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Console.WriteLine("[Seeder] Admin user already exists.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"[Seeder] Critical error during seeding: {ex.Message}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,11 @@
|
|||||||
|
|
||||||
<Router AppAssembly="@typeof(NexusReader.UI.Shared._Imports).Assembly">
|
<Router AppAssembly="@typeof(NexusReader.UI.Shared._Imports).Assembly">
|
||||||
<Found Context="routeData">
|
<Found Context="routeData">
|
||||||
<RouteView RouteData="@routeData" DefaultLayout="@typeof(NexusReader.UI.Shared.Layout.MainLayout)" />
|
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(NexusReader.UI.Shared.Layout.MainLayout)">
|
||||||
|
<NotAuthorized>
|
||||||
|
<RedirectToLogin />
|
||||||
|
</NotAuthorized>
|
||||||
|
</AuthorizeRouteView>
|
||||||
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
|
||||||
</Found>
|
</Found>
|
||||||
<NotFound>
|
<NotFound>
|
||||||
|
|||||||
@@ -11,7 +11,12 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<div id="app">Loading...</div>
|
<div id="app">
|
||||||
|
<div id="app-preloader">
|
||||||
|
<div class="preloader-spinner"></div>
|
||||||
|
<div class="preloader-text">Nexus Reader</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="blazor-error-ui">
|
<div id="blazor-error-ui">
|
||||||
An unhandled error has occurred.
|
An unhandled error has occurred.
|
||||||
|
|||||||
@@ -11,7 +11,9 @@
|
|||||||
@inject NavigationManager NavigationManager
|
@inject NavigationManager NavigationManager
|
||||||
@implements IDisposable
|
@implements IDisposable
|
||||||
|
|
||||||
<div class="app-container @_platformClass @(FocusMode.IsFocusModeActive ? "focus-mode-active" : "")">
|
<AuthorizeView>
|
||||||
|
<Authorized>
|
||||||
|
<div class="app-container @_platformClass @(FocusMode.IsFocusModeActive ? "focus-mode-active" : "")">
|
||||||
<div class="reader-pane">
|
<div class="reader-pane">
|
||||||
<main>
|
<main>
|
||||||
@Body
|
@Body
|
||||||
@@ -21,8 +23,6 @@
|
|||||||
|
|
||||||
<div class="resizer" id="sidebar-resizer"></div>
|
<div class="resizer" id="sidebar-resizer"></div>
|
||||||
|
|
||||||
<AuthorizeView>
|
|
||||||
<Authorized>
|
|
||||||
<div class="intelligence-sidebar">
|
<div class="intelligence-sidebar">
|
||||||
<IntelligenceToolbar />
|
<IntelligenceToolbar />
|
||||||
<div class="intelligence-content">
|
<div class="intelligence-content">
|
||||||
@@ -46,9 +46,18 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</Authorized>
|
</Authorized>
|
||||||
</AuthorizeView>
|
<Authorizing>
|
||||||
</div>
|
<div class="app-preloader">
|
||||||
|
<div class="preloader-spinner"></div>
|
||||||
|
<div class="preloader-text">Weryfikacja...</div>
|
||||||
|
</div>
|
||||||
|
</Authorizing>
|
||||||
|
<NotAuthorized>
|
||||||
|
@Body
|
||||||
|
</NotAuthorized>
|
||||||
|
</AuthorizeView>
|
||||||
|
|
||||||
<div id="blazor-error-ui" data-nosnippet>
|
<div id="blazor-error-ui" data-nosnippet>
|
||||||
An unhandled error has occurred.
|
An unhandled error has occurred.
|
||||||
|
|||||||
@@ -100,3 +100,56 @@ h1:focus {
|
|||||||
margin: 1rem;
|
margin: 1rem;
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Preloader Styles */
|
||||||
|
#app-preloader, .app-preloader {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
background: radial-gradient(circle at center, #1a1a1a 0%, var(--nexus-bg) 100%);
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
z-index: 9999;
|
||||||
|
transition: opacity 0.8s ease, visibility 0.8s;
|
||||||
|
}
|
||||||
|
|
||||||
|
#app-preloader.loaded {
|
||||||
|
opacity: 0;
|
||||||
|
visibility: hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preloader-spinner {
|
||||||
|
width: 80px;
|
||||||
|
height: 80px;
|
||||||
|
border: 3px solid rgba(0, 255, 153, 0.1);
|
||||||
|
border-top: 3px solid var(--nexus-neon);
|
||||||
|
border-radius: 50%;
|
||||||
|
animation: spin 1s cubic-bezier(0.4, 0, 0.2, 1) infinite;
|
||||||
|
filter: drop-shadow(0 0 10px var(--nexus-neon));
|
||||||
|
margin-bottom: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.preloader-text {
|
||||||
|
color: var(--nexus-neon);
|
||||||
|
font-family: var(--nexus-font-sans);
|
||||||
|
letter-spacing: 4px;
|
||||||
|
text-transform: uppercase;
|
||||||
|
font-size: 0.8rem;
|
||||||
|
font-weight: 500;
|
||||||
|
animation: pulse 2s infinite;
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes spin {
|
||||||
|
0% { transform: rotate(0deg); }
|
||||||
|
100% { transform: rotate(360deg); }
|
||||||
|
}
|
||||||
|
|
||||||
|
@keyframes pulse {
|
||||||
|
0%, 100% { opacity: 1; transform: scale(1); }
|
||||||
|
50% { opacity: 0.5; transform: scale(0.95); }
|
||||||
|
}
|
||||||
|
|
||||||
@@ -5,7 +5,6 @@
|
|||||||
<meta charset="utf-8" />
|
<meta charset="utf-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<base href="/" />
|
<base href="/" />
|
||||||
<ResourcePreloader />
|
|
||||||
<link rel="stylesheet" href="_content/NexusReader.UI.Shared/app.css" />
|
<link rel="stylesheet" href="_content/NexusReader.UI.Shared/app.css" />
|
||||||
<link rel="stylesheet" href="NexusReader.Web.styles.css" />
|
<link rel="stylesheet" href="NexusReader.Web.styles.css" />
|
||||||
<ImportMap />
|
<ImportMap />
|
||||||
@@ -14,9 +13,35 @@
|
|||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<div id="app-preloader">
|
||||||
|
<div class="preloader-spinner"></div>
|
||||||
|
<div class="preloader-text">Nexus Reader</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<NexusReader.UI.Shared.Routes @rendermode="InteractiveAuto" />
|
<NexusReader.UI.Shared.Routes @rendermode="InteractiveAuto" />
|
||||||
<ReconnectModal />
|
<ReconnectModal />
|
||||||
<script src="_framework/blazor.web.js"></script>
|
<script src="_framework/blazor.web.js"></script>
|
||||||
|
<script>
|
||||||
|
(function() {
|
||||||
|
function hidePreloader() {
|
||||||
|
const preloader = document.getElementById('app-preloader');
|
||||||
|
if (preloader) {
|
||||||
|
preloader.classList.add('loaded');
|
||||||
|
// Completely remove from DOM after transition for better accessibility
|
||||||
|
setTimeout(() => preloader.style.display = 'none', 1000);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (document.readyState === 'complete') {
|
||||||
|
hidePreloader();
|
||||||
|
} else {
|
||||||
|
window.addEventListener('load', hidePreloader);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fallback: If for some reason 'load' doesn't fire (e.g. big assets), hide after 3s anyway
|
||||||
|
setTimeout(hidePreloader, 3000);
|
||||||
|
})();
|
||||||
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -81,6 +81,7 @@ builder.Services.AddAuthentication(options =>
|
|||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddIdentityApiEndpoints<NexusUser>()
|
builder.Services.AddIdentityApiEndpoints<NexusUser>()
|
||||||
|
.AddRoles<IdentityRole>()
|
||||||
.AddEntityFrameworkStores<AppDbContext>();
|
.AddEntityFrameworkStores<AppDbContext>();
|
||||||
|
|
||||||
builder.Services.ConfigureApplicationCookie(options =>
|
builder.Services.ConfigureApplicationCookie(options =>
|
||||||
@@ -113,11 +114,38 @@ builder.Services.Configure<IdentityOptions>(options =>
|
|||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Ensure Database is initialized
|
// Ensure Database is initialized and seeded
|
||||||
using (var scope = app.Services.CreateScope())
|
using (var scope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var dbContext = scope.ServiceProvider.GetRequiredService<NexusReader.Infrastructure.Persistence.AppDbContext>();
|
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 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.
|
// Configure the HTTP request pipeline.
|
||||||
|
|||||||
@@ -26,5 +26,6 @@
|
|||||||
"Model": "gemini-2.5-flash-lite",
|
"Model": "gemini-2.5-flash-lite",
|
||||||
"MaxOutputTokens": 8192
|
"MaxOutputTokens": 8192
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
"ApiBaseUrl": "http://localhost:5000"
|
||||||
}
|
}
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user