2026-04-25 22:45:59 -05:00
using ApplianceRepair ;
2026-02-03 16:55:37 -06:00
using ApplianceRepair.Components ;
using Microsoft.EntityFrameworkCore ;
2026-04-25 22:45:59 -05:00
using ApplianceRepair.Components.Account ;
using Microsoft.AspNetCore.Components.Authorization ;
using Microsoft.AspNetCore.Identity ;
2026-02-03 16:55:37 -06:00
var builder = WebApplication . CreateBuilder ( args ) ;
2026-05-01 19:48:34 -05:00
var connectionString = builder . Configuration . GetConnectionString ( "DefaultConnection" ) ? ? throw new InvalidOperationException ( "Connection string 'DefaultConnection' not found." ) ;
2026-02-03 16:55:37 -06:00
// Add services to the container.
builder . Services . AddRazorComponents ( )
. AddInteractiveServerComponents ( ) ;
builder . Services . AddDbContext < DatabaseContext > ( options = >
2026-05-01 19:48:34 -05:00
options . UseSqlite ( connectionString ) ) ;
2026-02-03 16:55:37 -06:00
builder . Services . AddMemoryCache ( ) ;
builder . Services . AddLogging ( ) ;
builder . Services . AddScoped < BusinessConfigReader > ( ) ;
builder . Services . AddScoped < ContentCardReader > ( ) ;
builder . Services . AddScoped < HomePageReader > ( ) ;
2026-04-25 13:14:08 -05:00
builder . Services . AddScoped < RepairRequestReader > ( ) ;
builder . Services . AddScoped < RepairRequestMediaReader > ( ) ;
2026-02-03 16:55:37 -06:00
2026-04-25 22:45:59 -05:00
builder . Services . AddCascadingAuthenticationState ( ) ;
builder . Services . AddScoped < IdentityUserAccessor > ( ) ;
builder . Services . AddScoped < IdentityRedirectManager > ( ) ;
builder . Services . AddScoped < AuthenticationStateProvider , IdentityRevalidatingAuthenticationStateProvider > ( ) ;
builder . Services . AddAuthentication ( options = >
{
options . DefaultScheme = IdentityConstants . ApplicationScheme ;
options . DefaultSignInScheme = IdentityConstants . ExternalScheme ;
} )
. AddIdentityCookies ( ) ;
builder . Services . AddIdentityCore < IdentityUser > ( options = > options . SignIn . RequireConfirmedAccount = true )
. AddRoles < IdentityRole > ( )
. AddEntityFrameworkStores < DatabaseContext > ( )
. AddSignInManager ( )
. AddDefaultTokenProviders ( ) ;
2026-05-01 19:48:34 -05:00
builder . Services . AddServerSideBlazor ( )
. AddHubOptions ( options = >
{
options . MaximumReceiveMessageSize = 10 * 1024 * 1024 ; // 10MB
} ) ;
2026-04-25 22:45:59 -05:00
builder . Services . AddSingleton < IEmailSender < IdentityUser > , IdentityNoOpEmailSender > ( ) ;
2026-04-25 13:14:08 -05:00
var app = builder . Build ( ) ;
2026-02-03 16:55:37 -06:00
using ( var scope = app . Services . CreateScope ( ) )
{
var services = scope . ServiceProvider ;
try
{
2026-04-25 13:14:08 -05:00
var context = services . GetRequiredService < DatabaseContext > ( ) ;
await context . Database . MigrateAsync ( ) ;
await DatabaseContext . Initialize ( context ) ;
2026-04-25 22:45:59 -05:00
var roleManager = scope . ServiceProvider . GetRequiredService < RoleManager < IdentityRole > > ( ) ;
var userManager = scope . ServiceProvider . GetRequiredService < UserManager < IdentityUser > > ( ) ;
var logger = scope . ServiceProvider . GetRequiredService < ILogger < Program > > ( ) ;
if ( ! await roleManager . RoleExistsAsync ( "Admin" ) )
{
await roleManager . CreateAsync ( new IdentityRole ( "Admin" ) ) ;
}
var domain = builder . Configuration . GetValue < string > ( "SiteDomain" ) ;
var adminEmail = $"admin@{domain}" ;
var adminUser = await userManager . FindByEmailAsync ( adminEmail ) ;
if ( adminUser = = null )
{
adminUser = new IdentityUser
{
UserName = adminEmail ,
Email = adminEmail ,
EmailConfirmed = true
} ;
const string chars = "ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz0123456789!@#$%^&*?" ;
var random = new Random ( ) ;
var pass = new string ( Enumerable . Repeat ( chars , 16 )
. Select ( s = > s [ random . Next ( s . Length ) ] ) . ToArray ( ) ) + "1aA!" ;
var result = await userManager . CreateAsync ( adminUser , pass ) ;
if ( result . Succeeded )
{
await userManager . AddToRoleAsync ( adminUser , "Admin" ) ;
logger . LogCritical ( "****************************************************" ) ;
logger . LogCritical ( $"ADMIN USER CREATED. Email: {adminEmail}" ) ;
logger . LogCritical ( $"TEMPORARY PASSWORD: {pass}" ) ;
logger . LogCritical ( "****************************************************" ) ;
}
}
2026-02-03 16:55:37 -06:00
}
catch ( Exception ex )
{
var logger = services . GetRequiredService < ILogger < Program > > ( ) ;
2026-04-25 13:14:08 -05:00
logger . LogError ( ex , "An error occurred while migrating or seeding the database." ) ;
2026-02-03 16:55:37 -06:00
}
}
// Configure the HTTP request pipeline.
if ( ! app . Environment . IsDevelopment ( ) )
{
app . UseExceptionHandler ( "/Error" , createScopeForErrors : true ) ;
}
app . UseAntiforgery ( ) ;
app . MapStaticAssets ( ) ;
app . MapRazorComponents < App > ( )
. AddInteractiveServerRenderMode ( ) ;
2026-04-25 22:45:59 -05:00
app . MapAdditionalIdentityEndpoints ( ) ; ;
2026-02-03 16:55:37 -06:00
app . Run ( ) ;