aboutsummaryrefslogtreecommitdiffstats
path: root/src/slskd/Startup.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/slskd/Startup.cs')
-rw-r--r--src/slskd/Startup.cs59
1 files changed, 40 insertions, 19 deletions
diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs
index 635a6890..af0fd329 100644
--- a/src/slskd/Startup.cs
+++ b/src/slskd/Startup.cs
@@ -42,6 +42,7 @@ namespace slskd
using slskd.Peer;
using slskd.Search;
using slskd.Transfer;
+ using slskd.Validation;
/// <summary>
/// ASP.NET Startup.
@@ -51,29 +52,29 @@ namespace slskd
/// <summary>
/// Initializes a new instance of the <see cref="Startup"/> class.
/// </summary>
- /// <param name="configuration">The application configuration.</param>
+ /// <param name="configuration">The application configuration root.</param>
public Startup(IConfiguration configuration)
{
Configuration = configuration;
- Options = new Options();
- Configuration.GetSection(Program.AppName).Bind(Options, (o) =>
+ OptionsAtStartup = new OptionsAtStartup();
+ Configuration.GetSection(Program.AppName).Bind(OptionsAtStartup, (o) =>
{
o.BindNonPublicProperties = true;
});
- UrlBase = Options.Web.UrlBase;
+ UrlBase = OptionsAtStartup.Web.UrlBase;
UrlBase = UrlBase.StartsWith("/") ? UrlBase : "/" + UrlBase;
- ContentPath = Path.GetFullPath(Options.Web.ContentPath);
+ ContentPath = Path.GetFullPath(OptionsAtStartup.Web.ContentPath);
- JwtSigningKey = new SymmetricSecurityKey(Pbkdf2.GetKey(Options.Web.Authentication.Jwt.Key));
+ JwtSigningKey = new SymmetricSecurityKey(Pbkdf2.GetKey(OptionsAtStartup.Web.Authentication.Jwt.Key));
}
private IConfiguration Configuration { get; }
private string ContentPath { get; set; }
private SymmetricSecurityKey JwtSigningKey { get; set; }
- private Options Options { get; }
+ private OptionsAtStartup OptionsAtStartup { get; }
private string UrlBase { get; set; }
/// <summary>
@@ -84,14 +85,34 @@ namespace slskd
{
var logger = Log.ForContext<Startup>();
+ // add the instance of OptionsAtStartup to DI as they were at startup. use when Options might change, but
+ // the values at startup are to be used.
+ services.AddSingleton(OptionsAtStartup);
+
+ // add IOptionsMonitor and IOptionsSnapshot to DI. use when the current Options are to be used.
services.AddOptions<Options>()
- .Bind(Configuration.GetSection(Program.AppName), o => { o.BindNonPublicProperties = true; });
+ .Bind(Configuration.GetSection(Program.AppName), o => { o.BindNonPublicProperties = true; })
+ .Validate(options =>
+ {
+ if (!options.TryValidate(out var result))
+ {
+ logger.Warning("Options (re)configuration rejected.");
+ logger.Warning(result.GetResultView());
+ return false;
+ }
+
+ return true;
+ });
+
+ // add IStateMonitor to DI, to track application state in an observable way. similar to IOptionsMonitor,
+ // but state is managed by the application itself. Usage is roughly the same.
+ services.AddSingleton<IStateMonitor, StateMonitor>();
services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader()));
services.AddSingleton(JwtSigningKey);
- if (!Options.Web.Authentication.Disable)
+ if (!OptionsAtStartup.Web.Authentication.Disable)
{
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
@@ -139,7 +160,7 @@ namespace slskd
options.SubstituteApiVersionInUrl = true;
});
- if (Options.Feature.Swagger)
+ if (OptionsAtStartup.Feature.Swagger)
{
services.AddSwaggerGen(options =>
{
@@ -163,19 +184,19 @@ namespace slskd
});
}
- if (Options.Feature.Prometheus)
+ if (OptionsAtStartup.Feature.Prometheus)
{
services.AddSystemMetrics();
}
services.AddDbContextFactory<SearchDbContext>(options =>
{
- options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "search.db")}");
+ options.UseSqlite($"Data Source={Path.Combine(OptionsAtStartup.Directories.App, "search.db")}");
});
services.AddDbContextFactory<PeerDbContext>(options =>
{
- options.UseSqlite($"Data Source={Path.Combine(Options.Directories.App, "peer.db")}");
+ options.UseSqlite($"Data Source={Path.Combine(OptionsAtStartup.Directories.App, "peer.db")}");
});
services.AddHttpClient();
@@ -185,14 +206,14 @@ namespace slskd
services.AddSingleton<IConversationTracker, ConversationTracker>();
services.AddSingleton<IRoomTracker, RoomTracker>(_ => new RoomTracker(messageLimit: 250));
services.AddSingleton<ISharedFileCache>(_ =>
- new SharedFileCache(Options.Directories.Shared, 3600000));
+ new SharedFileCache(OptionsAtStartup.Directories.Shared, 3600000));
services.AddSingleton<ISearchService, SearchService>();
services.AddSingleton<IPeerService, PeerService>();
services.AddSingleton<IManagementService, ManagementService>();
services.AddSingleton<IFTPClientFactory, FTPClientFactory>();
- services.AddTransient<IFTPService, FTPService>();
+ services.AddSingleton<IFTPService, FTPService>();
services.AddSingleton<IPushbulletService, PushbulletService>();
@@ -213,7 +234,7 @@ namespace slskd
app.UseCors("AllowAll");
- if (Options.Web.Https.Force)
+ if (OptionsAtStartup.Web.Https.Force)
{
app.UseHttpsRedirection();
app.UseHsts();
@@ -258,7 +279,7 @@ namespace slskd
logger.Information("Serving static content from {ContentPath}", ContentPath);
}
- if (Options.Feature.Prometheus)
+ if (OptionsAtStartup.Feature.Prometheus)
{
app.UseHttpMetrics();
logger.Information("Publishing Prometheus metrics to /metrics");
@@ -275,13 +296,13 @@ namespace slskd
endpoints.MapControllers();
endpoints.MapHealthChecks("/health");
- if (Options.Feature.Prometheus)
+ if (OptionsAtStartup.Feature.Prometheus)
{
endpoints.MapMetrics();
}
});
- if (Options.Feature.Swagger)
+ if (OptionsAtStartup.Feature.Swagger)
{
app.UseSwagger();
app.UseSwaggerUI(options => apiVersionDescriptionProvider.ApiVersionDescriptions.ToList()