using System.Net.Mime; using Ardalis.ListStartupServices; using Azure.Identity; using BlazorAdmin; using BlazorAdmin.Services; using Blazored.LocalStorage; using BlazorShared; using Microsoft.AspNetCore.Authentication.Cookies; using Microsoft.AspNetCore.Diagnostics.HealthChecks; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.EntityFrameworkCore; using Microsoft.eShopWeb; using Microsoft.eShopWeb.ApplicationCore.Interfaces; using Microsoft.eShopWeb.Infrastructure.Data; using Microsoft.eShopWeb.Infrastructure.Identity; using Microsoft.eShopWeb.Web; using Microsoft.eShopWeb.Web.Configuration; using Microsoft.eShopWeb.Web.HealthChecks; using Microsoft.Extensions.Diagnostics.HealthChecks; var builder = WebApplication.CreateBuilder(args); builder.Logging.AddConsole(); // Configure SQL Server var credential = new ChainedTokenCredential(new AzureDeveloperCliCredential(), new DefaultAzureCredential()); builder.Configuration.AddAzureKeyVault(new Uri(builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"]), credential); builder.Services.AddDbContext(c => { var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_CATALOG_CONNECTION_STRING_KEY"]]; c.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); }); builder.Services.AddDbContext(options => { var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_IDENTITY_CONNECTION_STRING_KEY"]]; options.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); }); builder.Services.AddCookieSettings(); builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.Cookie.HttpOnly = true; options.Cookie.SecurePolicy = CookieSecurePolicy.Always; options.Cookie.SameSite = SameSiteMode.Lax; }); builder.Services.AddIdentity() .AddDefaultUI() .AddEntityFrameworkStores() .AddDefaultTokenProviders(); builder.Services.AddScoped(); builder.Configuration.AddEnvironmentVariables(); builder.Services.AddCoreServices(builder.Configuration); builder.Services.AddWebServices(builder.Configuration); // Add memory cache services builder.Services.AddMemoryCache(); builder.Services.AddRouting(options => { // Replace the type and the name used to refer to it with your own // IOutboundParameterTransformer implementation options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); }); builder.Services.AddMvc(options => { options.Conventions.Add(new RouteTokenTransformerConvention( new SlugifyParameterTransformer())); }); builder.Services.AddControllersWithViews(); builder.Services.AddRazorPages(options => { options.Conventions.AuthorizePage("/Basket/Checkout"); }); builder.Services.AddHttpContextAccessor(); builder.Services .AddHealthChecks() .AddCheck("api_health_check", tags: new[] { "apiHealthCheck" }) .AddCheck("home_page_health_check", tags: new[] { "homePageHealthCheck" }); builder.Services.Configure(config => { config.Services = new List(builder.Services); config.Path = "/allservices"; }); // blazor configuration var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); builder.Services.Configure(configSection); var baseUrlConfig = configSection.Get(); // Blazor Admin Required Services for Prerendering builder.Services.AddScoped(s => new HttpClient { BaseAddress = new Uri(baseUrlConfig.WebBase) }); // add blazor services builder.Services.AddBlazoredLocalStorage(); builder.Services.AddServerSideBlazor(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddBlazorServices(); builder.Services.AddDatabaseDeveloperPageExceptionFilter(); var app = builder.Build(); app.Logger.LogInformation("App created..."); app.Logger.LogInformation("Seeding Database..."); using (var scope = app.Services.CreateScope()) { var scopedProvider = scope.ServiceProvider; try { var catalogContext = scopedProvider.GetRequiredService(); await CatalogContextSeed.SeedAsync(catalogContext, app.Logger); var userManager = scopedProvider.GetRequiredService>(); var roleManager = scopedProvider.GetRequiredService>(); var identityContext = scopedProvider.GetRequiredService(); await AppIdentityDbContextSeed.SeedAsync(identityContext, userManager, roleManager); } catch (Exception ex) { app.Logger.LogError(ex, "An error occurred seeding the DB."); } } var catalogBaseUrl = builder.Configuration.GetValue(typeof(string), "CatalogBaseUrl") as string; if (!string.IsNullOrEmpty(catalogBaseUrl)) { app.Use((context, next) => { context.Request.PathBase = new PathString(catalogBaseUrl); return next(); }); } app.UseHealthChecks("/health", new HealthCheckOptions { ResponseWriter = async (context, report) => { var result = new { status = report.Status.ToString(), errors = report.Entries.Select(e => new { key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status) }) }.ToJson(); context.Response.ContentType = MediaTypeNames.Application.Json; await context.Response.WriteAsync(result); } }); if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Docker") { app.Logger.LogInformation("Adding Development middleware..."); app.UseDeveloperExceptionPage(); app.UseShowAllServicesMiddleware(); app.UseMigrationsEndPoint(); app.UseWebAssemblyDebugging(); } else { app.Logger.LogInformation("Adding non-Development middleware..."); app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseBlazorFrameworkFiles(); app.UseStaticFiles(); app.UseRouting(); app.UseCookiePolicy(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}"); endpoints.MapRazorPages(); endpoints.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") }); endpoints.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") }); //endpoints.MapBlazorHub("/admin"); endpoints.MapFallbackToFile("index.html"); }); app.Logger.LogInformation("LAUNCHING"); app.Run();