Spaces:
Running
Running
| using ECommerce.Model; | |
| using ECommerce.Model.Data; | |
| using ECommerce.Presenter; | |
| using Microsoft.AspNetCore.Identity; | |
| using Microsoft.EntityFrameworkCore; | |
| using Microsoft.AspNetCore.DataProtection; | |
| using Stripe; | |
| var builder = WebApplication.CreateBuilder(args); | |
| StripeConfiguration.ApiKey = builder.Configuration["Stripe:SecretKey"]; | |
| builder.Services.AddControllersWithViews(); | |
| builder.Services.AddRazorPages(); | |
| builder.Services.AddSession(); | |
| builder.Services.AddHttpContextAccessor(); | |
| builder.Services.AddModelLayer(builder.Configuration); | |
| builder.Services.AddPresenterLayer(); | |
| builder.Services.AddDefaultIdentity<IdentityUser>(options => | |
| { | |
| options.SignIn.RequireConfirmedAccount = false; | |
| options.Password.RequireDigit = true; | |
| options.Password.RequiredLength = 6; | |
| options.Password.RequireNonAlphanumeric = false; | |
| }) | |
| .AddRoles<IdentityRole>() | |
| .AddEntityFrameworkStores<AppDbContext>() | |
| .AddDefaultTokenProviders() | |
| .AddDefaultUI(); | |
| builder.Services.ConfigureApplicationCookie(options => | |
| { | |
| options.LoginPath = "/Account/Login"; | |
| options.LogoutPath = "/Account/Logout"; | |
| options.AccessDeniedPath = "/Account/AccessDenied"; | |
| }); | |
| builder.Services.AddDataProtection().SetApplicationName("DanStore"); | |
| builder.Services.AddAuthorization(options => | |
| { | |
| options.AddPolicy("AdminOnly", policy => policy.RequireRole("Admin")); | |
| }); | |
| var app = builder.Build(); | |
| using (var scope = app.Services.CreateScope()) | |
| { | |
| var sp = scope.ServiceProvider; | |
| var db = sp.GetRequiredService<AppDbContext>(); | |
| await db.Database.MigrateAsync(); | |
| await SeedData.InitializeAsync(db); | |
| var roleManager = sp.GetRequiredService<RoleManager<IdentityRole>>(); | |
| if (!await roleManager.RoleExistsAsync("Admin")) | |
| { | |
| await roleManager.CreateAsync(new IdentityRole("Admin")); | |
| } | |
| var userManager = sp.GetRequiredService<UserManager<IdentityUser>>(); | |
| var adminEmail = builder.Configuration["Admin:Email"] ?? "admin@store.com"; | |
| var adminPassword = builder.Configuration["Admin:Password"] ?? "Admin@123"; | |
| if (await userManager.FindByEmailAsync(adminEmail) == null) | |
| { | |
| var admin = new IdentityUser { UserName = adminEmail, Email = adminEmail, EmailConfirmed = true }; | |
| var result = await userManager.CreateAsync(admin, adminPassword); | |
| if (result.Succeeded) | |
| { | |
| await userManager.AddToRoleAsync(admin, "Admin"); | |
| } | |
| } | |
| } | |
| if (!app.Environment.IsDevelopment()) | |
| { | |
| app.UseExceptionHandler("/Home/Error"); | |
| app.UseHsts(); | |
| } | |
| app.UseHttpsRedirection(); | |
| app.UseStaticFiles(); | |
| app.UseRouting(); | |
| app.UseSession(); | |
| app.UseAuthentication(); | |
| app.UseAuthorization(); | |
| app.MapControllerRoute( | |
| name: "admin", | |
| pattern: "{area:exists}/{controller=Dashboard}/{action=Index}/{id?}"); | |
| app.MapControllerRoute( | |
| name: "default", | |
| pattern: "{controller=Home}/{action=Index}/{id?}").WithStaticAssets(); | |
| app.MapRazorPages(); | |
| // Health check endpoint for UptimeRobot (lightweight, no DB) | |
| app.MapGet("/health", () => Results.Ok("OK")); | |
| app.Run(); | |