From 9470631c67c4740982ff2d8a16cbbb86fdd34609 Mon Sep 17 00:00:00 2001 From: crupest Date: Tue, 5 Jan 2021 00:26:43 +0800 Subject: refactor: Refactor front end mode configuration. --- BackEnd/Timeline.Tests/Helpers/TestApplication.cs | 2 +- .../Timeline/Configs/ApplicationConfiguration.cs | 4 +- BackEnd/Timeline/FrontEndMode.cs | 10 +++++ BackEnd/Timeline/GlobalSuppressions.cs | 17 ++++---- BackEnd/Timeline/Properties/launchSettings.json | 8 ++-- BackEnd/Timeline/Startup.cs | 49 ++++++++++++---------- 6 files changed, 54 insertions(+), 36 deletions(-) create mode 100644 BackEnd/Timeline/FrontEndMode.cs diff --git a/BackEnd/Timeline.Tests/Helpers/TestApplication.cs b/BackEnd/Timeline.Tests/Helpers/TestApplication.cs index da8dea46..22f5f006 100644 --- a/BackEnd/Timeline.Tests/Helpers/TestApplication.cs +++ b/BackEnd/Timeline.Tests/Helpers/TestApplication.cs @@ -38,7 +38,7 @@ namespace Timeline.Tests.Helpers { config.AddInMemoryCollection(new Dictionary { - [ApplicationConfiguration.UseMockFrontEndKey] = "true", + [ApplicationConfiguration.FrontEndKey] = "Mock", ["WorkDir"] = WorkDir }); }) diff --git a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs index df281adb..25ee7ab2 100644 --- a/BackEnd/Timeline/Configs/ApplicationConfiguration.cs +++ b/BackEnd/Timeline/Configs/ApplicationConfiguration.cs @@ -6,8 +6,6 @@ public const string DefaultWorkDir = "/timeline"; public const string DatabaseFileName = "timeline.db"; public const string DatabaseBackupDirectoryName = "backup"; - public const string DisableFrontEndKey = "DisableFrontEnd"; - public const string UseMockFrontEndKey = "UseMockFrontEnd"; - public const string UseProxyFrontEndKey = "UseProxyFrontEnd"; + public const string FrontEndKey = "FrontEnd"; } } diff --git a/BackEnd/Timeline/FrontEndMode.cs b/BackEnd/Timeline/FrontEndMode.cs new file mode 100644 index 00000000..63503292 --- /dev/null +++ b/BackEnd/Timeline/FrontEndMode.cs @@ -0,0 +1,10 @@ +namespace Timeline +{ + enum FrontEndMode + { + Disable, + Mock, + Proxy, + Normal + } +} diff --git a/BackEnd/Timeline/GlobalSuppressions.cs b/BackEnd/Timeline/GlobalSuppressions.cs index 155ed9ff..178ae09c 100644 --- a/BackEnd/Timeline/GlobalSuppressions.cs +++ b/BackEnd/Timeline/GlobalSuppressions.cs @@ -3,10 +3,13 @@ // Project-level suppressions either have no target or are given // a specific target and scoped to a namespace, type, member, etc. -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "This is not a UI application.")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "This is not bad.")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Generated error response identifiers.", Scope = "type", Target = "Timeline.Models.Http.ErrorResponse")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Naming", "CA1724:Type names should not match namespaces", Justification = "Generated error response identifiers.", Scope = "type", Target = "Timeline.Models.Http.ErrorResponse")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "Generated error response.", Scope = "type", Target = "Timeline.Models.Http.ErrorResponse")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1056:Uri properties should not be strings", Justification = "That's unnecessary.")] -[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Redundant")] +using System.Diagnostics.CodeAnalysis; + +[assembly: SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "This is not a UI application.")] +[assembly: SuppressMessage("Design", "CA1034:Nested types should not be visible", Justification = "This is not bad.")] +[assembly: SuppressMessage("Naming", "CA1707:Identifiers should not contain underscores", Justification = "Generated error response identifiers.", Scope = "type", Target = "~T:Timeline.Models.Http.ErrorResponse")] +[assembly: SuppressMessage("Naming", "CA1724:Type names should not match namespaces", Justification = "Generated error response identifiers.", Scope = "type", Target = "~T:Timeline.Models.Http.ErrorResponse")] +[assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "Generated error response.", Scope = "type", Target = "~T:Timeline.Models.Http.ErrorResponse")] +[assembly: SuppressMessage("Design", "CA1056:Uri properties should not be strings", Justification = "That's unnecessary.")] +[assembly: SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "Redundant")] +[assembly: SuppressMessage("Globalization", "CA1303:Do not pass literals as localized parameters", Justification = "No localization demand.")] diff --git a/BackEnd/Timeline/Properties/launchSettings.json b/BackEnd/Timeline/Properties/launchSettings.json index 6d58040a..a31485ed 100644 --- a/BackEnd/Timeline/Properties/launchSettings.json +++ b/BackEnd/Timeline/Properties/launchSettings.json @@ -1,19 +1,19 @@ { "profiles": { - "Development": { + "Dev": { "commandName": "Project", "applicationUrl": "http://0.0.0.0:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "ASPNETCORE_USEPROXYFRONTEND": "true", + "ASPNETCORE_FRONTEND": "Proxy", "ASPNETCORE_WORKDIR": "D:\\timeline-development" } }, - "Development-Mock": { + "Dev-Mock": { "commandName": "Project", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development", - "ASPNETCORE_USEMOCKFRONTEND": "true", + "ASPNETCORE_FRONTEND": "Mock", "ASPNETCORE_WORKDIR": "D:\\timeline-development" } }, diff --git a/BackEnd/Timeline/Startup.cs b/BackEnd/Timeline/Startup.cs index 66c708ac..a706cf99 100644 --- a/BackEnd/Timeline/Startup.cs +++ b/BackEnd/Timeline/Startup.cs @@ -8,7 +8,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; -using Microsoft.Extensions.Hosting; using NSwag; using NSwag.Generation.Processors.Security; using System; @@ -29,16 +28,27 @@ namespace Timeline { public class Startup { - private readonly bool disableFrontEnd; - private readonly bool useMockFrontEnd; + private readonly FrontEndMode _frontEndMode; public Startup(IConfiguration configuration, IWebHostEnvironment environment) { Environment = environment; Configuration = configuration; - disableFrontEnd = Configuration.GetValue(ApplicationConfiguration.DisableFrontEndKey) ?? false; - useMockFrontEnd = Configuration.GetValue(ApplicationConfiguration.UseMockFrontEndKey) ?? false; + var frontEndModeString = Configuration.GetValue(ApplicationConfiguration.FrontEndKey); + + if (frontEndModeString is null) + { + _frontEndMode = FrontEndMode.Normal; + } + else + { + if (!Enum.TryParse(frontEndModeString, true, out _frontEndMode)) + { + _frontEndMode = FrontEndMode.Normal; + Console.WriteLine("Unknown FrontEnd configuaration value '{0}', fallback to normal.", frontEndModeString); + } + } } public IWebHostEnvironment Environment { get; } @@ -130,23 +140,20 @@ namespace Timeline document.OperationProcessors.Add(new ByteDataRequestOperationProcessor()); }); - if (!disableFrontEnd) + if (_frontEndMode == FrontEndMode.Mock) { - if (useMockFrontEnd) + services.AddSpaStaticFiles(config => { - services.AddSpaStaticFiles(config => - { - config.RootPath = "MockClientApp"; - }); + config.RootPath = "MockClientApp"; + }); - } - else if (!Environment.IsDevelopment()) // In development, we don't want to serve dist. Or it will take precedence than front end dev server. + } + else if (_frontEndMode == FrontEndMode.Normal) + { + services.AddSpaStaticFiles(config => { - services.AddSpaStaticFiles(config => - { - config.RootPath = "ClientApp"; - }); - } + config.RootPath = "ClientApp"; + }); } } @@ -156,7 +163,7 @@ namespace Timeline { app.UseRouting(); - if (!disableFrontEnd && (useMockFrontEnd || !Environment.IsDevelopment())) + if (_frontEndMode == FrontEndMode.Mock || _frontEndMode == FrontEndMode.Normal) { app.UseSpaStaticFiles(new StaticFileOptions { @@ -177,11 +184,11 @@ namespace Timeline UnknownEndpointMiddleware.Attach(app); - if (!disableFrontEnd) + if (_frontEndMode != FrontEndMode.Disable) { app.UseSpa(spa => { - if (!useMockFrontEnd && (Configuration.GetValue(ApplicationConfiguration.UseProxyFrontEndKey) ?? false)) + if (_frontEndMode == FrontEndMode.Proxy) { spa.UseProxyToSpaDevelopmentServer(new UriBuilder("http", "localhost", 3000).Uri); } -- cgit v1.2.3