aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Startup.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-05 00:26:43 +0800
committercrupest <crupest@outlook.com>2021-01-05 00:26:43 +0800
commit97e094c97dc9ed79cf7daa0a93568e1933015bdd (patch)
tree6eb662c6969c8f20c63af5220d8254f5fb754226 /BackEnd/Timeline/Startup.cs
parent79b597f5163619f8620e60c02e3ff33894ac29d4 (diff)
downloadtimeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.tar.gz
timeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.tar.bz2
timeline-97e094c97dc9ed79cf7daa0a93568e1933015bdd.zip
refactor: Refactor front end mode configuration.
Diffstat (limited to 'BackEnd/Timeline/Startup.cs')
-rw-r--r--BackEnd/Timeline/Startup.cs49
1 files changed, 28 insertions, 21 deletions
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);
}