From cfbf06093202a868fe3c9e5fe26cd17e37e2e295 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 23 Jan 2021 14:14:54 -0600 Subject: refactor options --- src/slskd/Controllers/SessionController.cs | 13 +++++----- src/slskd/Startup.cs | 40 +++++++++++++++--------------- 2 files changed, 27 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/slskd/Controllers/SessionController.cs b/src/slskd/Controllers/SessionController.cs index d41a9295..82155511 100644 --- a/src/slskd/Controllers/SessionController.cs +++ b/src/slskd/Controllers/SessionController.cs @@ -10,6 +10,7 @@ using slskd.DTO; using Microsoft.Extensions.Options; using System.Linq; + using slskd; /// /// Session @@ -21,13 +22,13 @@ [Consumes("application/json")] public class SessionController : ControllerBase { - private IOptionsSnapshot SoulseekOptions { get; set; } - private IOptionsSnapshot AuthenticationOptions { get; set; } + private IOptionsSnapshot SoulseekOptions { get; set; } + private IOptionsSnapshot AuthenticationOptions { get; set; } private SymmetricSecurityKey JwtSigningKey { get; set; } public SessionController( - IOptionsSnapshot soulseekOptions, - IOptionsSnapshot authenticationOptions, + IOptionsSnapshot soulseekOptions, + IOptionsSnapshot authenticationOptions, SymmetricSecurityKey jwtSigningKey) { SoulseekOptions = soulseekOptions; @@ -46,7 +47,7 @@ [ProducesResponseType(typeof(bool), 200)] public IActionResult Enabled() { - return Ok(!InstanceOptions.DisableAuthentication); + return base.Ok((object)!Program.Options.NoAuth); } /// @@ -115,7 +116,7 @@ private JwtSecurityToken GetJwtSecurityToken(string username, Role role) { var issuedUtc = DateTime.UtcNow; - var expiresUtc = DateTime.UtcNow.AddMilliseconds(InstanceOptions.JwtTTL); + var expiresUtc = DateTime.UtcNow.AddMilliseconds((double)Program.Options.Web.Jwt.TTL); var claims = new List() { diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 1e28deed..83f2cb71 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -61,21 +61,21 @@ } public IConfiguration Configuration { get; } - public ConcurrentDictionary Loggers { get; } = new ConcurrentDictionary(); + public ConcurrentDictionary Loggers { get; } = new ConcurrentDictionary(); public void ConfigureServices(IServiceCollection services) { - services.AddOptions() + services.AddOptions() .Bind(Configuration.GetSection("soulseek")); - services.AddOptions() + services.AddOptions() .Bind(Configuration.GetSection("authentication")); services.AddCors(options => options.AddPolicy("AllowAll", builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader())); - if (!InstanceOptions.DisableAuthentication) + if (!Program.Options.NoAuth) { - var jwtSigningKey = new SymmetricSecurityKey(PBKDF2.GetKey(InstanceOptions.JwtKey)); + var jwtSigningKey = new SymmetricSecurityKey(PBKDF2.GetKey(Program.Options.Web.Jwt.Key)); services.AddSingleton(jwtSigningKey); @@ -137,7 +137,7 @@ } }); - if (InstanceOptions.EnablePrometheus) + if (Program.Options.Feature.Prometheus) { services.AddSystemMetrics(); } @@ -157,8 +157,8 @@ ITransferTracker tracker, IBrowseTracker browseTracker, IConversationTracker conversationTracker, - IOptionsMonitor soulseekOptions, - IOptionsMonitor authenticationOptions, + IOptionsMonitor soulseekOptions, + IOptionsMonitor authenticationOptions, IRoomTracker roomTracker) { var logger = Log.ForContext(); @@ -174,8 +174,8 @@ app.UseCors("AllowAll"); - app.UsePathBase(InstanceOptions.UrlBase); - logger.Information("Using base url {UrlBase}", InstanceOptions.UrlBase); + app.UsePathBase(Program.Options.Web.UrlBase); + logger.Information("Using base url {UrlBase}", Program.Options.Web.UrlBase); // remove any errant double forward slashes which may have been introduced // by a reverse proxy or having the base path removed @@ -193,27 +193,27 @@ FileServerOptions fileServerOptions = default; - if (!System.IO.Directory.Exists(InstanceOptions.ContentPath)) + if (!System.IO.Directory.Exists(Program.Options.Web.ContentPath)) { - logger.Warning($"Static content disabled; cannot find content path '{InstanceOptions.ContentPath}'"); + logger.Warning($"Static content disabled; cannot find content path '{Program.Options.Web.ContentPath}'"); } else { fileServerOptions = new FileServerOptions { - FileProvider = new PhysicalFileProvider(InstanceOptions.ContentPath), + FileProvider = new PhysicalFileProvider(Program.Options.Web.ContentPath), RequestPath = "", EnableDirectoryBrowsing = false, EnableDefaultFiles = true }; app.UseFileServer(fileServerOptions); - logger.Information("Serving static content from {ContentPath}", InstanceOptions.ContentPath); + logger.Information("Serving static content from {ContentPath}", Program.Options.Web.ContentPath); } app.UseSerilogRequestLogging(); - if (InstanceOptions.EnablePrometheus) + if (Program.Options.Feature.Prometheus) { app.UseHttpMetrics(); } @@ -226,13 +226,13 @@ { endpoints.MapControllers(); - if (InstanceOptions.EnablePrometheus) + if (Program.Options.Feature.Prometheus) { endpoints.MapMetrics(); } }); - if (InstanceOptions.EnableSwagger) + if (Program.Options.Feature.Swagger) { app.UseSwagger(); app.UseSwaggerUI(options => provider.ApiVersionDescriptions.ToList() @@ -254,7 +254,7 @@ // finally, hit the fileserver again. if the path was modified to return the index above, the index document will be returned // otherwise it will throw a final 404 back to the client. - if (System.IO.Directory.Exists(InstanceOptions.ContentPath)) + if (System.IO.Directory.Exists(Program.Options.Web.ContentPath)) { app.UseFileServer(fileServerOptions); } @@ -288,8 +288,8 @@ enqueueDownloadAction: (username, endpoint, filename) => EnqueueDownloadAction(username, endpoint, filename, tracker), searchResponseResolver: SearchResponseResolver); - var username = soulseekOptions.CurrentValue.Username ?? InstanceOptions.Username; - var password = soulseekOptions.CurrentValue.Password ?? InstanceOptions.Password; + var username = soulseekOptions.CurrentValue.Username; + var password = soulseekOptions.CurrentValue.Password; Client = new SoulseekClient(options: clientOptions); -- cgit v1.2.3