summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-16 14:30:07 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2020-05-16 14:30:07 -0400
commit5e6f1640dcb8e30a44f8ff07572874850b12cc2e (patch)
tree48f259c0d052b6d4e104a951681ae8df7e341e76 /src/SMAPI.Web
parenta2cfb71d898aca98d621f1b86dd5611337eea034 (diff)
downloadSMAPI-5e6f1640dcb8e30a44f8ff07572874850b12cc2e.tar.gz
SMAPI-5e6f1640dcb8e30a44f8ff07572874850b12cc2e.tar.bz2
SMAPI-5e6f1640dcb8e30a44f8ff07572874850b12cc2e.zip
simplify single-instance deployment and make MongoDB server optional
Diffstat (limited to 'src/SMAPI.Web')
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs2
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs89
-rw-r--r--src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs (renamed from src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs)15
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs2
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs54
-rw-r--r--src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs (renamed from src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs)30
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/MongoDbConfig.cs25
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/StorageConfig.cs18
-rw-r--r--src/SMAPI.Web/Framework/ConfigModels/StorageMode.cs15
-rw-r--r--src/SMAPI.Web/Startup.cs94
-rw-r--r--src/SMAPI.Web/appsettings.Development.json3
-rw-r--r--src/SMAPI.Web/appsettings.json3
12 files changed, 271 insertions, 79 deletions
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
index bcec8b36..08749f3b 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/IModCacheRepository.cs
@@ -4,7 +4,7 @@ using StardewModdingAPI.Web.Framework.ModRepositories;
namespace StardewModdingAPI.Web.Framework.Caching.Mods
{
- /// <summary>Encapsulates logic for accessing the mod data cache.</summary>
+ /// <summary>Manages cached mod data.</summary>
internal interface IModCacheRepository : ICacheRepository
{
/*********
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
new file mode 100644
index 00000000..9c5a217e
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMemoryRepository.cs
@@ -0,0 +1,89 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using StardewModdingAPI.Toolkit.Framework.UpdateData;
+using StardewModdingAPI.Web.Framework.ModRepositories;
+
+namespace StardewModdingAPI.Web.Framework.Caching.Mods
+{
+ /// <summary>Manages cached mod data in-memory.</summary>
+ internal class ModCacheMemoryRepository : BaseCacheRepository, IModCacheRepository
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The cached mod data indexed by <c>{site key}:{ID}</c>.</summary>
+ private readonly IDictionary<string, CachedMod> Mods = new Dictionary<string, CachedMod>(StringComparer.InvariantCultureIgnoreCase);
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get the cached mod data.</summary>
+ /// <param name="site">The mod site to search.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The fetched mod.</param>
+ /// <param name="markRequested">Whether to update the mod's 'last requested' date.</param>
+ public bool TryGetMod(ModRepositoryKey site, string id, out CachedMod mod, bool markRequested = true)
+ {
+ // get mod
+ if (!this.Mods.TryGetValue(this.GetKey(site, id), out mod))
+ return false;
+
+ // bump 'last requested'
+ if (markRequested)
+ {
+ mod.LastRequested = DateTimeOffset.UtcNow;
+ mod = this.SaveMod(mod);
+ }
+
+ return true;
+ }
+
+ /// <summary>Save data fetched for a mod.</summary>
+ /// <param name="site">The mod site on which the mod is found.</param>
+ /// <param name="id">The mod's unique ID within the <paramref name="site"/>.</param>
+ /// <param name="mod">The mod data.</param>
+ /// <param name="cachedMod">The stored mod record.</param>
+ public void SaveMod(ModRepositoryKey site, string id, ModInfoModel mod, out CachedMod cachedMod)
+ {
+ string key = this.GetKey(site, id);
+ cachedMod = this.SaveMod(new CachedMod(site, id, mod));
+ }
+
+ /// <summary>Delete data for mods which haven't been requested within a given time limit.</summary>
+ /// <param name="age">The minimum age for which to remove mods.</param>
+ public void RemoveStaleMods(TimeSpan age)
+ {
+ DateTimeOffset minDate = DateTimeOffset.UtcNow.Subtract(age);
+
+ string[] staleKeys = this.Mods
+ .Where(p => p.Value.LastRequested < minDate)
+ .Select(p => p.Key)
+ .ToArray();
+
+ foreach (string key in staleKeys)
+ this.Mods.Remove(key);
+ }
+
+ /// <summary>Save data fetched for a mod.</summary>
+ /// <param name="mod">The mod data.</param>
+ public CachedMod SaveMod(CachedMod mod)
+ {
+ string key = this.GetKey(mod.Site, mod.ID);
+ return this.Mods[key] = mod;
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// <summary>Get a cache key.</summary>
+ /// <param name="site">The mod site.</param>
+ /// <param name="id">The mod ID.</param>
+ public string GetKey(ModRepositoryKey site, string id)
+ {
+ return $"{site}:{id.Trim()}".ToLower();
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs
index 6ba1d73d..f105baab 100644
--- a/src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Mods/ModCacheMongoRepository.cs
@@ -5,8 +5,8 @@ using StardewModdingAPI.Web.Framework.ModRepositories;
namespace StardewModdingAPI.Web.Framework.Caching.Mods
{
- /// <summary>Encapsulates logic for accessing the mod data cache.</summary>
- internal class ModCacheRepository : BaseCacheRepository, IModCacheRepository
+ /// <summary>Manages cached mod data in MongoDB.</summary>
+ internal class ModCacheMongoRepository : BaseCacheRepository, IModCacheRepository
{
/*********
** Fields
@@ -20,7 +20,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
*********/
/// <summary>Construct an instance.</summary>
/// <param name="database">The authenticated MongoDB database.</param>
- public ModCacheRepository(IMongoDatabase database)
+ public ModCacheMongoRepository(IMongoDatabase database)
{
// get collections
this.Mods = database.GetCollection<CachedMod>("mods");
@@ -29,6 +29,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedMod>(Builders<CachedMod>.IndexKeys.Ascending(p => p.ID).Ascending(p => p.Site)));
}
+
/*********
** Public methods
*********/
@@ -75,10 +76,6 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
this.Mods.DeleteMany(p => p.LastRequested < minDate);
}
-
- /*********
- ** Private methods
- *********/
/// <summary>Save data fetched for a mod.</summary>
/// <param name="mod">The mod data.</param>
public CachedMod SaveMod(CachedMod mod)
@@ -94,6 +91,10 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
return mod;
}
+
+ /*********
+ ** Private methods
+ *********/
/// <summary>Normalize a mod ID for case-insensitive search.</summary>
/// <param name="id">The mod ID.</param>
public string NormalizeId(string id)
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
index b54c8a2f..02097f52 100644
--- a/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/IWikiCacheRepository.cs
@@ -5,7 +5,7 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
namespace StardewModdingAPI.Web.Framework.Caching.Wiki
{
- /// <summary>Encapsulates logic for accessing the wiki data cache.</summary>
+ /// <summary>Manages cached wiki data.</summary>
internal interface IWikiCacheRepository : ICacheRepository
{
/*********
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs
new file mode 100644
index 00000000..4621f5e3
--- /dev/null
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMemoryRepository.cs
@@ -0,0 +1,54 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Linq.Expressions;
+using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
+
+namespace StardewModdingAPI.Web.Framework.Caching.Wiki
+{
+ /// <summary>Manages cached wiki data in-memory.</summary>
+ internal class WikiCacheMemoryRepository : BaseCacheRepository, IWikiCacheRepository
+ {
+ /*********
+ ** Fields
+ *********/
+ /// <summary>The saved wiki metadata.</summary>
+ private CachedWikiMetadata Metadata;
+
+ /// <summary>The cached wiki data.</summary>
+ private CachedWikiMod[] Mods = new CachedWikiMod[0];
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get the cached wiki metadata.</summary>
+ /// <param name="metadata">The fetched metadata.</param>
+ public bool TryGetWikiMetadata(out CachedWikiMetadata metadata)
+ {
+ metadata = this.Metadata;
+ return metadata != null;
+ }
+
+ /// <summary>Get the cached wiki mods.</summary>
+ /// <param name="filter">A filter to apply, if any.</param>
+ public IEnumerable<CachedWikiMod> GetWikiMods(Expression<Func<CachedWikiMod, bool>> filter = null)
+ {
+ return filter != null
+ ? this.Mods.Where(filter.Compile())
+ : this.Mods.ToArray();
+ }
+
+ /// <summary>Save data fetched from the wiki compatibility list.</summary>
+ /// <param name="stableVersion">The current stable Stardew Valley version.</param>
+ /// <param name="betaVersion">The current beta Stardew Valley version.</param>
+ /// <param name="mods">The mod data.</param>
+ /// <param name="cachedMetadata">The stored metadata record.</param>
+ /// <param name="cachedMods">The stored mod records.</param>
+ public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable<WikiModEntry> mods, out CachedWikiMetadata cachedMetadata, out CachedWikiMod[] cachedMods)
+ {
+ this.Metadata = cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion);
+ this.Mods = cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray();
+ }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs
index 1ae9d38f..07e7c721 100644
--- a/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs
+++ b/src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheMongoRepository.cs
@@ -7,17 +7,17 @@ using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
namespace StardewModdingAPI.Web.Framework.Caching.Wiki
{
- /// <summary>Encapsulates logic for accessing the wiki data cache.</summary>
- internal class WikiCacheRepository : BaseCacheRepository, IWikiCacheRepository
+ /// <summary>Manages cached wiki data in MongoDB.</summary>
+ internal class WikiCacheMongoRepository : BaseCacheRepository, IWikiCacheRepository
{
/*********
** Fields
*********/
/// <summary>The collection for wiki metadata.</summary>
- private readonly IMongoCollection<CachedWikiMetadata> WikiMetadata;
+ private readonly IMongoCollection<CachedWikiMetadata> Metadata;
/// <summary>The collection for wiki mod data.</summary>
- private readonly IMongoCollection<CachedWikiMod> WikiMods;
+ private readonly IMongoCollection<CachedWikiMod> Mods;
/*********
@@ -25,21 +25,21 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
*********/
/// <summary>Construct an instance.</summary>
/// <param name="database">The authenticated MongoDB database.</param>
- public WikiCacheRepository(IMongoDatabase database)
+ public WikiCacheMongoRepository(IMongoDatabase database)
{
// get collections
- this.WikiMetadata = database.GetCollection<CachedWikiMetadata>("wiki-metadata");
- this.WikiMods = database.GetCollection<CachedWikiMod>("wiki-mods");
+ this.Metadata = database.GetCollection<CachedWikiMetadata>("wiki-metadata");
+ this.Mods = database.GetCollection<CachedWikiMod>("wiki-mods");
// add indexes if needed
- this.WikiMods.Indexes.CreateOne(new CreateIndexModel<CachedWikiMod>(Builders<CachedWikiMod>.IndexKeys.Ascending(p => p.ID)));
+ this.Mods.Indexes.CreateOne(new CreateIndexModel<CachedWikiMod>(Builders<CachedWikiMod>.IndexKeys.Ascending(p => p.ID)));
}
/// <summary>Get the cached wiki metadata.</summary>
/// <param name="metadata">The fetched metadata.</param>
public bool TryGetWikiMetadata(out CachedWikiMetadata metadata)
{
- metadata = this.WikiMetadata.Find("{}").FirstOrDefault();
+ metadata = this.Metadata.Find("{}").FirstOrDefault();
return metadata != null;
}
@@ -48,8 +48,8 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
public IEnumerable<CachedWikiMod> GetWikiMods(Expression<Func<CachedWikiMod, bool>> filter = null)
{
return filter != null
- ? this.WikiMods.Find(filter).ToList()
- : this.WikiMods.Find("{}").ToList();
+ ? this.Mods.Find(filter).ToList()
+ : this.Mods.Find("{}").ToList();
}
/// <summary>Save data fetched from the wiki compatibility list.</summary>
@@ -63,11 +63,11 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
cachedMetadata = new CachedWikiMetadata(stableVersion, betaVersion);
cachedMods = mods.Select(mod => new CachedWikiMod(mod)).ToArray();
- this.WikiMods.DeleteMany("{}");
- this.WikiMods.InsertMany(cachedMods);
+ this.Mods.DeleteMany("{}");
+ this.Mods.InsertMany(cachedMods);
- this.WikiMetadata.DeleteMany("{}");
- this.WikiMetadata.InsertOne(cachedMetadata);
+ this.Metadata.DeleteMany("{}");
+ this.Metadata.InsertOne(cachedMetadata);
}
}
}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/MongoDbConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/MongoDbConfig.cs
deleted file mode 100644
index c7b6cb00..00000000
--- a/src/SMAPI.Web/Framework/ConfigModels/MongoDbConfig.cs
+++ /dev/null
@@ -1,25 +0,0 @@
-namespace StardewModdingAPI.Web.Framework.ConfigModels
-{
- /// <summary>The config settings for mod compatibility list.</summary>
- internal class MongoDbConfig
- {
- /*********
- ** Accessors
- *********/
- /// <summary>The MongoDB connection string.</summary>
- public string ConnectionString { get; set; }
-
- /// <summary>The database name.</summary>
- public string Database { get; set; }
-
-
- /*********
- ** Public method
- *********/
- /// <summary>Get whether a MongoDB instance is configured.</summary>
- public bool IsConfigured()
- {
- return !string.IsNullOrWhiteSpace(this.ConnectionString);
- }
- }
-}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/StorageConfig.cs b/src/SMAPI.Web/Framework/ConfigModels/StorageConfig.cs
new file mode 100644
index 00000000..61cc4855
--- /dev/null
+++ b/src/SMAPI.Web/Framework/ConfigModels/StorageConfig.cs
@@ -0,0 +1,18 @@
+namespace StardewModdingAPI.Web.Framework.ConfigModels
+{
+ /// <summary>The config settings for cache storage.</summary>
+ internal class StorageConfig
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>The storage mechanism to use.</summary>
+ public StorageMode Mode { get; set; }
+
+ /// <summary>The connection string for the storage mechanism, if applicable.</summary>
+ public string ConnectionString { get; set; }
+
+ /// <summary>The database name for the storage mechanism, if applicable.</summary>
+ public string Database { get; set; }
+ }
+}
diff --git a/src/SMAPI.Web/Framework/ConfigModels/StorageMode.cs b/src/SMAPI.Web/Framework/ConfigModels/StorageMode.cs
new file mode 100644
index 00000000..4c2ea801
--- /dev/null
+++ b/src/SMAPI.Web/Framework/ConfigModels/StorageMode.cs
@@ -0,0 +1,15 @@
+namespace StardewModdingAPI.Web.Framework.ConfigModels
+{
+ /// <summary>Indicates a storage mechanism to use.</summary>
+ internal enum StorageMode
+ {
+ /// <summary>Store data in a hosted MongoDB instance.</summary>
+ Mongo,
+
+ /// <summary>Store data in an in-memory MongoDB instance. This is useful for testing MongoDB storage locally, but will likely fail when deployed since it needs permission to open a local port.</summary>
+ MongoInMemory,
+
+ /// <summary>Store data in-memory. This is suitable for local testing or single-instance servers, but will cause issues when distributed across multiple servers.</summary>
+ InMemory
+ }
+}
diff --git a/src/SMAPI.Web/Startup.cs b/src/SMAPI.Web/Startup.cs
index 35d22459..ddfae166 100644
--- a/src/SMAPI.Web/Startup.cs
+++ b/src/SMAPI.Web/Startup.cs
@@ -67,12 +67,13 @@ namespace StardewModdingAPI.Web
.Configure<BackgroundServicesConfig>(this.Configuration.GetSection("BackgroundServices"))
.Configure<ModCompatibilityListConfig>(this.Configuration.GetSection("ModCompatibilityList"))
.Configure<ModUpdateCheckConfig>(this.Configuration.GetSection("ModUpdateCheck"))
- .Configure<MongoDbConfig>(this.Configuration.GetSection("MongoDB"))
+ .Configure<StorageConfig>(this.Configuration.GetSection("Storage"))
.Configure<SiteConfig>(this.Configuration.GetSection("Site"))
.Configure<RouteOptions>(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
.AddLogging()
.AddMemoryCache();
- MongoDbConfig mongoConfig = this.Configuration.GetSection("MongoDB").Get<MongoDbConfig>();
+ StorageConfig storageConfig = this.Configuration.GetSection("Storage").Get<StorageConfig>();
+ StorageMode storageMode = storageConfig.Mode;
// init MVC
services
@@ -82,44 +83,66 @@ namespace StardewModdingAPI.Web
services
.AddRazorPages();
- // init MongoDB
- services.AddSingleton<MongoDbRunner>(_ => !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 =>
+ // init storage
+ switch (storageMode)
{
- // get connection string
- string connectionString = mongoConfig.IsConfigured()
- ? mongoConfig.ConnectionString
- : serv.GetRequiredService<MongoDbRunner>().ConnectionString;
-
- // get client
- BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
- 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>()));
+ case StorageMode.InMemory:
+ services.AddSingleton<IModCacheRepository>(new ModCacheMemoryRepository());
+ services.AddSingleton<IWikiCacheRepository>(new WikiCacheMemoryRepository());
+ break;
+
+ case StorageMode.Mongo:
+ case StorageMode.MongoInMemory:
+ {
+ // local MongoDB instance
+ services.AddSingleton<MongoDbRunner>(_ => storageMode == StorageMode.MongoInMemory
+ ? MongoDbRunner.Start()
+ : throw new NotSupportedException($"The in-memory MongoDB runner isn't available in storage mode {storageMode}.")
+ );
+
+ // MongoDB
+ services.AddSingleton<IMongoDatabase>(serv =>
+ {
+ BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
+ return new MongoClient(this.GetMongoDbConnectionString(serv, storageConfig))
+ .GetDatabase(storageConfig.Database);
+ });
+
+ // repositories
+ services.AddSingleton<IModCacheRepository>(serv => new ModCacheMongoRepository(serv.GetRequiredService<IMongoDatabase>()));
+ services.AddSingleton<IWikiCacheRepository>(serv => new WikiCacheMongoRepository(serv.GetRequiredService<IMongoDatabase>()));
+ }
+ break;
+
+ default:
+ throw new NotSupportedException($"Unhandled storage mode '{storageMode}'.");
+ }
// init Hangfire
services
- .AddHangfire(config =>
+ .AddHangfire((serv, config) =>
{
config
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings();
- if (mongoConfig.IsConfigured())
+ switch (storageMode)
{
- config.UseMongoStorage(MongoClientSettings.FromConnectionString(mongoConfig.ConnectionString), $"{mongoConfig.Database}-hangfire", new MongoStorageOptions
- {
- MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop),
- CheckConnection = false // error on startup takes down entire process
- });
+ case StorageMode.InMemory:
+ config.UseMemoryStorage();
+ break;
+
+ case StorageMode.MongoInMemory:
+ case StorageMode.Mongo:
+ string connectionString = this.GetMongoDbConnectionString(serv, storageConfig);
+ config.UseMongoStorage(MongoClientSettings.FromConnectionString(connectionString), $"{storageConfig.Database}-hangfire", new MongoStorageOptions
+ {
+ MigrationOptions = new MongoMigrationOptions(MongoMigrationStrategy.Drop),
+ CheckConnection = false // error on startup takes down entire process
+ });
+ break;
}
- else
- config.UseMemoryStorage();
});
// init background service
@@ -140,6 +163,7 @@ namespace StardewModdingAPI.Web
baseUrl: api.ChucklefishBaseUrl,
modPageUrlFormat: api.ChucklefishModPageUrlFormat
));
+
services.AddSingleton<ICurseForgeClient>(new CurseForgeClient(
userAgent: userAgent,
apiUrl: api.CurseForgeBaseUrl
@@ -229,6 +253,20 @@ namespace StardewModdingAPI.Web
settings.NullValueHandling = NullValueHandling.Ignore;
}
+ /// <summary>Get the MongoDB connection string for the given storage configuration.</summary>
+ /// <param name="services">The service provider.</param>
+ /// <param name="storageConfig">The storage configuration</param>
+ /// <exception cref="NotSupportedException">There's no MongoDB instance in the given storage mode.</exception>
+ private string GetMongoDbConnectionString(IServiceProvider services, StorageConfig storageConfig)
+ {
+ return storageConfig.Mode switch
+ {
+ StorageMode.Mongo => storageConfig.ConnectionString,
+ StorageMode.MongoInMemory => services.GetRequiredService<MongoDbRunner>().ConnectionString,
+ _ => throw new NotSupportedException($"There's no MongoDB instance in storage mode {storageConfig.Mode}.")
+ };
+ }
+
/// <summary>Get the redirect rules to apply.</summary>
private RewriteOptions GetRedirectRules()
{
diff --git a/src/SMAPI.Web/appsettings.Development.json b/src/SMAPI.Web/appsettings.Development.json
index 54460c46..41c00e79 100644
--- a/src/SMAPI.Web/appsettings.Development.json
+++ b/src/SMAPI.Web/appsettings.Development.json
@@ -17,7 +17,8 @@
"NexusApiKey": null
},
- "MongoDB": {
+ "Storage": {
+ "Mode": "MongoInMemory",
"ConnectionString": null,
"Database": "smapi-edge"
},
diff --git a/src/SMAPI.Web/appsettings.json b/src/SMAPI.Web/appsettings.json
index 9cd1efc8..b1d39a6f 100644
--- a/src/SMAPI.Web/appsettings.json
+++ b/src/SMAPI.Web/appsettings.json
@@ -49,7 +49,8 @@
"PastebinBaseUrl": "https://pastebin.com/"
},
- "MongoDB": {
+ "Storage": {
+ "Mode": "InMemory",
"ConnectionString": null,
"Database": "smapi"
},