using LiteCharms.Features.Extensions; using LiteCharms.Features.Mediator; using Microsoft.AspNetCore.Authentication; using ShopAdmin.Components; using static LiteCharms.Features.Email.Extensions.Constants; var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); builder.Services.AddCascadingAuthenticationState(); builder.AddMonitoring(); builder.Services.AddControllers(); builder.Services.AddBlazoredToast(); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddMediator(); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(TelemetryPipelineBehavior<,>)); builder.Services.AddScoped(typeof(IPipelineBehavior<,>), typeof(LoggingPipelineBehavior<,>)); builder.Services.AddSalesServiceBus(); builder.Services.AddGeneralServiceBus(); builder.Services.AddQuartzSchedulerClient(ShopSchedulerName, builder.Configuration); builder.Services.AddEmailServices(builder.Configuration); builder.Services.AddEmailServiceBus(); builder.Services.AddShopServices(); builder.Services.AddShopDatabase(builder.Configuration); builder.Services.AddPostgresHealtchCheck(); builder.Services.AddQuartzHealtchCheck(); builder.Services.AddHealthChecksSupport(builder.Configuration); builder.Services.AddAuthentication(options => { options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme; }) .AddCookie() .AddOpenIdConnect(options => { options.Authority = builder.Configuration.GetSection("IdKongisa:Authority").Value; options.ClientId = builder.Configuration.GetSection("IdKongisa:ClientId").Value; options.ClientSecret = builder.Configuration.GetSection("IdKongisa:ClientSecret").Value; options.ResponseType = "code"; options.SaveTokens = true; options.GetClaimsFromUserInfoEndpoint = true; options.MetadataAddress = $"{options.Authority}/.well-known/openid-configuration"; options.Scope.Clear(); options.Scope.Add("openid"); options.Scope.Add("profile"); options.Scope.Add("email"); options.TokenValidationParameters = new Microsoft.IdentityModel.Tokens.TokenValidationParameters { NameClaimType = "name", RoleClaimType = "groups" }; }); var app = builder.Build(); var schedulerFactory = app.Services.GetRequiredService(); var scheduler = await schedulerFactory.GetScheduler(ShopSchedulerName); 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.UseAuthentication(); app.UseAuthorization(); app.MapStaticAssets(); app.MapGet("/auth/login", (string redirectUri = "/") => Results.Challenge(new AuthenticationProperties { RedirectUri = redirectUri },[OpenIdConnectDefaults.AuthenticationScheme])); app.MapGet("/auth/logout", () => Results.SignOut(new AuthenticationProperties { RedirectUri = "/" }, [CookieAuthenticationDefaults.AuthenticationScheme, OpenIdConnectDefaults.AuthenticationScheme])); app.MapRazorComponents() .AddInteractiveServerRenderMode(); app.Run();