diff options
author | crupest <crupest@outlook.com> | 2021-01-05 00:26:43 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2021-01-05 00:26:43 +0800 |
commit | 97e094c97dc9ed79cf7daa0a93568e1933015bdd (patch) | |
tree | 6eb662c6969c8f20c63af5220d8254f5fb754226 | |
parent | 79b597f5163619f8620e60c02e3ff33894ac29d4 (diff) | |
download | timeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.tar.gz timeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.tar.bz2 timeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.zip |
refactor: Refactor front end mode configuration.
-rw-r--r-- | BackEnd/Timeline.Tests/Helpers/TestApplication.cs | 2 | ||||
-rw-r--r-- | BackEnd/Timeline/Configs/ApplicationConfiguration.cs | 4 | ||||
-rw-r--r-- | BackEnd/Timeline/FrontEndMode.cs | 10 | ||||
-rw-r--r-- | BackEnd/Timeline/GlobalSuppressions.cs | 17 | ||||
-rw-r--r-- | BackEnd/Timeline/Properties/launchSettings.json | 8 | ||||
-rw-r--r-- | BackEnd/Timeline/Startup.cs | 49 |
6 files changed, 54 insertions, 36 deletions
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<string, string>
{
- [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<bool?>(ApplicationConfiguration.DisableFrontEndKey) ?? false;
- useMockFrontEnd = Configuration.GetValue<bool?>(ApplicationConfiguration.UseMockFrontEndKey) ?? false;
+ var frontEndModeString = Configuration.GetValue<string?>(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<bool?>(ApplicationConfiguration.UseProxyFrontEndKey) ?? false))
+ if (_frontEndMode == FrontEndMode.Proxy)
{
spa.UseProxyToSpaDevelopmentServer(new UriBuilder("http", "localhost", 3000).Uri);
}
|