diff --git a/docs/release-notes.md b/docs/release-notes.md
index a660888c..abe07dd9 100644
--- a/docs/release-notes.md
+++ b/docs/release-notes.md
@@ -8,12 +8,16 @@
* For the web UI:
* Updated web framework to improve site performance and reliability.
* Added GitHub licenses to mod compatibility list.
+ * Internal changes to improve performance and reliability.
* For modders:
* Added `Multiplayer.PeerConnected` event.
* Simplified paranoid warnings in the log and reduced their log level.
* Fixed asset propagation for Gil's portraits.
+* For SMAPI developers:
+ * When deploying web services to a single-instance app, the MongoDB server can now be replaced with in-memory storage.
+
## 3.5
Released 27 April 2020 for Stardew Valley 1.4.1 or later.
diff --git a/docs/technical/web.md b/docs/technical/web.md
index 67e86c8b..ef591aee 100644
--- a/docs/technical/web.md
+++ b/docs/technical/web.md
@@ -340,9 +340,20 @@ short url | → | target page
A local environment lets you run a complete copy of the web project (including cache database) on
your machine, with no external dependencies aside from the actual mod sites.
-1. Enter the Nexus credentials in `appsettings.Development.json` . You can leave the other
- credentials empty to default to fetching data anonymously, and storing data in-memory and
- on disk.
+1. Edit `appsettings.Development.json` and set these options:
+
+ property name | description
+ ------------- | -----------
+ `NexusApiKey` | [Your Nexus API key](https://www.nexusmods.com/users/myaccount?tab=api#personal_key).
+
+ Optional settings:
+
+ property name | description
+ --------------------------- | -----------
+ `AzureBlobConnectionString` | The connection string for the Azure Blob storage account. Defaults to using the system's temporary file folder if not specified.
+ `GitHubUsername`
`GitHubPassword` | The GitHub credentials with which to query GitHub release info. Defaults to anonymous requests if not specified.
+ `Storage` | How to storage cached wiki/mod data. `InMemory` is recommended in most cases, or `MongoInMemory` to test the MongoDB storage code. See [production environment](#production-environment) for more info on `Mongo`.
+
2. Launch `SMAPI.Web` from Visual Studio to run a local version of the site.
### Production environment
@@ -355,19 +366,15 @@ accordingly.
Initial setup:
-1. Launch an empty MongoDB server (e.g. using [MongoDB Atlas](https://www.mongodb.com/cloud/atlas))
- for mod data.
-2. Create an Azure Blob storage account for uploaded files.
-3. Create an Azure App Services environment running the latest .NET Core on Linux or Windows.
-4. Add these application settings in the new App Services environment:
+1. Create an Azure Blob storage account for uploaded files.
+2. Create an Azure App Services environment running the latest .NET Core on Linux or Windows.
+3. Add these application settings in the new App Services environment:
property name | description
------------------------------- | -----------------
`ApiClients.AzureBlobConnectionString` | The connection string for the Azure Blob storage account created in step 2.
`ApiClients.GitHubUsername`
`ApiClients.GitHubPassword` | The login credentials for the GitHub account with which to fetch release info. If these are omitted, GitHub will impose much stricter rate limits.
`ApiClients:NexusApiKey` | The [Nexus API authentication key](https://github.com/Pathoschild/FluentNexus#init-a-client).
- `MongoDB:ConnectionString` | The connection string for the MongoDB instance.
- `MongoDB:Database` | The MongoDB database name (e.g. `smapi` in production or `smapi-edge` in testing environments).
Optional settings:
@@ -378,6 +385,23 @@ Initial setup:
`Site:BetaBlurb` | If `Site:BetaEnabled` is true and there's a beta version of SMAPI in its GitHub releases, this is shown on the beta download button as explanatory subtext.
`Site:SupporterList` | A list of Patreon supports to credit on the download page.
+To enable distributed servers:
+
+1. Launch an empty MongoDB server (e.g. using [MongoDB Atlas](https://www.mongodb.com/cloud/atlas))
+ for mod data.
+2. Add these application settings in the App Services environment:
+
+ property name | description
+ ------------------------------- | -----------------
+ `Storage:Mode` | Set to `Mongo`.
+ `Storage:ConnectionString` | Set to the connection string for the MongoDB instance.
+
+ Optional settings:
+
+ property name | description
+ ------------------------------- | -----------------
+ `Storage:Database` | Set to the MongoDB database name (defaults to `smapi`).
+
To deploy updates:
1. [Deploy the web project from Visual Studio](https://docs.microsoft.com/en-us/visualstudio/deployment/quickstart-deploy-to-azure).
2. If the MongoDB schema changed, delete the MongoDB database. (It'll be recreated automatically.)
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
{
- /// Encapsulates logic for accessing the mod data cache.
+ /// Manages cached mod data.
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
+{
+ /// Manages cached mod data in-memory.
+ internal class ModCacheMemoryRepository : BaseCacheRepository, IModCacheRepository
+ {
+ /*********
+ ** Fields
+ *********/
+ /// The cached mod data indexed by {site key}:{ID}.
+ private readonly IDictionary Mods = new Dictionary(StringComparer.InvariantCultureIgnoreCase);
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// Get the cached mod data.
+ /// The mod site to search.
+ /// The mod's unique ID within the .
+ /// The fetched mod.
+ /// Whether to update the mod's 'last requested' date.
+ 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;
+ }
+
+ /// Save data fetched for a mod.
+ /// The mod site on which the mod is found.
+ /// The mod's unique ID within the .
+ /// The mod data.
+ /// The stored mod record.
+ 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));
+ }
+
+ /// Delete data for mods which haven't been requested within a given time limit.
+ /// The minimum age for which to remove mods.
+ 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);
+ }
+
+ /// Save data fetched for a mod.
+ /// The mod data.
+ public CachedMod SaveMod(CachedMod mod)
+ {
+ string key = this.GetKey(mod.Site, mod.ID);
+ return this.Mods[key] = mod;
+ }
+
+
+ /*********
+ ** Private methods
+ *********/
+ /// Get a cache key.
+ /// The mod site.
+ /// The mod ID.
+ 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
similarity index 94%
rename from src/SMAPI.Web/Framework/Caching/Mods/ModCacheRepository.cs
rename to 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
{
- /// Encapsulates logic for accessing the mod data cache.
- internal class ModCacheRepository : BaseCacheRepository, IModCacheRepository
+ /// Manages cached mod data in MongoDB.
+ internal class ModCacheMongoRepository : BaseCacheRepository, IModCacheRepository
{
/*********
** Fields
@@ -20,7 +20,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
*********/
/// Construct an instance.
/// The authenticated MongoDB database.
- public ModCacheRepository(IMongoDatabase database)
+ public ModCacheMongoRepository(IMongoDatabase database)
{
// get collections
this.Mods = database.GetCollection("mods");
@@ -29,6 +29,7 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
this.Mods.Indexes.CreateOne(new CreateIndexModel(Builders.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
- *********/
/// Save data fetched for a mod.
/// The mod data.
public CachedMod SaveMod(CachedMod mod)
@@ -94,6 +91,10 @@ namespace StardewModdingAPI.Web.Framework.Caching.Mods
return mod;
}
+
+ /*********
+ ** Private methods
+ *********/
/// Normalize a mod ID for case-insensitive search.
/// The mod ID.
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
{
- /// Encapsulates logic for accessing the wiki data cache.
+ /// Manages cached wiki data.
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
+{
+ /// Manages cached wiki data in-memory.
+ internal class WikiCacheMemoryRepository : BaseCacheRepository, IWikiCacheRepository
+ {
+ /*********
+ ** Fields
+ *********/
+ /// The saved wiki metadata.
+ private CachedWikiMetadata Metadata;
+
+ /// The cached wiki data.
+ private CachedWikiMod[] Mods = new CachedWikiMod[0];
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// Get the cached wiki metadata.
+ /// The fetched metadata.
+ public bool TryGetWikiMetadata(out CachedWikiMetadata metadata)
+ {
+ metadata = this.Metadata;
+ return metadata != null;
+ }
+
+ /// Get the cached wiki mods.
+ /// A filter to apply, if any.
+ public IEnumerable GetWikiMods(Expression> filter = null)
+ {
+ return filter != null
+ ? this.Mods.Where(filter.Compile())
+ : this.Mods.ToArray();
+ }
+
+ /// Save data fetched from the wiki compatibility list.
+ /// The current stable Stardew Valley version.
+ /// The current beta Stardew Valley version.
+ /// The mod data.
+ /// The stored metadata record.
+ /// The stored mod records.
+ public void SaveWikiData(string stableVersion, string betaVersion, IEnumerable 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
similarity index 66%
rename from src/SMAPI.Web/Framework/Caching/Wiki/WikiCacheRepository.cs
rename to 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
{
- /// Encapsulates logic for accessing the wiki data cache.
- internal class WikiCacheRepository : BaseCacheRepository, IWikiCacheRepository
+ /// Manages cached wiki data in MongoDB.
+ internal class WikiCacheMongoRepository : BaseCacheRepository, IWikiCacheRepository
{
/*********
** Fields
*********/
/// The collection for wiki metadata.
- private readonly IMongoCollection WikiMetadata;
+ private readonly IMongoCollection Metadata;
/// The collection for wiki mod data.
- private readonly IMongoCollection WikiMods;
+ private readonly IMongoCollection Mods;
/*********
@@ -25,21 +25,21 @@ namespace StardewModdingAPI.Web.Framework.Caching.Wiki
*********/
/// Construct an instance.
/// The authenticated MongoDB database.
- public WikiCacheRepository(IMongoDatabase database)
+ public WikiCacheMongoRepository(IMongoDatabase database)
{
// get collections
- this.WikiMetadata = database.GetCollection("wiki-metadata");
- this.WikiMods = database.GetCollection("wiki-mods");
+ this.Metadata = database.GetCollection("wiki-metadata");
+ this.Mods = database.GetCollection("wiki-mods");
// add indexes if needed
- this.WikiMods.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(p => p.ID)));
+ this.Mods.Indexes.CreateOne(new CreateIndexModel(Builders.IndexKeys.Ascending(p => p.ID)));
}
/// Get the cached wiki metadata.
/// The fetched metadata.
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 GetWikiMods(Expression> filter = null)
{
return filter != null
- ? this.WikiMods.Find(filter).ToList()
- : this.WikiMods.Find("{}").ToList();
+ ? this.Mods.Find(filter).ToList()
+ : this.Mods.Find("{}").ToList();
}
/// Save data fetched from the wiki compatibility list.
@@ -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
-{
- /// The config settings for mod compatibility list.
- internal class MongoDbConfig
- {
- /*********
- ** Accessors
- *********/
- /// The MongoDB connection string.
- public string ConnectionString { get; set; }
-
- /// The database name.
- public string Database { get; set; }
-
-
- /*********
- ** Public method
- *********/
- /// Get whether a MongoDB instance is configured.
- 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
+{
+ /// The config settings for cache storage.
+ internal class StorageConfig
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// The storage mechanism to use.
+ public StorageMode Mode { get; set; }
+
+ /// The connection string for the storage mechanism, if applicable.
+ public string ConnectionString { get; set; }
+
+ /// The database name for the storage mechanism, if applicable.
+ 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
+{
+ /// Indicates a storage mechanism to use.
+ internal enum StorageMode
+ {
+ /// Store data in a hosted MongoDB instance.
+ Mongo,
+
+ /// 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.
+ MongoInMemory,
+
+ /// Store data in-memory. This is suitable for local testing or single-instance servers, but will cause issues when distributed across multiple servers.
+ 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(this.Configuration.GetSection("BackgroundServices"))
.Configure(this.Configuration.GetSection("ModCompatibilityList"))
.Configure(this.Configuration.GetSection("ModUpdateCheck"))
- .Configure(this.Configuration.GetSection("MongoDB"))
+ .Configure(this.Configuration.GetSection("Storage"))
.Configure(this.Configuration.GetSection("Site"))
.Configure(options => options.ConstraintMap.Add("semanticVersion", typeof(VersionConstraint)))
.AddLogging()
.AddMemoryCache();
- MongoDbConfig mongoConfig = this.Configuration.GetSection("MongoDB").Get();
+ StorageConfig storageConfig = this.Configuration.GetSection("Storage").Get();
+ StorageMode storageMode = storageConfig.Mode;
// init MVC
services
@@ -82,44 +83,66 @@ namespace StardewModdingAPI.Web
services
.AddRazorPages();
- // init MongoDB
- services.AddSingleton(_ => !mongoConfig.IsConfigured()
- ? MongoDbRunner.Start()
- : throw new InvalidOperationException("The MongoDB connection is configured, so the local development version should not be used.")
- );
- services.AddSingleton(serv =>
+ // init storage
+ switch (storageMode)
{
- // get connection string
- string connectionString = mongoConfig.IsConfigured()
- ? mongoConfig.ConnectionString
- : serv.GetRequiredService().ConnectionString;
+ case StorageMode.InMemory:
+ services.AddSingleton(new ModCacheMemoryRepository());
+ services.AddSingleton(new WikiCacheMemoryRepository());
+ break;
- // get client
- BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
- return new MongoClient(connectionString).GetDatabase(mongoConfig.Database);
- });
- services.AddSingleton(serv => new ModCacheRepository(serv.GetRequiredService()));
- services.AddSingleton(serv => new WikiCacheRepository(serv.GetRequiredService()));
+ case StorageMode.Mongo:
+ case StorageMode.MongoInMemory:
+ {
+ // local MongoDB instance
+ services.AddSingleton(_ => storageMode == StorageMode.MongoInMemory
+ ? MongoDbRunner.Start()
+ : throw new NotSupportedException($"The in-memory MongoDB runner isn't available in storage mode {storageMode}.")
+ );
+
+ // MongoDB
+ services.AddSingleton(serv =>
+ {
+ BsonSerializer.RegisterSerializer(new UtcDateTimeOffsetSerializer());
+ return new MongoClient(this.GetMongoDbConnectionString(serv, storageConfig))
+ .GetDatabase(storageConfig.Database);
+ });
+
+ // repositories
+ services.AddSingleton(serv => new ModCacheMongoRepository(serv.GetRequiredService()));
+ services.AddSingleton(serv => new WikiCacheMongoRepository(serv.GetRequiredService()));
+ }
+ 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(new CurseForgeClient(
userAgent: userAgent,
apiUrl: api.CurseForgeBaseUrl
@@ -229,6 +253,20 @@ namespace StardewModdingAPI.Web
settings.NullValueHandling = NullValueHandling.Ignore;
}
+ /// Get the MongoDB connection string for the given storage configuration.
+ /// The service provider.
+ /// The storage configuration
+ /// There's no MongoDB instance in the given storage mode.
+ private string GetMongoDbConnectionString(IServiceProvider services, StorageConfig storageConfig)
+ {
+ return storageConfig.Mode switch
+ {
+ StorageMode.Mongo => storageConfig.ConnectionString,
+ StorageMode.MongoInMemory => services.GetRequiredService().ConnectionString,
+ _ => throw new NotSupportedException($"There's no MongoDB instance in storage mode {storageConfig.Mode}.")
+ };
+ }
+
/// Get the redirect rules to apply.
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"
},