From 6cf3c7891fe8a810b20bb799db9f3fb97414c4de Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 21 Feb 2020 11:59:58 +0800 Subject: Migrate to use sqlite. --- Timeline/Startup.cs | 35 ++++++++++++----------------------- 1 file changed, 12 insertions(+), 23 deletions(-) (limited to 'Timeline/Startup.cs') diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 2640a061..f305c39d 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -8,9 +8,8 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.Hosting; -using Pomelo.EntityFrameworkCore.MySql.Infrastructure; -using Pomelo.EntityFrameworkCore.MySql.Storage; using System; +using System.IO; using System.Text.Json.Serialization; using Timeline.Auth; using Timeline.Configs; @@ -37,6 +36,12 @@ namespace Timeline // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { + var workDir = Configuration.GetValue("WorkDir"); + if (workDir == null) + { + throw new InvalidOperationException("Please set a work directory first."); + } + services.AddControllers(setup => { setup.InputFormatters.Add(new StringInputFormatter()); @@ -51,7 +56,7 @@ namespace Timeline options.InvalidModelStateResponseFactory = InvalidModelResponseFactory.Factory; }); - services.Configure(Configuration.GetSection(nameof(JwtConfig))); + services.Configure(Configuration.GetSection("Jwt")); services.AddAuthentication(AuthenticationConstants.Scheme) .AddScheme(AuthenticationConstants.Scheme, AuthenticationConstants.DisplayName, o => { }); services.AddAuthorization(); @@ -94,27 +99,11 @@ namespace Timeline services.TryAddSingleton(); - var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get(); - - if (databaseConfig.UseDevelopment) + var dbConnectionString = $"Data Source={Path.Combine(workDir, "timeline.db")}"; + services.AddDbContext(options => { - 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, - mySqlOptions => mySqlOptions.ServerVersion(new ServerVersion(new Version(5, 7), ServerType.MySql))); - }); - } + options.UseSqlite(dbConnectionString); + }); } -- cgit v1.2.3