From b629695c7c28fb89b91f2f7e09f7eea632c21aba Mon Sep 17 00:00:00 2001 From: crupest Date: Sun, 5 Jan 2020 23:19:56 +0800 Subject: Use sqlite development database. --- Timeline/Startup.cs | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'Timeline/Startup.cs') diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 672e5f15..5b6499a4 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -1,11 +1,12 @@ using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpOverrides; using Microsoft.AspNetCore.Localization; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; +using System; using System.Collections.Generic; using System.Globalization; using System.Text.Json.Serialization; @@ -55,15 +56,27 @@ namespace Timeline services.AddAuthorization(); - var corsConfig = Configuration.GetSection("Cors").Get(); - services.AddCors(setup => + if (Environment.IsDevelopment()) { - setup.AddDefaultPolicy(new CorsPolicyBuilder() - .AllowAnyHeader() - .AllowAnyMethod() - .WithOrigins(corsConfig).Build() - ); - }); + 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.AddLocalization(options => { @@ -81,10 +94,24 @@ namespace Timeline var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get(); - services.AddDbContext(options => + if (databaseConfig.UseDevelopment) { - options.UseMySql(databaseConfig.ConnectionString); - }); + services.AddDbContext(options => + { + if (databaseConfig.DevelopmentConnectionString == null) + throw new InvalidOperationException("DatabaseConfig.DevelopmentConnectionString is not set. Please set it as a sqlite connection string."); + options.UseSqlite(databaseConfig.DevelopmentConnectionString); + }); + } + else + { + services.AddDbContext(options => + { + if (databaseConfig.ConnectionString == null) + throw new InvalidOperationException("DatabaseConfig.ConnectionString is not set. Please set it as a mysql connection string."); + options.UseMySql(databaseConfig.ConnectionString); + }); + } services.AddMemoryCache(); } -- cgit v1.2.3