diff options
author | crupest <crupest@outlook.com> | 2020-02-21 11:59:58 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2020-02-21 11:59:58 +0800 |
commit | 6cf3c7891fe8a810b20bb799db9f3fb97414c4de (patch) | |
tree | a143ff8698660608d188428957a1ad56d620cca6 /Timeline/Startup.cs | |
parent | e6a153380c720e386137904498b78b5488868f13 (diff) | |
download | timeline-6cf3c7891fe8a810b20bb799db9f3fb97414c4de.tar.gz timeline-6cf3c7891fe8a810b20bb799db9f3fb97414c4de.tar.bz2 timeline-6cf3c7891fe8a810b20bb799db9f3fb97414c4de.zip |
Migrate to use sqlite.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r-- | Timeline/Startup.cs | 35 |
1 files changed, 12 insertions, 23 deletions
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<string?>("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<JwtConfig>(Configuration.GetSection(nameof(JwtConfig)));
+ services.Configure<JwtConfiguration>(Configuration.GetSection("Jwt"));
services.AddAuthentication(AuthenticationConstants.Scheme)
.AddScheme<MyAuthenticationOptions, MyAuthenticationHandler>(AuthenticationConstants.Scheme, AuthenticationConstants.DisplayName, o => { });
services.AddAuthorization();
@@ -94,27 +99,11 @@ namespace Timeline services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
- var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get<DatabaseConfig>();
-
- if (databaseConfig.UseDevelopment)
+ var dbConnectionString = $"Data Source={Path.Combine(workDir, "timeline.db")}";
+ services.AddDbContext<DatabaseContext>(options =>
{
- 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,
- mySqlOptions => mySqlOptions.ServerVersion(new ServerVersion(new Version(5, 7), ServerType.MySql)));
- });
- }
+ options.UseSqlite(dbConnectionString);
+ });
}
|