aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Startup.cs
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2020-01-05 23:19:56 +0800
committercrupest <crupest@outlook.com>2020-01-05 23:19:56 +0800
commitb629695c7c28fb89b91f2f7e09f7eea632c21aba (patch)
tree5c884cc6752341a4dde274f6fafd79ffcb4fb972 /Timeline/Startup.cs
parent24f5c10f5c96b07bc8c1280a1198024c0f00c101 (diff)
downloadtimeline-b629695c7c28fb89b91f2f7e09f7eea632c21aba.tar.gz
timeline-b629695c7c28fb89b91f2f7e09f7eea632c21aba.tar.bz2
timeline-b629695c7c28fb89b91f2f7e09f7eea632c21aba.zip
Use sqlite development database.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r--Timeline/Startup.cs51
1 files changed, 39 insertions, 12 deletions
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<string[]>();
- 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<string[]>();
+ 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<DatabaseConfig>();
- services.AddDbContext<DatabaseContext>(options =>
+ if (databaseConfig.UseDevelopment)
{
- options.UseMySql(databaseConfig.ConnectionString);
- });
+ services.AddDbContext<DatabaseContext, DevelopmentDatabaseContext>(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<DatabaseContext, ProductionDatabaseContext>(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();
}