diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-02-02 21:28:25 -0600 |
|---|---|---|
| committer | JP Dillingham <jpdillingham@gmail.com> | 2021-02-02 21:28:25 -0600 |
| commit | 1ecd35a3879bb5e8afd15f3e7ea22993c78f823d (patch) | |
| tree | 7a72375529e7a0a316f45bd3221dc4c92f9d028a /src | |
| parent | b35efd3792064e49b34823fb6b1bf4e3f7cd86a1 (diff) | |
| download | slskd-1ecd35a3879bb5e8afd15f3e7ea22993c78f823d.tar.gz | |
move config to Configuration namespace
Diffstat (limited to 'src')
7 files changed, 405 insertions, 5 deletions
diff --git a/src/slskd/Common/Configuration/CommandLineArguments.cs b/src/slskd/Common/Configuration/CommandLineArguments.cs index 133fd334..491965e7 100644 --- a/src/slskd/Common/Configuration/CommandLineArguments.cs +++ b/src/slskd/Common/Configuration/CommandLineArguments.cs @@ -1,4 +1,4 @@ -namespace slskd +namespace slskd.Configuration { using System; using System.Collections; diff --git a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs index c65e316b..fb479fd4 100644 --- a/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs +++ b/src/slskd/Common/Configuration/CommandLineConfigurationSource.cs @@ -1,4 +1,4 @@ -namespace slskd +namespace slskd.Configuration { using Microsoft.Extensions.Configuration; using System; diff --git a/src/slskd/Common/Configuration/ConfigurationExtensions.cs b/src/slskd/Common/Configuration/ConfigurationExtensions.cs new file mode 100644 index 00000000..77b59700 --- /dev/null +++ b/src/slskd/Common/Configuration/ConfigurationExtensions.cs @@ -0,0 +1,14 @@ +namespace slskd.Configuration +{ + public static class ConfigurationExtensions + { + public static CommandLineArgument ToCommandLineArgument(this Option option) + => new(option.ShortName, option.LongName, option.Type, option.Key, option.Description); + + public static EnvironmentVariable ToEnvironmentVariable(this Option option) + => new(option.EnvironmentVariable, option.Type, option.Key, option.Description); + + public static DefaultValue ToDefaultValue(this Option option) + => new(option.Key, option.Type, option.Default); + } +} diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 57fa2712..accc76c9 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -1,4 +1,4 @@ -namespace slskd +namespace slskd.Configuration { using Microsoft.Extensions.Configuration; using System; diff --git a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs index 2ace942b..a18faebc 100644 --- a/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs +++ b/src/slskd/Common/Configuration/EnvironmentVariableConfigurationSource.cs @@ -1,4 +1,4 @@ -namespace slskd +namespace slskd.Configuration { using Microsoft.Extensions.Configuration; using System; diff --git a/src/slskd/Common/Configuration/Options.cs b/src/slskd/Common/Configuration/Options.cs new file mode 100644 index 00000000..f10bd5a3 --- /dev/null +++ b/src/slskd/Common/Configuration/Options.cs @@ -0,0 +1,386 @@ +namespace slskd.Configuration +{ + using slskd.Validation; + using Soulseek.Diagnostics; + using System; + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.Diagnostics; + + public record Option(char ShortName, string LongName, string EnvironmentVariable, string Key, Type Type, object Default = null, string Description = null); + + public class Options + { + public static Options Defaults { get; } = new Options(); + + public bool Debug { get; private set; } = Debugger.IsAttached; + public bool NoLogo { get; private set; } = false; + public string InstanceName { get; private set; } = "default"; + [Validate] + public DirectoriesOptions Directories { get; private set; } = new DirectoriesOptions(); + [Validate] + public WebOptions Web { get; private set; } = new WebOptions(); + [Validate] + public LoggerOptions Logger { get; private set; } = new LoggerOptions(); + [Validate] + public FeatureOptions Feature { get; private set; } = new FeatureOptions(); + [Validate] + public SoulseekOptions Soulseek { get; private set; } = new SoulseekOptions(); + + public class DirectoriesOptions + { + [Required] + public string Shared { get; private set; } + [Required] + public string Downloads { get; private set; } + } + + public class FeatureOptions + { + public bool Prometheus { get; private set; } = false; + public bool Swagger { get; private set; } = false; + } + + public class LoggerOptions + { + public string Loki { get; private set; } = null; + } + + public class SoulseekOptions + { + [Required] + public string Password { get; private set; } + [Required] + public string Username { get; private set; } + [Range(1024, 65535)] + public int? ListenPort { get; private set; } = 50000; + public DiagnosticLevel DiagnosticLevel { get; private set; } = DiagnosticLevel.Info; + [Validate] + public DistributedNetworkOptions DistributedNetwork { get; private set; } = new DistributedNetworkOptions(); + [Validate] + public ConnectionOptions Connection { get; private set; } = new ConnectionOptions(); + + public class ConnectionOptions + { + [Validate] + public TimeoutOptions Timeout { get; private set; } = new TimeoutOptions(); + [Validate] + public BufferOptions Buffer { get; private set; } = new BufferOptions(); + + public class BufferOptions + { + [Range(1024, int.MaxValue)] + public int Read { get; private set; } = 16384; + [Range(1024, int.MaxValue)] + public int Write { get; private set; } = 16384; + } + + public class TimeoutOptions + { + [Range(1000, int.MaxValue)] + public int Connect { get; private set; } = 5000; + [Range(1000, int.MaxValue)] + public int Inactivity { get; private set; } = 15000; + } + } + + public class DistributedNetworkOptions + { + public bool Disabled { get; private set; } = false; + [Range(1, int.MaxValue)] + public int ChildLimit { get; private set; } = 25; + } + } + + public class WebOptions + { + [Range(1, 65535)] + public int Port { get; private set; } = 5000; + [Validate] + public HttpsOptions Https { get; private set; } = new HttpsOptions(); + public string UrlBase { get; private set; } = "/"; + [Required(ErrorMessage = "A content root is required")] + public string ContentPath { get; private set; } = "wwwroot"; + [Validate] + public AuthenticationOptions Authentication { get; private set; } = new AuthenticationOptions(); + + public class AuthenticationOptions + { + public bool Disable { get; private set; } = false; + [Required] + public string Username { get; private set; } = "slskd"; + [Required] + public string Password { get; private set; } = "slskd"; + [Validate] + public JwtOptions Jwt { get; private set; } = new JwtOptions(); + + public class JwtOptions + { + [Required] + public string Key { get; private set; } = Guid.NewGuid().ToString(); + [Range(3600, int.MaxValue)] + public int Ttl { get; private set; } = 604800000; + } + } + + public class HttpsOptions + { + [Range(1, 65535)] + public int Port { get; private set; } = 5001; + public bool Force { get; private set; } = false; + } + } + + public static IEnumerable<Option> Map => new Option[] + { + new( + ShortName: 'v', + LongName: "version", + EnvironmentVariable: null, + Key: null, + Type: typeof(bool), + Default: false, + Description: "display version information"), + new( + ShortName: 'h', + LongName: "help", + EnvironmentVariable: null, + Key: null, + Type: typeof(bool), + Default: false, + Description: "display command line usage"), + new( + ShortName: 'e', + LongName: "envars", + EnvironmentVariable: null, + Key: null, + Type: typeof(bool), + Default: false, + Description: "display environment variables"), + new( + ShortName: 'd', + LongName: "debug", + EnvironmentVariable: "DEBUG", + Key: "slskd:debug", + Type: typeof(bool), + Default: Defaults.Debug, + Description: "run in debug mode"), + new( + ShortName: 'n', + LongName: "no-logo", + EnvironmentVariable: "NO_LOGO", + Key: "slskd:nologo", + Type: typeof(bool), + Default: Defaults.NoLogo, + Description: "suppress logo on startup"), + new( + ShortName: 'i', + LongName: "instance-name", + EnvironmentVariable: "INSTANCE_NAME", + Key: "slskd:instancename", + Type: typeof(string), + Default: Defaults.InstanceName, + Description: "optional; a unique name for this instance"), + new( + ShortName: 'o', + LongName: "downloads", + EnvironmentVariable: "DOWNLOADS_DIR", + Key: "slskd:directories:downloads", + Type: typeof(string), + Default: Defaults.Directories.Downloads, + Description: "the path where downloaded files are saved"), + new( + ShortName: 's', + LongName: "shared", + EnvironmentVariable: "SHARED_DIR", + Key: "slskd:directories:shared", + Type: typeof(string), + Default: Defaults.Directories.Shared, + Description: "the path to shared files"), + new( + ShortName: 'l', + LongName: "http-port", + EnvironmentVariable: "HTTP_PORT", + Key: "slskd:web:port", + Type: typeof(int), + Default: Defaults.Web.Port, + Description: "HTTP listen port for web server"), + new( + ShortName: 'L', + LongName: "https-port", + EnvironmentVariable: "HTTPS_PORT", + Key: "slskd:web:https:port", + Type: typeof(int), + Default: Defaults.Web.Https.Port, + Description: "HTTPS listen port for web server"), + new( + ShortName: 'f', + LongName: "force-https", + EnvironmentVariable: "HTTPS_FORCE", + Key: "slskd:web:https:force", + Type: typeof(bool), + Default: Defaults.Web.Https.Force, + Description: "redirect HTTP to HTTPS"), + new( + ShortName: default, + LongName: "url-base", + EnvironmentVariable: "URL_BASE", + Key: "slskd:web:urlbase", + Type: typeof(string), + Default: Defaults.Web.UrlBase, + Description: "base url for web requests"), + new( + ShortName: default, + LongName: "content-path", + EnvironmentVariable: "CONTENT_PATH", + Key: "slskd:web:contentpath", + Type: typeof(string), + Default: Defaults.Web.ContentPath, + Description: "path to static web content"), + new( + ShortName: 'x', + LongName: "no-auth", + EnvironmentVariable: "NO_AUTH", + Key: "slskd:web:authentication:disable", + Type: typeof(bool), + Default: Defaults.Web.Authentication.Disable, + Description: "disable authentication for web requests"), + new( + ShortName: 'u', + LongName: "username", + EnvironmentVariable: "USERNAME", + Key: "slskd:web:authentication:username", + Type: typeof(string), + Default: Defaults.Web.Authentication.Username, + Description: "the username for the web UI"), + new( + ShortName: 'p', + LongName: "password", + EnvironmentVariable: "PASSWORD", + Key: "slskd:web:authentication:password", + Type: typeof(string), + Default: Defaults.Web.Authentication.Password, + Description: "the password for web UI"), + new( + ShortName: default, + LongName: "jwt-key", + EnvironmentVariable: "JWT_KEY", + Key: "slskd:web:authentication:jwt:key", + Type: typeof(string), + Default: Defaults.Web.Authentication.Jwt.Key, + Description: "the key with which to sign JWTs"), + new( + ShortName: default, + LongName: "jwt-ttl", + EnvironmentVariable: "JWT_TTL", + Key: "slskd:web:authentication:jwt:ttl", + Type: typeof(int), + Default: Defaults.Web.Authentication.Jwt.Ttl, + Description: "the TTL for JWTs"), + new( + ShortName: default, + LongName: "prometheus", + EnvironmentVariable: "PROMETHEUS", + Key: "slskd:feature:prometheus", + Type: typeof(bool), + Default: Defaults.Feature.Prometheus, + Description: "enable collection and publishing of prometheus metrics"), + new( + ShortName: default, + LongName: "swagger", + EnvironmentVariable: "SWAGGER", + Key: "slskd:feature:swagger", + Type: typeof(bool), + Default: Defaults.Feature.Swagger, + Description: "enable swagger documentation and UI"), + new( + ShortName: default, + LongName: "loki", + EnvironmentVariable: "LOKI", + Key: "slskd:logger:loki", + Type: typeof(string), + Default: Defaults.Logger.Loki, + Description: "optional; the url to a Grafana Loki instance to which to log"), + new( + ShortName: default, + LongName: "slsk-username", + EnvironmentVariable: "SLSK_USERNAME", + Key: "slskd:soulseek:username", + Type: typeof(string), + Default: Defaults.Soulseek.Username, + Description: "the username for the Soulseek network"), + new( + ShortName: default, + LongName: "slsk-password", + EnvironmentVariable: "SLSK_PASSWORD", + Key: "slskd:soulseek:password", + Type: typeof(string), + Default: Defaults.Soulseek.Password, + Description: "the password for the Soulseek network"), + new( + ShortName: default, + LongName: "slsk-listen-port", + EnvironmentVariable: "SLSK_LISTEN_PORT", + Key: "slskd:soulseek:listenport", + Type: typeof(int), + Default: Defaults.Soulseek.ListenPort, + Description: "the port on which to listen for incoming connections"), + new( + ShortName: default, + LongName: "slsk-diag-level", + EnvironmentVariable: "SLSK_DIAG_LEVEL", + Key: "slskd:soulseek:diagnosticlevel", + Type: typeof(DiagnosticLevel), + Default: Defaults.Soulseek.DiagnosticLevel, + Description: "the minimum diagnostic level (None, Warning, Info, Debug)"), + new( + ShortName: default, + LongName: "slsk-no-dnet", + EnvironmentVariable: "SLSK_NO_DNET", + Key: "slskd:soulseek:distributednetwork:disabled", + Type: typeof(bool), + Default: Defaults.Soulseek.DistributedNetwork.Disabled, + Description: "disables the distributed network"), + new( + ShortName: default, + LongName: "slsk-dnet-children", + EnvironmentVariable: "SLSK_DNET_CHILDREN", + Key: "slskd:soulseek:distributednetwork:childlimit", + Type: typeof(int), + Default: Defaults.Soulseek.DistributedNetwork.ChildLimit, + Description: "the limit for the number of distributed children"), + new( + ShortName: default, + LongName: "slsk-connection-timeout", + EnvironmentVariable: "SLSK_CONNECTION_TIMEOUT", + Key: "slskd:soulseek:connection:timeout:connect", + Type: typeof(int), + Default: Defaults.Soulseek.Connection.Timeout.Connect, + Description: "the connection timeout, in miliseconds"), + new( + ShortName: default, + LongName: "slsk-inactivity-timeout", + EnvironmentVariable: "SLSK_INACTIVITY_TIMEOUT", + Key: "slskd:soulseek:connection:timeout:inactivity", + Type: typeof(int), + Default: Defaults.Soulseek.Connection.Timeout.Inactivity, + Description: "the connection inactivity timeout, in miliseconds"), + new( + ShortName: default, + LongName: "slsk-read-buffer", + EnvironmentVariable: "SLSK_READ_BUFFER", + Key: "slskd:soulseek:connection:buffer:read", + Type: typeof(int), + Default: Defaults.Soulseek.Connection.Buffer.Read, + Description: "the read buffer size for connections"), + new( + ShortName: default, + LongName: "slsk-write-buffer", + EnvironmentVariable: "SLSK_WRITE_BUFFER", + Key: "slskd:soulseek:connection:buffer:write", + Type: typeof(int), + Default: Defaults.Soulseek.Connection.Buffer.Write, + Description: "the write buffer size for connections"), + }; + } +}
\ No newline at end of file diff --git a/src/slskd/Common/Configuration/YamlConfigurationSource.cs b/src/slskd/Common/Configuration/YamlConfigurationSource.cs index 0ee5aa8a..27ca6fea 100644 --- a/src/slskd/Common/Configuration/YamlConfigurationSource.cs +++ b/src/slskd/Common/Configuration/YamlConfigurationSource.cs @@ -1,4 +1,4 @@ -namespace slskd +namespace slskd.Configuration { using Microsoft.Extensions.Configuration; using Microsoft.Extensions.FileProviders; |
