|
| 1 | +using System.Net.Mime; |
| 2 | +using Ardalis.ListStartupServices; |
| 3 | +using Azure.Identity; |
| 4 | +using BlazorAdmin; |
| 5 | +using BlazorAdmin.Services; |
| 6 | +using Blazored.LocalStorage; |
| 7 | +using BlazorShared; |
| 8 | +using Microsoft.AspNetCore.Authentication.Cookies; |
| 9 | +using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| 10 | +using Microsoft.AspNetCore.Identity; |
| 11 | +using Microsoft.AspNetCore.Mvc.ApplicationModels; |
| 12 | +using Microsoft.EntityFrameworkCore; |
| 13 | +using Microsoft.eShopWeb; |
| 14 | +using Microsoft.eShopWeb.ApplicationCore.Interfaces; |
| 15 | +using Microsoft.eShopWeb.Infrastructure.Data; |
| 16 | +using Microsoft.eShopWeb.Infrastructure.Identity; |
| 17 | +using Microsoft.eShopWeb.Web; |
| 18 | +using Microsoft.eShopWeb.Web.Configuration; |
| 19 | +using Microsoft.eShopWeb.Web.HealthChecks; |
| 20 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 21 | + |
1 | 22 | var builder = WebApplication.CreateBuilder(args); |
2 | 23 | builder.Logging.AddConsole(); |
3 | 24 |
|
4 | | -// -------------------- DATABASE CONFIG -------------------- |
5 | | -bool useInMemoryDb = builder.Environment.IsDevelopment() || |
6 | | - builder.Environment.EnvironmentName == "Docker" || |
7 | | - bool.TryParse(builder.Configuration["UseInMemoryDatabase"], out var result) && result; |
8 | | - |
9 | | -if (useInMemoryDb) |
10 | | -{ |
11 | | - // In-memory database |
12 | | - builder.Services.AddDbContext<CatalogContext>(c => c.UseInMemoryDatabase("CatalogDb")); |
13 | | - builder.Services.AddDbContext<AppIdentityDbContext>(options => options.UseInMemoryDatabase("IdentityDb")); |
| 25 | +if (builder.Environment.IsDevelopment() || builder.Environment.EnvironmentName == "Docker"){ |
| 26 | + // Configure SQL Server (local) |
| 27 | + Microsoft.eShopWeb.Infrastructure.Dependencies.ConfigureServices(builder.Configuration, builder.Services); |
14 | 28 | } |
15 | | -else |
16 | | -{ |
17 | | - // Real SQL Server (Azure) - opcional si decides usar SQL real |
18 | | - var credential = new ChainedTokenCredential(new AzureDeveloperCliCredential(), new DefaultAzureCredential()); |
19 | | - builder.Configuration.AddAzureKeyVault(new Uri(builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"] ?? ""), credential); |
20 | | - |
21 | | - builder.Services.AddDbContext<CatalogContext>(c => |
22 | | - { |
23 | | - var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_CATALOG_CONNECTION_STRING_KEY"] ?? ""]; |
24 | | - c.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); |
25 | | - }); |
26 | | - |
27 | | - builder.Services.AddDbContext<AppIdentityDbContext>(options => |
28 | | - { |
29 | | - var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_IDENTITY_CONNECTION_STRING_KEY"] ?? ""]; |
30 | | - options.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); |
31 | | - }); |
| 29 | +else{ |
| 30 | + // Configure SQL Server (prod) |
| 31 | + Microsoft.eShopWeb.Infrastructure.Dependencies.ConfigureServices(builder.Configuration, builder.Services); |
32 | 32 | } |
33 | | -// --------------------------------------------------------- |
34 | 33 |
|
35 | 34 | builder.Services.AddCookieSettings(); |
| 35 | + |
36 | 36 | builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) |
37 | 37 | .AddCookie(options => |
38 | 38 | { |
|
44 | 44 | builder.Services.AddIdentity<ApplicationUser, IdentityRole>() |
45 | 45 | .AddDefaultUI() |
46 | 46 | .AddEntityFrameworkStores<AppIdentityDbContext>() |
47 | | - .AddDefaultTokenProviders(); |
| 47 | + .AddDefaultTokenProviders(); |
48 | 48 |
|
49 | 49 | builder.Services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>(); |
50 | 50 | builder.Configuration.AddEnvironmentVariables(); |
51 | 51 | builder.Services.AddCoreServices(builder.Configuration); |
52 | 52 | builder.Services.AddWebServices(builder.Configuration); |
53 | 53 |
|
| 54 | +// Add memory cache services |
54 | 55 | builder.Services.AddMemoryCache(); |
55 | 56 | builder.Services.AddRouting(options => |
56 | 57 | { |
| 58 | + // Replace the type and the name used to refer to it with your own |
| 59 | + // IOutboundParameterTransformer implementation |
57 | 60 | options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); |
58 | 61 | }); |
59 | 62 |
|
60 | 63 | builder.Services.AddMvc(options => |
61 | 64 | { |
62 | | - options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer())); |
63 | | -}); |
| 65 | + options.Conventions.Add(new RouteTokenTransformerConvention( |
| 66 | + new SlugifyParameterTransformer())); |
64 | 67 |
|
| 68 | +}); |
65 | 69 | builder.Services.AddControllersWithViews(); |
66 | 70 | builder.Services.AddRazorPages(options => |
67 | 71 | { |
68 | 72 | options.Conventions.AuthorizePage("/Basket/Checkout"); |
69 | 73 | }); |
70 | 74 | builder.Services.AddHttpContextAccessor(); |
71 | | -builder.Services.AddHealthChecks() |
| 75 | +builder.Services |
| 76 | + .AddHealthChecks() |
72 | 77 | .AddCheck<ApiHealthCheck>("api_health_check", tags: new[] { "apiHealthCheck" }) |
73 | 78 | .AddCheck<HomePageHealthCheck>("home_page_health_check", tags: new[] { "homePageHealthCheck" }); |
74 | 79 | builder.Services.Configure<ServiceConfig>(config => |
|
77 | 82 | config.Path = "/allservices"; |
78 | 83 | }); |
79 | 84 |
|
| 85 | +// blazor configuration |
80 | 86 | var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); |
81 | 87 | builder.Services.Configure<BaseUrlConfiguration>(configSection); |
82 | 88 | var baseUrlConfig = configSection.Get<BaseUrlConfiguration>(); |
83 | | -builder.Services.AddScoped<HttpClient>(s => new HttpClient { BaseAddress = new Uri(baseUrlConfig!.WebBase) }); |
84 | 89 |
|
| 90 | +// Blazor Admin Required Services for Prerendering |
| 91 | +builder.Services.AddScoped<HttpClient>(s => new HttpClient |
| 92 | +{ |
| 93 | + BaseAddress = new Uri(baseUrlConfig!.WebBase) |
| 94 | +}); |
| 95 | + |
| 96 | +// add blazor services |
85 | 97 | builder.Services.AddBlazoredLocalStorage(); |
86 | 98 | builder.Services.AddServerSideBlazor(); |
87 | 99 | builder.Services.AddScoped<ToastService>(); |
|
93 | 105 | var app = builder.Build(); |
94 | 106 |
|
95 | 107 | app.Logger.LogInformation("App created..."); |
| 108 | + |
96 | 109 | app.Logger.LogInformation("Seeding Database..."); |
97 | 110 |
|
98 | 111 | using (var scope = app.Services.CreateScope()) |
|
114 | 127 | } |
115 | 128 | } |
116 | 129 |
|
117 | | -// ------------------- APP PIPELINE ------------------- |
118 | 130 | var catalogBaseUrl = builder.Configuration.GetValue(typeof(string), "CatalogBaseUrl") as string; |
119 | 131 | if (!string.IsNullOrEmpty(catalogBaseUrl)) |
120 | 132 | { |
|
125 | 137 | }); |
126 | 138 | } |
127 | 139 |
|
128 | | -app.UseHealthChecks("/health", new HealthCheckOptions |
129 | | -{ |
130 | | - ResponseWriter = async (context, report) => |
| 140 | +app.UseHealthChecks("/health", |
| 141 | + new HealthCheckOptions |
131 | 142 | { |
132 | | - var result = new |
| 143 | + ResponseWriter = async (context, report) => |
133 | 144 | { |
134 | | - status = report.Status.ToString(), |
135 | | - errors = report.Entries.Select(e => new { key = e.Key, value = Enum.GetName(typeof(HealthStatus), e.Value.Status) }) |
136 | | - }.ToJson(); |
137 | | - context.Response.ContentType = MediaTypeNames.Application.Json; |
138 | | - await context.Response.WriteAsync(result); |
139 | | - } |
140 | | -}); |
141 | | - |
| 145 | + var result = new |
| 146 | + { |
| 147 | + status = report.Status.ToString(), |
| 148 | + errors = report.Entries.Select(e => new |
| 149 | + { |
| 150 | + key = e.Key, |
| 151 | + value = Enum.GetName(typeof(HealthStatus), e.Value.Status) |
| 152 | + }) |
| 153 | + }.ToJson(); |
| 154 | + context.Response.ContentType = MediaTypeNames.Application.Json; |
| 155 | + await context.Response.WriteAsync(result); |
| 156 | + } |
| 157 | + }); |
142 | 158 | if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Docker") |
143 | 159 | { |
| 160 | + app.Logger.LogInformation("Adding Development middleware..."); |
144 | 161 | app.UseDeveloperExceptionPage(); |
145 | 162 | app.UseShowAllServicesMiddleware(); |
146 | 163 | app.UseMigrationsEndPoint(); |
147 | 164 | app.UseWebAssemblyDebugging(); |
148 | 165 | } |
149 | 166 | else |
150 | 167 | { |
| 168 | + app.Logger.LogInformation("Adding non-Development middleware..."); |
151 | 169 | app.UseExceptionHandler("/Error"); |
152 | 170 | app.UseHsts(); |
153 | 171 | } |
|
161 | 179 | app.UseAuthentication(); |
162 | 180 | app.UseAuthorization(); |
163 | 181 |
|
| 182 | + |
164 | 183 | app.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}"); |
165 | 184 | app.MapRazorPages(); |
166 | 185 | app.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") }); |
167 | 186 | app.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") }); |
| 187 | +//endpoints.MapBlazorHub("/admin"); |
168 | 188 | app.MapFallbackToFile("index.html"); |
169 | 189 |
|
170 | | -// ------------------- FORCE PORT ------------------- |
171 | | -var port = Environment.GetEnvironmentVariable("PORT") ?? "5000"; // Azure asigna PORT dinámicamente |
172 | | -app.Urls.Clear(); |
173 | | -app.Urls.Add($"http://*:{port}"); |
174 | | - |
175 | 190 | app.Logger.LogInformation("LAUNCHING"); |
176 | 191 | app.Run(); |
0 commit comments