From f5071fce7461949be7ffac129f283815a3ecd389 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 2 Feb 2021 21:28:39 -0600 Subject: add Validation namespace and components --- .../Common/Validation/ValidationExtensions.cs | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/slskd/Common/Validation/ValidationExtensions.cs (limited to 'src/slskd/Common/Validation/ValidationExtensions.cs') diff --git a/src/slskd/Common/Validation/ValidationExtensions.cs b/src/slskd/Common/Validation/ValidationExtensions.cs new file mode 100644 index 00000000..511e1a31 --- /dev/null +++ b/src/slskd/Common/Validation/ValidationExtensions.cs @@ -0,0 +1,50 @@ +namespace slskd.Validation +{ + using System.Collections.Generic; + using System.ComponentModel.DataAnnotations; + using System.Linq; + using slskd.Configuration; + + public static class ValidationExtensions + { + public static string GetResultView(this ValidationResult result) + { + return string.Join("\n", result.GetResultView(0)); + } + + public static IEnumerable GetResultView(this ValidationResult result, int depth = 0) + { + var indent = new string(' ', depth * 2); + + if (result is CompositeValidationResult composite) + { + var lines = new[] { indent + result + ":" }.ToList(); + + foreach (var child in composite.Results) + { + lines.AddRange(child.GetResultView(depth + 1)); + } + + return lines; + } + else + { + return new[] { indent + result }; + } + } + + public static bool TryValidate(this Options options, out CompositeValidationResult result) + { + result = null; + var results = new List(); + + if (!Validator.TryValidateObject(options, new ValidationContext(options), results, true)) + { + result = new CompositeValidationResult("Invalid configuration", results); + return false; + } + + return true; + } + } +} -- cgit v1.2.3 From d3011c281c118a20b41b9ab30367e3fc6d1fca5b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 2 Feb 2021 22:00:40 -0600 Subject: use props for config and options --- .../Common/Validation/ValidationExtensions.cs | 13 ++++-- src/slskd/Program.cs | 50 +++++++++++----------- 2 files changed, 36 insertions(+), 27 deletions(-) (limited to 'src/slskd/Common/Validation/ValidationExtensions.cs') diff --git a/src/slskd/Common/Validation/ValidationExtensions.cs b/src/slskd/Common/Validation/ValidationExtensions.cs index 511e1a31..f0186e76 100644 --- a/src/slskd/Common/Validation/ValidationExtensions.cs +++ b/src/slskd/Common/Validation/ValidationExtensions.cs @@ -7,12 +7,19 @@ public static class ValidationExtensions { - public static string GetResultView(this ValidationResult result) + public static string GetResultView(this ValidationResult result, string message) { - return string.Join("\n", result.GetResultView(0)); + var view = result.GetResultView(0).ToList(); + + if (view.Count() > 0 && !string.IsNullOrEmpty(message)) + { + view[0] = message; + } + + return string.Join("\n", view); } - public static IEnumerable GetResultView(this ValidationResult result, int depth = 0) + private static IEnumerable GetResultView(this ValidationResult result, int depth = 0) { var indent = new string(' ', depth * 2); diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index e0540ef6..da4984fe 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -58,6 +58,9 @@ [CommandLineArgument('v', "version", "display version information")] private static bool ShowVersion { get; set; } + private static Options Options { get; } = new Options(); + private static IConfigurationRoot Configuration { get; set; } + public static void Main(string[] args) { CommandLineArguments.Populate(); @@ -76,20 +79,18 @@ return; } - var configuration = new ConfigurationBuilder() - .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile) - .Build(); - - var options = new Options(); - try { - configuration.GetSection("slskd") - .Bind(options, (o) => { o.BindNonPublicProperties = true; }); + Configuration = new ConfigurationBuilder() + .AddConfigurationProviders(EnvironmentVariablePrefix, ConfigurationFile) + .Build(); + + Configuration.GetSection("slskd") + .Bind(Options, (o) => { o.BindNonPublicProperties = true; }); - if (!options.TryValidate(out var result)) + if (!Options.TryValidate(out var result)) { - Console.WriteLine(result.GetResultView()); + Console.WriteLine(result.GetResultView("Invalid configuration:")); return; } } @@ -99,20 +100,21 @@ return; } - if (!options.NoLogo) + if (!Options.NoLogo) { PrintLogo(Version); } - if (options.Debug) + if (Options.Debug) { - Console.WriteLine(configuration.GetDebugView()); + Console.WriteLine("Configuration:"); + Console.WriteLine(Configuration.GetDebugView()); Log.Logger = new LoggerConfiguration() .MinimumLevel.Debug() .MinimumLevel.Override("Microsoft", LogEventLevel.Information) .Enrich.WithProperty("Version", Version) - .Enrich.WithProperty("InstanceName", options.InstanceName) + .Enrich.WithProperty("InstanceName", Options.InstanceName) .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() @@ -124,9 +126,9 @@ outputTemplate: "[{SourceContext}] [{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( - e => !string.IsNullOrEmpty(options.Logger.Loki), + e => !string.IsNullOrEmpty(Options.Logger.Loki), config => config.GrafanaLoki( - options.Logger.Loki ?? string.Empty, + Options.Logger.Loki ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) .CreateLogger(); } @@ -137,7 +139,7 @@ .MinimumLevel.Override("Microsoft", LogEventLevel.Warning) .MinimumLevel.Override("slskd.Security.PassthroughAuthenticationHandler", LogEventLevel.Information) .Enrich.WithProperty("Version", Version) - .Enrich.WithProperty("InstanceName", options.InstanceName) + .Enrich.WithProperty("InstanceName", Options.InstanceName) .Enrich.WithProperty("InvocationId", InvocationId) .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() @@ -149,9 +151,9 @@ outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( - e => !string.IsNullOrEmpty(options.Logger.Loki), + e => !string.IsNullOrEmpty(Options.Logger.Loki), config => config.GrafanaLoki( - options.Logger.Loki ?? string.Empty, + Options.Logger.Loki ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) .CreateLogger(); } @@ -159,18 +161,18 @@ var logger = Log.ForContext(); logger.Information("Version: {Version}", Version); - logger.Information("Instance Name: {InstanceName}", options.InstanceName); + logger.Information("Instance Name: {InstanceName}", Options.InstanceName); logger.Information("Invocation ID: {InvocationId}", InvocationId); logger.Information("Process ID: {ProcessId}", ProcessId); - if (!string.IsNullOrEmpty(options.Logger.Loki)) + if (!string.IsNullOrEmpty(Options.Logger.Loki)) { - logger.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", options.Logger.Loki); + logger.Information("Forwarding logs to Grafana Loki instance at {LoggerLokiUrl}", Options.Logger.Loki); } try { - if (options.Feature.Prometheus) + if (Options.Feature.Prometheus) { using var runtimeMetrics = DotNetRuntimeStatsBuilder.Default().StartCollecting(); } @@ -186,7 +188,7 @@ //.UseUrls( // $"http://0.0.0.0:{options.Web.Port}", // $"https://0.0.0.0:{options.Web.Https.Port}") - .UseUrls($"http://0.0.0.0:{options.Web.Port}") + .UseUrls($"http://0.0.0.0:{Options.Web.Port}") .Build() .Run(); } -- cgit v1.2.3