From 232a19d7dfe0e3847b3a9a9a9be83485ffb9031c Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 30 May 2020 16:23:25 +0800 Subject: Merge front end to this repo. But I need to wait for aspnet core support for custom port and package manager for dev server. --- Timeline/Startup.cs | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) (limited to 'Timeline/Startup.cs') diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index f5220446..8da09686 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Mvc.Infrastructure; +using Microsoft.AspNetCore.SpaServices.ReactDevelopmentServer; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; @@ -16,6 +17,7 @@ using Timeline.Entities; using Timeline.Formatters; using Timeline.Helpers; using Timeline.Models.Converters; +using Timeline.Routes; using Timeline.Services; namespace Timeline @@ -44,6 +46,7 @@ namespace Timeline services.AddControllers(setup => { setup.InputFormatters.Add(new StringInputFormatter()); + setup.UseApiRoutePrefix("api"); }) .AddJsonOptions(options => { @@ -61,29 +64,6 @@ namespace Timeline .AddScheme(AuthenticationConstants.Scheme, AuthenticationConstants.DisplayName, o => { }); services.AddAuthorization(); - - if (Environment.IsDevelopment()) - { - services.AddCors(setup => - { - setup.AddDefaultPolicy(builder => - { - builder.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin(); - }); - }); - } - else - { - var corsConfig = Configuration.GetSection("Cors").Get(); - services.AddCors(setup => - { - setup.AddDefaultPolicy(builder => - { - builder.AllowAnyHeader().AllowAnyMethod().WithOrigins(corsConfig); - }); - }); - } - services.AddScoped(); services.AddAutoMapper(GetType().Assembly); @@ -113,6 +93,11 @@ namespace Timeline var pathProvider = services.GetRequiredService(); options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}"); }); + + services.AddSpaStaticFiles(config => + { + config.RootPath = "ClientApp/dist"; + }); } @@ -126,7 +111,7 @@ namespace Timeline app.UseRouting(); - app.UseCors(); + app.UseSpaStaticFiles(); app.UseAuthentication(); app.UseAuthorization(); @@ -135,6 +120,18 @@ namespace Timeline { endpoints.MapControllers(); }); + + app.UseSpa(spa => + { + spa.Options.SourcePath = "ClientApp"; + + if (Environment.IsDevelopment()) + { + // TODO! I'll waiting for aspnetcore to support custom package manager and port. + // It is already in master branch code but not published. + spa.UseReactDevelopmentServer(npmScript: "start"); + } + }); } } } -- cgit v1.2.3