diff --git a/application/account-management/Api/Api.csproj b/application/account-management/Api/Api.csproj index 9c07756d8..a21838838 100644 --- a/application/account-management/Api/Api.csproj +++ b/application/account-management/Api/Api.csproj @@ -31,11 +31,6 @@ - - diff --git a/application/account-management/Api/Program.cs b/application/account-management/Api/Program.cs index d2de6b5ca..f43b443a6 100644 --- a/application/account-management/Api/Program.cs +++ b/application/account-management/Api/Program.cs @@ -18,7 +18,7 @@ // Add common configuration for all APIs like Swagger, HSTS, DeveloperExceptionPage, and run EF database migrations. app.AddApiCoreConfiguration(); -app.UseWebAppMiddleware("WebApp"); +app.UseWebAppMiddleware(); app.MapTenantEndpoints(); app.MapUserEndpoints(); diff --git a/application/shared-kernel/ApiCore/Middleware/WebAppMiddleware.cs b/application/shared-kernel/ApiCore/Middleware/WebAppMiddleware.cs index b2050a9df..a761a9937 100644 --- a/application/shared-kernel/ApiCore/Middleware/WebAppMiddleware.cs +++ b/application/shared-kernel/ApiCore/Middleware/WebAppMiddleware.cs @@ -25,15 +25,12 @@ public class WebAppMiddleware private readonly Dictionary _runtimeEnvironment; private string? _htmlTemplate; - public WebAppMiddleware( - RequestDelegate next, - Dictionary runtimeEnvironment, - string htmlTemplatePath - ) + public WebAppMiddleware(RequestDelegate next, Dictionary runtimeEnvironment, + string htmlTemplatePath) { + _next = next; _runtimeEnvironment = runtimeEnvironment; _htmlTemplatePath = htmlTemplatePath; - _next = next; VerifyRuntimeEnvironment(runtimeEnvironment); @@ -47,14 +44,9 @@ private void VerifyRuntimeEnvironment(Dictionary environmentVari { foreach (var key in environmentVariables.Keys) { - if (key.StartsWith(PublicKeyPrefix) || _publicAllowedKeys.Contains(key)) - { - continue; - } + if (key.StartsWith(PublicKeyPrefix) || _publicAllowedKeys.Contains(key)) continue; - throw new SecurityException( - $"Environment variable '{key}' is not allowed to be public." - ); + throw new SecurityException($"Environment variable '{key}' is not allowed to be public."); } } @@ -95,8 +87,7 @@ private StringValues GetContentSecurityPolicy() return string.Join( " ", - contentSecurityPolicies.Select( - policy => $"{policy.Key} {string.Join(" ", policy.Value)};" + contentSecurityPolicies.Select(policy => $"{policy.Key} {string.Join(" ", policy.Value)};" ) ); } @@ -117,19 +108,11 @@ public async Task InvokeAsync(HttpContext context) public static class WebAppMiddlewareExtensions { - private const string WebAppDistRootName = "dist"; - [UsedImplicitly] - public static IApplicationBuilder UseWebAppMiddleware( - this IApplicationBuilder builder, - string webAppProjectName, - Dictionary? publicEnvironmentVariables = null - ) + public static IApplicationBuilder UseWebAppMiddleware(this IApplicationBuilder builder, + string webAppProjectName = "WebApp", Dictionary? publicEnvironmentVariables = null) { - if (Environment.GetEnvironmentVariable("SWAGGER_GENERATOR") == "true") - { - return builder; - } + if (Environment.GetEnvironmentVariable("SWAGGER_GENERATOR") == "true") return builder; var publicUrl = GetEnvironmentVariableOrThrow(WebAppMiddleware.PublicUrlKey); var cdnUrl = GetEnvironmentVariableOrThrow(WebAppMiddleware.CdnUrlKey); @@ -150,7 +133,7 @@ public static IApplicationBuilder UseWebAppMiddleware( } } - var buildRootPath = GetWebAppDistRoot(webAppProjectName); + var buildRootPath = GetWebAppDistRoot(webAppProjectName, "dist"); var templateFilePath = Path.Combine(buildRootPath, "index.html"); if (!File.Exists(templateFilePath)) @@ -159,31 +142,27 @@ public static IApplicationBuilder UseWebAppMiddleware( } return builder - .UseStaticFiles( - new StaticFileOptions {FileProvider = new PhysicalFileProvider(buildRootPath)} - ) + .UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(buildRootPath) }) .UseMiddleware(runtimeEnvironmentVariables, templateFilePath); } private static string GetEnvironmentVariableOrThrow(string variableName) { return Environment.GetEnvironmentVariable(variableName) - ?? throw new InvalidOperationException( - $"Required environment variable '{variableName}' is not set." - ); + ?? throw new InvalidOperationException($"Required environment variable '{variableName}' is not set."); } - private static string GetWebAppDistRoot(string webAppProjectName) + private static string GetWebAppDistRoot(string webAppProjectName, string webAppDistRootName) { var assemblyPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)!; var directoryInfo = new DirectoryInfo(assemblyPath); - while (directoryInfo != null && !directoryInfo.GetDirectories(webAppProjectName).Any() && - !Path.Exists(Path.Join(directoryInfo.FullName, webAppProjectName, WebAppDistRootName))) + while (directoryInfo is not null && !directoryInfo.GetDirectories(webAppProjectName).Any() && + !Path.Exists(Path.Join(directoryInfo.FullName, webAppProjectName, webAppDistRootName))) { directoryInfo = directoryInfo.Parent; } - return Path.Join(directoryInfo!.FullName, webAppProjectName, WebAppDistRootName); + return Path.Join(directoryInfo!.FullName, webAppProjectName, webAppDistRootName); } } \ No newline at end of file