diff options
author | crupest <crupest@outlook.com> | 2020-02-21 20:19:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-21 20:19:10 +0800 |
commit | 15371296c7b4b6a3a9a75b844ade5ccf00ec53bb (patch) | |
tree | bc3da9df67e73dff6578da9a0f4cd3982f4cd5f2 /Timeline/Startup.cs | |
parent | 32765bc2009d36cd3bc124e2a9bb769fc3ec9b4b (diff) | |
parent | 90e5eb7672e58745d1c41c28051375582d22e6ec (diff) | |
download | timeline-15371296c7b4b6a3a9a75b844ade5ccf00ec53bb.tar.gz timeline-15371296c7b4b6a3a9a75b844ade5ccf00ec53bb.tar.bz2 timeline-15371296c7b4b6a3a9a75b844ade5ccf00ec53bb.zip |
Merge pull request #59 from crupest/dev
Migrate to sqlite.
Diffstat (limited to 'Timeline/Startup.cs')
-rw-r--r-- | Timeline/Startup.cs | 36 |
1 files changed, 13 insertions, 23 deletions
diff --git a/Timeline/Startup.cs b/Timeline/Startup.cs index 2640a061..14bd14cc 100644 --- a/Timeline/Startup.cs +++ b/Timeline/Startup.cs @@ -8,8 +8,6 @@ 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.Text.Json.Serialization;
using Timeline.Auth;
@@ -37,6 +35,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 +55,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();
@@ -79,6 +83,8 @@ namespace Timeline });
}
+ services.AddScoped<IPathProvider, PathProvider>();
+
services.AddAutoMapper(GetType().Assembly);
services.AddTransient<IClock, Clock>();
@@ -94,27 +100,11 @@ namespace Timeline services.TryAddSingleton<IActionContextAccessor, ActionContextAccessor>();
- var databaseConfig = Configuration.GetSection(nameof(DatabaseConfig)).Get<DatabaseConfig>();
-
- if (databaseConfig.UseDevelopment)
- {
- 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>((services, options )=>
{
- 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)));
- });
- }
+ var pathProvider = services.GetRequiredService<IPathProvider>();
+ options.UseSqlite($"Data Source={pathProvider.GetDatabaseFilePath()}");
+ });
}
|