diff --git a/src/Web/WebSPA/AppSettings.cs b/src/Web/WebSPA/AppSettings.cs index 941a5fdcd..789356666 100644 --- a/src/Web/WebSPA/AppSettings.cs +++ b/src/Web/WebSPA/AppSettings.cs @@ -3,7 +3,6 @@ public class AppSettings { public string IdentityUrl { get; set; } - public string BasketUrl { get; set; } public string MarketingUrl { get; set; } public string PurchaseUrl { get; set; } diff --git a/src/Web/WebSPA/Extensions/Extensions.cs b/src/Web/WebSPA/Extensions/Extensions.cs new file mode 100644 index 000000000..862924daf --- /dev/null +++ b/src/Web/WebSPA/Extensions/Extensions.cs @@ -0,0 +1,12 @@ +internal static class Extensions +{ + public static IServiceCollection AddHealthChecks(this IServiceCollection services, IConfiguration configuration) + { + var hcBuilder = services.AddHealthChecks(); + + hcBuilder + .AddUrlGroup(_ => new Uri(configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" }); + + return services; + } +} diff --git a/src/Web/WebSPA/GlobalUsings.cs b/src/Web/WebSPA/GlobalUsings.cs index 07c26ded8..1b1e2dd7d 100644 --- a/src/Web/WebSPA/GlobalUsings.cs +++ b/src/Web/WebSPA/GlobalUsings.cs @@ -1,24 +1,6 @@ -global using eShopConContainers.WebSPA; -global using Microsoft.AspNetCore; -global using Microsoft.AspNetCore.Hosting; -global using Microsoft.Extensions.Configuration; -global using Microsoft.Extensions.Logging; -global using System.IO; +global using System.IO.Compression; global using eShopOnContainers.WebSPA; -global using HealthChecks.UI.Client; -global using Microsoft.AspNetCore.Antiforgery; -global using Microsoft.AspNetCore.Builder; -global using Microsoft.AspNetCore.DataProtection; -global using Microsoft.AspNetCore.Diagnostics.HealthChecks; -global using Microsoft.AspNetCore.Http; -global using Microsoft.AspNetCore.Mvc; global using Microsoft.AspNetCore.SpaServices.AngularCli; -global using Microsoft.Extensions.DependencyInjection; -global using Microsoft.Extensions.Diagnostics.HealthChecks; -global using Microsoft.Extensions.Hosting; -global using StackExchange.Redis; -global using System; -global using WebSPA.Infrastructure; global using Microsoft.Extensions.Options; -global using System.IO.Compression; -global using System.Linq; +global using Services.Common; +global using WebSPA.Infrastructure; diff --git a/src/Web/WebSPA/Program.cs b/src/Web/WebSPA/Program.cs index 82cf6feed..896180758 100644 --- a/src/Web/WebSPA/Program.cs +++ b/src/Web/WebSPA/Program.cs @@ -1,78 +1,21 @@ var builder = WebApplication.CreateBuilder(args); -builder.WebHost.UseContentRoot(Directory.GetCurrentDirectory()); -builder.Services.AddApplicationInsightsTelemetry(builder.Configuration); -builder.Services.AddApplicationInsightsKubernetesEnricher(); -builder.Services.AddHealthChecks() - .AddCheck("self", () => HealthCheckResult.Healthy()) - .AddUrlGroup(new Uri(builder.Configuration["IdentityUrlHC"]), name: "identityapi-check", tags: new string[] { "identityapi" }); +builder.AddServiceDefaults(); +builder.Services.AddHealthChecks(builder.Configuration); builder.Services.Configure(builder.Configuration); -if (builder.Configuration.GetValue("IsClusterEnv") == bool.TrueString) -{ - builder.Services.AddDataProtection(opts => - { - opts.ApplicationDiscriminator = "eshop.webspa"; - }) - .PersistKeysToStackExchangeRedis(ConnectionMultiplexer.Connect(builder.Configuration["DPConnectionString"]), "DataProtection-Keys"); -} - - -// Add Anti-forgery services and configure the header name that angular will use by default. -builder.Services.AddAntiforgery(options => options.HeaderName = "X-XSRF-TOKEN"); - -// Add controllers support and add a global AutoValidateAntiforgeryTokenFilter that will make the application check for an Anti-forgery token on all "mutating" requests (POST, PUT, DELETE). -// The AutoValidateAntiforgeryTokenFilter is an internal class registered when we register views, so we need to register controllers and views also. -builder.Services.AddControllersWithViews(options => options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute())) - .AddJsonOptions(options => - { - options.JsonSerializerOptions.PropertyNameCaseInsensitive = true; - }); // Setup where the compiled version of our spa application will be, when in production. -builder.Services.AddSpaStaticFiles(configuration => +builder.Services.AddSpaStaticFiles(options => { - configuration.RootPath = "wwwroot"; + options.RootPath = "wwwroot"; }); -builder.Logging.AddConfiguration(builder.Configuration.GetSection("Logging")); -builder.Logging.AddAzureWebAppDiagnostics(); - var app = builder.Build(); -// Here we add Angular default Anti-forgery cookie name on first load. https://angular.io/guide/http#security-xsrf-protection -// This cookie will be read by Angular app and its value will be sent back to the application as the header configured in .AddAntiforgery() -var antiForgery = app.Services.GetRequiredService(); -app.Use(next => context => -{ - string path = context.Request.Path.Value; - - if (string.Equals(path, "/", StringComparison.OrdinalIgnoreCase) || - string.Equals(path, "/index.html", StringComparison.OrdinalIgnoreCase)) - { - // The request token has to be sent as a JavaScript-readable cookie, - // and Angular uses it by default. - var tokens = antiForgery.GetAndStoreTokens(context); - context.Response.Cookies.Append("XSRF-TOKEN", tokens.RequestToken, - new CookieOptions() { HttpOnly = false }); - } +app.UseServiceDefaults(); - return next(context); -}); - -// Seed Data -WebContextSeed.Seed(app, app.Environment, app.Services.GetRequiredService>()); - -var pathBase = app.Configuration["PATH_BASE"]; - -if (!string.IsNullOrEmpty(pathBase)) -{ - app.Services.GetRequiredService>().LogDebug("Using PATH_BASE '{PathBase}'", pathBase); - app.UsePathBase(pathBase); -} - -app.UseDefaultFiles(); -app.UseStaticFiles(); +app.UseFileServer(); // This will make the application to respond with the index.html and the rest of the assets present on the configured folder (at AddSpaStaticFiles() (wwwroot)) if (!app.Environment.IsDevelopment()) @@ -81,16 +24,12 @@ if (!app.Environment.IsDevelopment()) } app.UseRouting(); -app.MapDefaultControllerRoute(); -app.MapControllers(); -app.MapHealthChecks("/liveness", new HealthCheckOptions -{ - Predicate = r => r.Name.Contains("self") -}); -app.MapHealthChecks("/hc", new HealthCheckOptions() + +#pragma warning disable ASP0014 // Suggest using top level route registrations +app.UseEndpoints(routes => { - Predicate = _ => true, - ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse + // TODO: Change this route + routes.MapGet("/home/configuration", (IOptions options) => options.Value); }); // Handles all still unattended (by any other middleware) requests by returning the default page of the SPA (wwwroot/index.html). @@ -109,4 +48,7 @@ app.UseSpa(spa => } }); +// Seed Data +WebContextSeed.Seed(app, app.Environment, app.Logger); + await app.RunAsync(); diff --git a/src/Web/WebSPA/Properties/launchSettings.json b/src/Web/WebSPA/Properties/launchSettings.json index 30f70cbe8..7aa20e07c 100644 --- a/src/Web/WebSPA/Properties/launchSettings.json +++ b/src/Web/WebSPA/Properties/launchSettings.json @@ -1,24 +1,5 @@ { - "iisSettings": { - "windowsAuthentication": false, - "anonymousAuthentication": true, - "iisExpress": { - "applicationUrl": "http://localhost:58018/", - "sslPort": 0 - } - }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", - "environmentVariables": { - "ASPNETCORE_ENVIRONMENT": "Development" - } - }, - "Docker": { - "commandName": "Docker", - "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", - "publishAllPorts": true - }, "WebSPA": { "commandName": "Project", "launchBrowser": true, @@ -27,6 +8,11 @@ "ASPNETCORE_ENVIRONMENT": "Development" }, "applicationUrl": "http://localhost:5104" + }, + "Docker": { + "commandName": "Docker", + "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}", + "publishAllPorts": true } } } \ No newline at end of file diff --git a/src/Web/WebSPA/Server/Controllers/HomeController.cs b/src/Web/WebSPA/Server/Controllers/HomeController.cs deleted file mode 100644 index 0c115028f..000000000 --- a/src/Web/WebSPA/Server/Controllers/HomeController.cs +++ /dev/null @@ -1,18 +0,0 @@ -// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860 -namespace eShopConContainers.WebSPA.Server.Controllers; - -public class HomeController : Controller -{ - private readonly IWebHostEnvironment _env; - private readonly IOptionsSnapshot _settings; - - public HomeController(IWebHostEnvironment env, IOptionsSnapshot settings) - { - _env = env; - _settings = settings; - } - public IActionResult Configuration() - { - return Json(_settings.Value); - } -} diff --git a/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs b/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs index 89f559873..a9d5b0cc5 100644 --- a/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs +++ b/src/Web/WebSPA/Server/Infrastructure/WebContextSeed.cs @@ -4,7 +4,7 @@ using Microsoft.Extensions.Logging; public class WebContextSeed { - public static void Seed(IApplicationBuilder applicationBuilder, IWebHostEnvironment env, ILogger logger) + public static void Seed(IApplicationBuilder applicationBuilder, IWebHostEnvironment env, ILogger logger) { var settings = applicationBuilder .ApplicationServices.GetRequiredService>().Value; diff --git a/src/Web/WebSPA/WebSPA.csproj b/src/Web/WebSPA/WebSPA.csproj index 8a842a792..c5b0e461c 100644 --- a/src/Web/WebSPA/WebSPA.csproj +++ b/src/Web/WebSPA/WebSPA.csproj @@ -2,9 +2,9 @@ net7.0 + enable aspnetcorespa-c23d27a4-eb88-4b18-9b77-2a93f3b15119 false - true true wwwroot/dist/** $(DefaultItemExcludes);$(GeneratedItemPatterns) @@ -22,26 +22,20 @@ PreserveNewest - - PreserveNewest - PreserveNewest - - - - - - - + + + + diff --git a/src/Web/WebSPA/appsettings.json b/src/Web/WebSPA/appsettings.json index f02404452..838165ab5 100644 --- a/src/Web/WebSPA/appsettings.json +++ b/src/Web/WebSPA/appsettings.json @@ -1,24 +1,19 @@ { - "IdentityUrl": "http://host.docker.internal:5105", - "CallBackUrl": "http://host.docker.internal:5104/", - "BasketUrl" : "http://host.docker.internal:5103", - "PurchaseUrl": "http://host.docker.internal:5202", - "PurchaseUrlHC": "http://host.docker.internal:5202/hc", - "IdentityUrlHC": "http://host.docker.internal:5105/hc", - "SignalrHubUrl": "http://host.docker.internal:5112", - "UseCustomizationData": true, - "IsClusterEnv": "False", - "ActivateCampaignDetailFunction": false, "Logging": { - "Console": { - "IncludeScopes": false - }, "LogLevel": { - "Default": "Debug", - "System": "Information", - "Microsoft": "Information" + "Default": "Information", + "Microsoft.AspNetCore": "Warning" } }, + "IdentityUrl": "http://localhost:5223", + "CallBackUrl": "http://localhost:5331/", + "PurchaseUrl": "http://localhost:5229", + "PurchaseUrlHC": "http://localhost:5229/hc", + "IdentityUrlHC": "http://localhost:5223/hc", + "SignalrHubUrl": "http://localhost:5229", + "UseCustomizationData": true, + "IsClusterEnv": false, + "ActivateCampaignDetailFunction": false, "ApplicationInsights": { "InstrumentationKey": "" } diff --git a/src/Web/WebSPA/web.config b/src/Web/WebSPA/web.config deleted file mode 100644 index d0433a765..000000000 --- a/src/Web/WebSPA/web.config +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/src/docker-compose.override.yml b/src/docker-compose.override.yml index a08b080e5..4acec25f1 100644 --- a/src/docker-compose.override.yml +++ b/src/docker-compose.override.yml @@ -276,13 +276,13 @@ services: environment: - ASPNETCORE_ENVIRONMENT=Production - ASPNETCORE_URLS=http://0.0.0.0:80 - - Identity__Url=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105 - - PurchaseUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5202 + - IdentityUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105 + - PurchaseUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5121 - IdentityUrlHC=http://identity-api/hc + - SignalrHubUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5121 - UseCustomizationData=True - ApplicationInsights__InstrumentationKey=${INSTRUMENTATION_KEY} - OrchestratorType=${ORCHESTRATOR_TYPE} - - SignalrHubUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5202 ports: - "5104:80" diff --git a/src/docker-compose.prod.yml b/src/docker-compose.prod.yml index 0bc2cd1d2..5623d2e3e 100644 --- a/src/docker-compose.prod.yml +++ b/src/docker-compose.prod.yml @@ -111,7 +111,7 @@ services: environment: - ASPNETCORE_ENVIRONMENT=Development - ASPNETCORE_URLS=http://0.0.0.0:80 - - Identity__Url=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105 + - IdentityUrl=http://${ESHOP_EXTERNAL_DNS_NAME_OR_IP}:5105 - PurchaseUrl=http://${ESHOP_PROD_EXTERNAL_DNS_NAME_OR_IP}:5202 - CatalogUrlHC=http://catalog-api/hc - OrderingUrlHC=http://ordering-api/hc diff --git a/src/docker-compose.yml b/src/docker-compose.yml index 6a9a48310..827683472 100644 --- a/src/docker-compose.yml +++ b/src/docker-compose.yml @@ -138,7 +138,6 @@ services: NODE_IMAGE: ${NODE_IMAGE:-node:16-bullseye} depends_on: - webshoppingagg - - webshoppingapigw webmvc: image: ${REGISTRY:-eshop}/webmvc:${PLATFORM:-linux}-${TAG:-latest}