|
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 | | - |
22 | 1 | var builder = WebApplication.CreateBuilder(args); |
23 | 2 | builder.Logging.AddConsole(); |
24 | 3 |
|
25 | | -if (builder.Environment.IsDevelopment() || builder.Environment.EnvironmentName == "Docker"){ |
26 | | - // Configure SQL Server (local) |
27 | | - Microsoft.eShopWeb.Infrastructure.Dependencies.ConfigureServices(builder.Configuration, builder.Services); |
| 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")); |
28 | 14 | } |
29 | | -else{ |
30 | | - // Configure SQL Server (prod) |
| 15 | +else |
| 16 | +{ |
| 17 | + // Real SQL Server (Azure) - opcional si decides usar SQL real |
31 | 18 | var credential = new ChainedTokenCredential(new AzureDeveloperCliCredential(), new DefaultAzureCredential()); |
32 | 19 | builder.Configuration.AddAzureKeyVault(new Uri(builder.Configuration["AZURE_KEY_VAULT_ENDPOINT"] ?? ""), credential); |
| 20 | + |
33 | 21 | builder.Services.AddDbContext<CatalogContext>(c => |
34 | 22 | { |
35 | 23 | var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_CATALOG_CONNECTION_STRING_KEY"] ?? ""]; |
36 | 24 | c.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); |
37 | 25 | }); |
| 26 | + |
38 | 27 | builder.Services.AddDbContext<AppIdentityDbContext>(options => |
39 | 28 | { |
40 | 29 | var connectionString = builder.Configuration[builder.Configuration["AZURE_SQL_IDENTITY_CONNECTION_STRING_KEY"] ?? ""]; |
41 | 30 | options.UseSqlServer(connectionString, sqlOptions => sqlOptions.EnableRetryOnFailure()); |
42 | 31 | }); |
43 | 32 | } |
| 33 | +// --------------------------------------------------------- |
44 | 34 |
|
45 | 35 | builder.Services.AddCookieSettings(); |
46 | | - |
47 | 36 | builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) |
48 | 37 | .AddCookie(options => |
49 | 38 | { |
|
55 | 44 | builder.Services.AddIdentity<ApplicationUser, IdentityRole>() |
56 | 45 | .AddDefaultUI() |
57 | 46 | .AddEntityFrameworkStores<AppIdentityDbContext>() |
58 | | - .AddDefaultTokenProviders(); |
| 47 | + .AddDefaultTokenProviders(); |
59 | 48 |
|
60 | 49 | builder.Services.AddScoped<ITokenClaimsService, IdentityTokenClaimService>(); |
61 | 50 | builder.Configuration.AddEnvironmentVariables(); |
62 | 51 | builder.Services.AddCoreServices(builder.Configuration); |
63 | 52 | builder.Services.AddWebServices(builder.Configuration); |
64 | 53 |
|
65 | | -// Add memory cache services |
66 | 54 | builder.Services.AddMemoryCache(); |
67 | 55 | builder.Services.AddRouting(options => |
68 | 56 | { |
69 | | - // Replace the type and the name used to refer to it with your own |
70 | | - // IOutboundParameterTransformer implementation |
71 | 57 | options.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer); |
72 | 58 | }); |
73 | 59 |
|
74 | 60 | builder.Services.AddMvc(options => |
75 | 61 | { |
76 | | - options.Conventions.Add(new RouteTokenTransformerConvention( |
77 | | - new SlugifyParameterTransformer())); |
78 | | - |
| 62 | + options.Conventions.Add(new RouteTokenTransformerConvention(new SlugifyParameterTransformer())); |
79 | 63 | }); |
| 64 | + |
80 | 65 | builder.Services.AddControllersWithViews(); |
81 | 66 | builder.Services.AddRazorPages(options => |
82 | 67 | { |
83 | 68 | options.Conventions.AuthorizePage("/Basket/Checkout"); |
84 | 69 | }); |
85 | 70 | builder.Services.AddHttpContextAccessor(); |
86 | | -builder.Services |
87 | | - .AddHealthChecks() |
| 71 | +builder.Services.AddHealthChecks() |
88 | 72 | .AddCheck<ApiHealthCheck>("api_health_check", tags: new[] { "apiHealthCheck" }) |
89 | 73 | .AddCheck<HomePageHealthCheck>("home_page_health_check", tags: new[] { "homePageHealthCheck" }); |
90 | 74 | builder.Services.Configure<ServiceConfig>(config => |
|
93 | 77 | config.Path = "/allservices"; |
94 | 78 | }); |
95 | 79 |
|
96 | | -// blazor configuration |
97 | 80 | var configSection = builder.Configuration.GetRequiredSection(BaseUrlConfiguration.CONFIG_NAME); |
98 | 81 | builder.Services.Configure<BaseUrlConfiguration>(configSection); |
99 | 82 | var baseUrlConfig = configSection.Get<BaseUrlConfiguration>(); |
| 83 | +builder.Services.AddScoped<HttpClient>(s => new HttpClient { BaseAddress = new Uri(baseUrlConfig!.WebBase) }); |
100 | 84 |
|
101 | | -// Blazor Admin Required Services for Prerendering |
102 | | -builder.Services.AddScoped<HttpClient>(s => new HttpClient |
103 | | -{ |
104 | | - BaseAddress = new Uri(baseUrlConfig!.WebBase) |
105 | | -}); |
106 | | - |
107 | | -// add blazor services |
108 | 85 | builder.Services.AddBlazoredLocalStorage(); |
109 | 86 | builder.Services.AddServerSideBlazor(); |
110 | 87 | builder.Services.AddScoped<ToastService>(); |
|
116 | 93 | var app = builder.Build(); |
117 | 94 |
|
118 | 95 | app.Logger.LogInformation("App created..."); |
119 | | - |
120 | 96 | app.Logger.LogInformation("Seeding Database..."); |
121 | 97 |
|
122 | 98 | using (var scope = app.Services.CreateScope()) |
|
138 | 114 | } |
139 | 115 | } |
140 | 116 |
|
| 117 | +// ------------------- APP PIPELINE ------------------- |
141 | 118 | var catalogBaseUrl = builder.Configuration.GetValue(typeof(string), "CatalogBaseUrl") as string; |
142 | 119 | if (!string.IsNullOrEmpty(catalogBaseUrl)) |
143 | 120 | { |
|
148 | 125 | }); |
149 | 126 | } |
150 | 127 |
|
151 | | -app.UseHealthChecks("/health", |
152 | | - new HealthCheckOptions |
| 128 | +app.UseHealthChecks("/health", new HealthCheckOptions |
| 129 | +{ |
| 130 | + ResponseWriter = async (context, report) => |
153 | 131 | { |
154 | | - ResponseWriter = async (context, report) => |
| 132 | + var result = new |
155 | 133 | { |
156 | | - var result = new |
157 | | - { |
158 | | - status = report.Status.ToString(), |
159 | | - errors = report.Entries.Select(e => new |
160 | | - { |
161 | | - key = e.Key, |
162 | | - value = Enum.GetName(typeof(HealthStatus), e.Value.Status) |
163 | | - }) |
164 | | - }.ToJson(); |
165 | | - context.Response.ContentType = MediaTypeNames.Application.Json; |
166 | | - await context.Response.WriteAsync(result); |
167 | | - } |
168 | | - }); |
| 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 | + |
169 | 142 | if (app.Environment.IsDevelopment() || app.Environment.EnvironmentName == "Docker") |
170 | 143 | { |
171 | | - app.Logger.LogInformation("Adding Development middleware..."); |
172 | 144 | app.UseDeveloperExceptionPage(); |
173 | 145 | app.UseShowAllServicesMiddleware(); |
174 | 146 | app.UseMigrationsEndPoint(); |
175 | 147 | app.UseWebAssemblyDebugging(); |
176 | 148 | } |
177 | 149 | else |
178 | 150 | { |
179 | | - app.Logger.LogInformation("Adding non-Development middleware..."); |
180 | 151 | app.UseExceptionHandler("/Error"); |
181 | 152 | app.UseHsts(); |
182 | 153 | } |
|
190 | 161 | app.UseAuthentication(); |
191 | 162 | app.UseAuthorization(); |
192 | 163 |
|
193 | | - |
194 | 164 | app.MapControllerRoute("default", "{controller:slugify=Home}/{action:slugify=Index}/{id?}"); |
195 | 165 | app.MapRazorPages(); |
196 | 166 | app.MapHealthChecks("home_page_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("homePageHealthCheck") }); |
197 | 167 | app.MapHealthChecks("api_health_check", new HealthCheckOptions { Predicate = check => check.Tags.Contains("apiHealthCheck") }); |
198 | | -//endpoints.MapBlazorHub("/admin"); |
199 | 168 | app.MapFallbackToFile("index.html"); |
200 | 169 |
|
| 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 | + |
201 | 175 | app.Logger.LogInformation("LAUNCHING"); |
202 | 176 | app.Run(); |
0 commit comments