From 0bb5da351392347054efefc4c4a34993da21ce08 Mon Sep 17 00:00:00 2001 From: Khwezi Mngoma Date: Sun, 14 Jun 2026 23:40:47 +0200 Subject: [PATCH] Updates app pipelining and cleaned up service registration --- MidrandBookshop/MidrandBookshop.csproj | 5 +- MidrandBookshop/Program.cs | 108 ++++++------------------- MidrandBookshop/Setup.cs | 53 ++++++++++++ 3 files changed, 81 insertions(+), 85 deletions(-) create mode 100644 MidrandBookshop/Setup.cs diff --git a/MidrandBookshop/MidrandBookshop.csproj b/MidrandBookshop/MidrandBookshop.csproj index 255a7ba..cf7b0c2 100644 --- a/MidrandBookshop/MidrandBookshop.csproj +++ b/MidrandBookshop/MidrandBookshop.csproj @@ -18,13 +18,13 @@ - + - + @@ -62,6 +62,7 @@ + diff --git a/MidrandBookshop/Program.cs b/MidrandBookshop/Program.cs index 22f538f..9a1dac3 100644 --- a/MidrandBookshop/Program.cs +++ b/MidrandBookshop/Program.cs @@ -1,109 +1,51 @@ using LiteCharms.Features.Extensions; -using LiteCharms.Features.Mediator; -using LiteCharms.Features.MidrandBooks.Extensions; -using LiteCharms.Features.MidrandBooks.Payments; using LiteCharms.Features.Postgres; +using MidrandBookshop; 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); -}); +builder.Services.RegisterServices(builder.Configuration); 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.UseForwardedHeaders(); +app.UseHttpsRedirection(); + +app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true); app.UseHealthChecks("/health", new HealthCheckOptions { ResponseWriter = HealthChecks.UI.Client.UIResponseWriter.WriteHealthCheckUIResponse }); -app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true); -app.UseHttpsRedirection(); - -app.UseAntiforgery(); - app.MapStaticAssets(); +app.UseCookiePolicy(); + +app.UseAuthentication(); +app.UseAuthorization(); +app.UseAntiforgery(); +app.AddSecurityEndpoints(); + +using (var security = app.Services.CreateScope()) +{ + var dataProtectionContext = security.ServiceProvider.GetRequiredService(); + + await dataProtectionContext.Database.MigrateAsync(); +} + +var schedulerFactory = app.Services.GetRequiredService(); +var scheduler = await schedulerFactory.GetScheduler(MidrandShopSchedulerName); + +if (!scheduler!.IsStarted) await scheduler.Start(); + app.MapRazorComponents() .AddInteractiveServerRenderMode(); diff --git a/MidrandBookshop/Setup.cs b/MidrandBookshop/Setup.cs new file mode 100644 index 0000000..4780053 --- /dev/null +++ b/MidrandBookshop/Setup.cs @@ -0,0 +1,53 @@ +using LiteCharms.Features.Mediator; +using LiteCharms.Features.MidrandBooks.Payments; +using LiteCharms.Features.Extensions; +using LiteCharms.Features.MidrandBooks.Extensions; +using static LiteCharms.Features.Extensions.Quartz; + +namespace MidrandBookshop; + +public static class Setup +{ + public static IServiceCollection RegisterServices(this IServiceCollection services, IConfiguration configuration) + { + services.AddAntiforgery(); + + services.AddRazorComponents() + .AddInteractiveServerComponents(); + + services.AddEndpointsApiExplorer(); + + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>)); + services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>)); + + services.AddQuartzSchedulerClient(MidrandShopSchedulerName, configuration); + + services.AddMediator(); + services.AddEmailServices(configuration); + services.AddEmailServiceBus(); + + services.AddHttpClient(); + services.AddScoped(); + services.AddShopServices(includeLocalStorage: true); + services.AddHashServices(configuration); + services.AddPayfastServices(configuration); + + services.AddDataProtectionDatabase(configuration); + services.AddMidrandShopDatabase(configuration); + + services.AddSecurityApiSdk(configuration); + services.AddLiteCharmsWebSecurity(configuration); + + services.AddMidrandShopPostgresHealthCheck(); + services.AddMidrandShopQuartzHealthCheck(); + services.AddHealthChecksSupport(configuration); + + services.Configure(options => + { + options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto; + options.KnownProxies.Clear(); + }); + + return services; + } +}