summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Startup.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-12-22 00:44:13 -0500
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2019-12-22 00:44:13 -0500
commit082f285bc7ce156ad0750bb48d46ed65a2e4aedb (patch)
tree7dc63890caf6eee699590a67013d634e213fabe3 /src/SMAPI.Web/Startup.cs
parentc1b15fb3725661ebfd8e03cec08343ae49e5d6da (diff)
downloadSMAPI-082f285bc7ce156ad0750bb48d46ed65a2e4aedb.tar.gz
SMAPI-082f285bc7ce156ad0750bb48d46ed65a2e4aedb.tar.bz2
SMAPI-082f285bc7ce156ad0750bb48d46ed65a2e4aedb.zip
streamline local environments, update technical docs & privacy page
Diffstat (limited to 'src/SMAPI.Web/Startup.cs')
-rw-r--r--src/SMAPI.Web/Startup.cs25
1 files changed, 22 insertions, 3 deletions
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 07ee0c9e..338cd2d5 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -1,5 +1,7 @@
+using System;
using System.Collections.Generic;
using Hangfire;
+using Hangfire.MemoryStorage;
using Hangfire.Mongo;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
@@ -8,6 +10,7 @@ using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
+using Mongo2Go;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using Newtonsoft.Json;
@@ -89,10 +92,20 @@ namespace StardewModdingAPI.Web
}
// init MongoDB
+ services.AddSingleton<MongoDbRunner>(serv => !mongoConfig.IsConfigured()
+ ? MongoDbRunner.Start()
+ : throw new InvalidOperationException("The MongoDB connection is configured, so the local development version should not be used.")
+ );
services.AddSingleton<IMongoDatabase>(serv =>
{
+ // get connection string
+ string connectionString = mongoConfig.IsConfigured()
+ ? mongoConfig.GetConnectionString()
+ : serv.GetRequiredService<MongoDbRunner>().ConnectionString;
+
+ // get client
BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
- return new MongoClient(mongoConfig.GetConnectionString()).GetDatabase(mongoConfig.Database);
+ return new MongoClient(connectionString).GetDatabase(mongoConfig.Database);
});
services.AddSingleton<IModCacheRepository>(serv => new ModCacheRepository(serv.GetRequiredService<IMongoDatabase>()));
services.AddSingleton<IWikiCacheRepository>(serv => new WikiCacheRepository(serv.GetRequiredService<IMongoDatabase>()));
@@ -104,12 +117,18 @@ namespace StardewModdingAPI.Web
config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
- .UseRecommendedSerializerSettings()
- .UseMongoStorage(mongoConfig.GetConnectionString(), $"{mongoConfig.Database}-hangfire", new MongoStorageOptions
+ .UseRecommendedSerializerSettings();
+
+ if (mongoConfig.IsConfigured())
+ {
+ config.UseMongoStorage(mongoConfig.GetConnectionString(), $"{mongoConfig.Database}-hangfire", new MongoStorageOptions
{
MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop),
CheckConnection = false // error on startup takes down entire process
});
+ }
+ else
+ config.UseMemoryStorage();
});
// init API clients