using LiteCharms.Features.Extensions; using LiteCharms.Features.Mediator; using LiteCharms.Features.MidrandBooks.Extensions; using LiteCharms.Features.MidrandBooks.Payments; using LiteCharms.Features.Postgres; using MidrandBookshop.Components; using System.Security.Cryptography.X509Certificates; using static LiteCharms.Features.Extensions.Quartz; var builder = WebApplication.CreateBuilder(args); builder.Services.AddAntiforgery(); builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.AddMonitoring(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>)); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>)); builder.Services.AddQuartzSchedulerClient(MidrandShopSchedulerName, builder.Configuration); builder.Services.AddMediator(); builder.Services.AddEmailServices(builder.Configuration); builder.Services.AddEmailServiceBus(); builder.Services.AddHttpClient(); builder.Services.AddScoped(); builder.Services.AddShopServices(includeLocalStorage: true); builder.Services.AddHashServices(builder.Configuration); builder.Services.AddPayfastServices(builder.Configuration); builder.Services.AddDataProtectionDatabase(builder.Configuration); builder.Services.AddMidrandShopDatabase(builder.Configuration); builder.Services.AddSecurityApiSdk(builder.Configuration); builder.Services.AddLiteCharmsWebSecurity(builder.Configuration); builder.Services.AddMidrandShopPostgresHealthCheck(); builder.Services.AddMidrandShopQuartzHealthCheck(); builder.Services.AddHealthChecksSupport(builder.Configuration); builder.Services.Configure(options => { options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; options.KnownProxies.Clear(); }); builder.WebHost.ConfigureKestrel(options => { var certBase64 = builder.Configuration["DataProtection:Certificate"]; var certPassword = builder.Configuration["DataProtection:Password"]; if (!string.IsNullOrWhiteSpace(certBase64)) { var rawBytes = Convert.FromBase64String(certBase64); var kestrelCert = X509CertificateLoader.LoadPkcs12(rawBytes, certPassword); options.ListenAnyIP(8443, listenOptions => { listenOptions.UseHttps(kestrelCert); }); } else options.ListenAnyIP(8080); }); var app = builder.Build(); app.UseForwardedHeaders(); app.UseCookiePolicy(); using var security = app.Services.CreateScope(); { var dataProtectionContext = security.ServiceProvider.GetRequiredService(); await dataProtectionContext.Database.MigrateAsync(); } app.AddSecurityEndpoints(); var schedulerFactory = app.Services.GetRequiredService(); var scheduler = await schedulerFactory.GetScheduler(MidrandShopSchedulerName); if (!scheduler!.IsStarted) await scheduler.Start(); if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error", createScopeForErrors: true); app.UseHsts(); } app.UseHealthChecks("/health", new HealthCheckOptions { ResponseWriter = HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse }); app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true); app.UseHttpsRedirection(); app.UseAntiforgery(); app.MapStaticAssets(); app.MapRazorComponents() .AddInteractiveServerRenderMode(); app.Run();