From b66ccf8ffa201c7acaaba5f4e512de3e1ddc694a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 25 Jan 2026 18:53:00 -0600 Subject: stub a configuration source --- .../RuntimePatchConfigurationSource.cs | 152 +++++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs new file mode 100644 index 00000000..84dd8b35 --- /dev/null +++ b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs @@ -0,0 +1,152 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Configuration +{ + using System; + using System.Collections.Generic; + using System.Reflection; + using Microsoft.Extensions.Configuration; + + /// + /// Extension methods for adding . + /// + public static class RuntimePatchConfigurationExtensions + { + /// + /// Adds a runtime patch configuration source to . + /// + /// The to which to add. + /// Configures the source. + /// The updated . + public static IConfigurationBuilder AddRuntimePatch( + this IConfigurationBuilder builder, + Action configureSource) + => builder.Add(configureSource); + } + + /// + /// A runtime patch . + /// + public class RuntimePatchConfigurationProvider : ConfigurationProvider + { + /// + /// Initializes a new instance of the class. + /// + /// The source settings. + public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source) + { + Source = source; + source.Provider = this; + } + + private RuntimePatchConfigurationSource Source { get; } + + /// + /// Loads patch values from the current and maps them to the corresponding keys. + /// + public override void Load() + { + var data = new Dictionary(StringComparer.OrdinalIgnoreCase); + + if (Source.Patch != null) + { + Flatten(Source.Patch, "slskd", data); + } + + Data = data; + } + + /// + /// Applies the specified and triggers a configuration reload. + /// + /// The patch to apply. + public void ApplyPatch(OptionsPatch patch) + { + Source.Patch = patch; + Load(); + OnReload(); + } + + private void Flatten(object obj, string path, Dictionary data) + { + if (obj == null) + { + return; + } + + var type = obj.GetType(); + var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); + + foreach (var property in props) + { + var value = property.GetValue(obj); + + if (value == null) + { + continue; + } + + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) + { + Flatten(value, key, data); + } + else + { + data[key] = value.ToString(); + } + } + } + } + + /// + /// Represents a runtime patch as an . + /// + public class RuntimePatchConfigurationSource : IConfigurationSource + { + /// + /// Gets or sets the provider instance. + /// + internal RuntimePatchConfigurationProvider Provider { get; set; } + + /// + /// Gets or sets the current patch. + /// + internal OptionsPatch Patch { get; set; } + + /// + /// Builds the for this source. + /// + /// The . + /// A . + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return new RuntimePatchConfigurationProvider(this); + } + + /// + /// Applies the specified and triggers a configuration reload. + /// + /// The patch to apply. + public void ApplyPatch(OptionsPatch patch) + { + Provider?.ApplyPatch(patch); + } + } +} -- cgit v1.2.3 From e8e3a4aa80fbcca9cf8c841c94ff73396c662352 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 19:48:55 -0600 Subject: wip --- .../RuntimePatchConfigurationSource.cs | 152 --------------------- .../VolatileOverlayConfigurationSource.cs | 127 +++++++++++++++++ src/slskd/Program.cs | 6 +- 3 files changed, 132 insertions(+), 153 deletions(-) delete mode 100644 src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs create mode 100644 src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs b/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs deleted file mode 100644 index 84dd8b35..00000000 --- a/src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs +++ /dev/null @@ -1,152 +0,0 @@ -// -// Copyright (c) slskd Team. All rights reserved. -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU Affero General Public License as published -// by the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU Affero General Public License for more details. -// -// You should have received a copy of the GNU Affero General Public License -// along with this program. If not, see https://www.gnu.org/licenses/. -// - -namespace slskd.Configuration -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using Microsoft.Extensions.Configuration; - - /// - /// Extension methods for adding . - /// - public static class RuntimePatchConfigurationExtensions - { - /// - /// Adds a runtime patch configuration source to . - /// - /// The to which to add. - /// Configures the source. - /// The updated . - public static IConfigurationBuilder AddRuntimePatch( - this IConfigurationBuilder builder, - Action configureSource) - => builder.Add(configureSource); - } - - /// - /// A runtime patch . - /// - public class RuntimePatchConfigurationProvider : ConfigurationProvider - { - /// - /// Initializes a new instance of the class. - /// - /// The source settings. - public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source) - { - Source = source; - source.Provider = this; - } - - private RuntimePatchConfigurationSource Source { get; } - - /// - /// Loads patch values from the current and maps them to the corresponding keys. - /// - public override void Load() - { - var data = new Dictionary(StringComparer.OrdinalIgnoreCase); - - if (Source.Patch != null) - { - Flatten(Source.Patch, "slskd", data); - } - - Data = data; - } - - /// - /// Applies the specified and triggers a configuration reload. - /// - /// The patch to apply. - public void ApplyPatch(OptionsPatch patch) - { - Source.Patch = patch; - Load(); - OnReload(); - } - - private void Flatten(object obj, string path, Dictionary data) - { - if (obj == null) - { - return; - } - - var type = obj.GetType(); - var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); - - foreach (var property in props) - { - var value = property.GetValue(obj); - - if (value == null) - { - continue; - } - - var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); - - if (property.PropertyType.IsClass && property.PropertyType != typeof(string)) - { - Flatten(value, key, data); - } - else - { - data[key] = value.ToString(); - } - } - } - } - - /// - /// Represents a runtime patch as an . - /// - public class RuntimePatchConfigurationSource : IConfigurationSource - { - /// - /// Gets or sets the provider instance. - /// - internal RuntimePatchConfigurationProvider Provider { get; set; } - - /// - /// Gets or sets the current patch. - /// - internal OptionsPatch Patch { get; set; } - - /// - /// Builds the for this source. - /// - /// The . - /// A . - public IConfigurationProvider Build(IConfigurationBuilder builder) - { - return new RuntimePatchConfigurationProvider(this); - } - - /// - /// Applies the specified and triggers a configuration reload. - /// - /// The patch to apply. - public void ApplyPatch(OptionsPatch patch) - { - Provider?.ApplyPatch(patch); - } - } -} diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs new file mode 100644 index 00000000..30328292 --- /dev/null +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -0,0 +1,127 @@ +// +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// + +namespace slskd.Configuration +{ + using System; + using System.Linq; + using System.Reflection; + using System.Text.Json; + using Microsoft.Extensions.Configuration; + + /// + /// A runtime patch . + /// + public class VolatileOverlayConfigurationProvider : ConfigurationProvider + { + // /// + // /// Initializes a new instance of the class. + // /// + // /// The source settings. + // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) + // { + // TargetType = source.TargetType; + // Namespace = TargetType.Namespace.Split('.').First(); + // } + public VolatileOverlayConfigurationProvider() + { + TargetType = typeof(T); + Namespace = TargetType.Namespace.Split('.').First(); + } + + private T Current { get; set; } + private string Namespace { get; set; } + private Type TargetType { get; set; } + + public override void Load() + { + if (Current is null) + { + return; + } + + void Map(Type type, string path) + { + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); + + foreach (PropertyInfo property in props) + { + var key = ConfigurationPath.Combine(path, property.Name.ToLowerInvariant()); + + if (property.PropertyType.Namespace.StartsWith(Namespace)) + { + Map(property.PropertyType, key); + } + else + { + // don't add array values to the configuration; these are additive across providers + // and the default value from the class is "stuck", so adding them again results in duplicates. + if (!property.PropertyType.IsArray) + { + var value = property.GetValue(Current); + + if (value != null) + { + Data[key] = value.ToString(); + } + } + else + { + // serialize array defaults and stick them on the parent key + // (not indexed by array position). this value is "stuck", and + // we want to show that in the config debug view. this isn't really + // functional, just illustrative. + Data[key] = JsonSerializer.Serialize(property.GetValue(Current)); + } + } + } + } + + Map(TargetType, Namespace); + + Console.WriteLine("load called!"); + // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); + + // todo: take the current patch value and stuff the options into Data[key] + Console.WriteLine(Data.ToJson()); + } + + public void Apply(T overlay) + { + Current = overlay; + Load(); + OnReload(); + } + } + + public class VolatileOverlayConfigurationSource : IConfigurationSource + { + public VolatileOverlayConfigurationSource() + { + Provider = new VolatileOverlayConfigurationProvider(); + } + + private VolatileOverlayConfigurationProvider Provider { get; set; } + + public void Apply(T overlay) => Provider.Apply(overlay); + + public IConfigurationProvider Build(IConfigurationBuilder builder) + { + return Provider; + } + } +} diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index d6873dc0..16cfe6c9 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -244,6 +244,7 @@ namespace slskd private static ILogger Log { get; set; } = new ConsoleWriteLineLogger(); private static Mutex Mutex { get; set; } private static IDisposable DotNetRuntimeStats { get; set; } + private static VolatileOverlayConfigurationSource VolatileOverlayConfigurationSource { get; set; } = new VolatileOverlayConfigurationSource(); [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -269,6 +270,8 @@ namespace slskd /// An optional exit code. public static void Exit(int code = 1) => Environment.Exit(code); + public static void ApplyConfigurationOverlay(OptionsOverlay overlay) => VolatileOverlayConfigurationSource.Apply(overlay); + /// /// Entrypoint. /// @@ -1281,7 +1284,8 @@ namespace slskd .AddCommandLine( targetType: typeof(Options), multiValuedArguments, - commandLine: Environment.CommandLine); + commandLine: Environment.CommandLine) + .Add(VolatileOverlayConfigurationSource); } private static IServiceCollection AddDbContext(this IServiceCollection services, string connectionString) -- cgit v1.2.3 From 95197676a8712df1e30edece4f20ec39726d1257 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Tue, 27 Jan 2026 20:07:43 -0600 Subject: more wip --- .../VolatileOverlayConfigurationSource.cs | 25 +++++----------------- 1 file changed, 5 insertions(+), 20 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 30328292..9ae9c9ef 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -28,15 +28,6 @@ namespace slskd.Configuration /// public class VolatileOverlayConfigurationProvider : ConfigurationProvider { - // /// - // /// Initializes a new instance of the class. - // /// - // /// The source settings. - // public VolatileOverlayConfigurationProvider(VolatileOverlayConfigurationSource source) - // { - // TargetType = source.TargetType; - // Namespace = TargetType.Namespace.Split('.').First(); - // } public VolatileOverlayConfigurationProvider() { TargetType = typeof(T); @@ -54,7 +45,7 @@ namespace slskd.Configuration return; } - void Map(Type type, string path) + void Map(Type type, string path, object instance) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -64,7 +55,7 @@ namespace slskd.Configuration if (property.PropertyType.Namespace.StartsWith(Namespace)) { - Map(property.PropertyType, key); + Map(property.PropertyType, key, property.GetValue(instance)); } else { @@ -72,7 +63,7 @@ namespace slskd.Configuration // and the default value from the class is "stuck", so adding them again results in duplicates. if (!property.PropertyType.IsArray) { - var value = property.GetValue(Current); + var value = property.GetValue(instance); if (value != null) { @@ -85,19 +76,13 @@ namespace slskd.Configuration // (not indexed by array position). this value is "stuck", and // we want to show that in the config debug view. this isn't really // functional, just illustrative. - Data[key] = JsonSerializer.Serialize(property.GetValue(Current)); + Data[key] = JsonSerializer.Serialize(property.GetValue(instance)); } } } } - Map(TargetType, Namespace); - - Console.WriteLine("load called!"); - // Data["slskd:soulseek:listenport"] = Current.Soulseek.ListenPort.ToString(); - - // todo: take the current patch value and stuff the options into Data[key] - Console.WriteLine(Data.ToJson()); + Map(TargetType, Namespace, Current); } public void Apply(T overlay) -- cgit v1.2.3 From 894458eaa695a44eed02e5c50190326428337cda Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Thu, 29 Jan 2026 08:03:27 -0600 Subject: docs and stuff --- .../DefaultValueConfigurationSource.cs | 1 + .../VolatileOverlayConfigurationSource.cs | 56 ++++++++++++++++++++-- 2 files changed, 52 insertions(+), 5 deletions(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs index 8cf62e91..f125b1fc 100644 --- a/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs +++ b/src/slskd/Common/Configuration/DefaultValueConfigurationSource.cs @@ -85,6 +85,7 @@ namespace slskd.Configuration /// public override void Load() { + // note: pretty much the same as VolatileOverlayConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path) { var defaults = Activator.CreateInstance(type); diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 9ae9c9ef..8707fe69 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -24,27 +24,44 @@ namespace slskd.Configuration using Microsoft.Extensions.Configuration; /// - /// A runtime patch . + /// A that provides a volatile, run-time overlay that supersedes other configuration + /// sources. /// + /// + /// To use this provider as intended, the application must instantiate and store an instance of either + /// or , and then invoke with the desired overlay object. + /// + /// The Type of the overlay. public class VolatileOverlayConfigurationProvider : ConfigurationProvider { + /// + /// Initializes a new instance of the class. + /// public VolatileOverlayConfigurationProvider() { TargetType = typeof(T); Namespace = TargetType.Namespace.Split('.').First(); } - private T Current { get; set; } + /// + /// Gets the current overlay value. + /// + public T CurrentValue { get; private set; } + private string Namespace { get; set; } private Type TargetType { get; set; } + /// + /// Loads values from the overlay. + /// public override void Load() { - if (Current is null) + if (CurrentValue is null) { return; } + // note: pretty much the same as DefaultValueConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path, object instance) { var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); @@ -82,28 +99,57 @@ namespace slskd.Configuration } } - Map(TargetType, Namespace, Current); + Map(TargetType, Namespace, CurrentValue); } + /// + /// Applies the goven . + /// + /// An object containing the values to overlay. public void Apply(T overlay) { - Current = overlay; + CurrentValue = overlay; Load(); OnReload(); } } + /// + /// Represents values that are provided at run-time as an . + /// + /// + /// To use this provider as intended, the application must instantiate and store an instance of either + /// or , and then invoke with the desired overlay object. + /// + /// The Type of the overlay. public class VolatileOverlayConfigurationSource : IConfigurationSource { + /// + /// Initializes a new instance of the class. + /// public VolatileOverlayConfigurationSource() { Provider = new VolatileOverlayConfigurationProvider(); } + /// + /// Gets the current overlay value. + /// + public T CurrentValue => Provider.CurrentValue; + private VolatileOverlayConfigurationProvider Provider { get; set; } + /// + /// Applies the given to the underlying . + /// + /// An object containing the values to overlay. public void Apply(T overlay) => Provider.Apply(overlay); + /// + /// Builds the for this source. + /// + /// The . + /// A . public IConfigurationProvider Build(IConfigurationBuilder builder) { return Provider; -- cgit v1.2.3 From d70aa1b4427be1a4fed47409720f2035034bf41e Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 09:45:06 -0600 Subject: Update src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 8707fe69..318742e7 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -103,7 +103,7 @@ namespace slskd.Configuration } /// - /// Applies the goven . + /// Applies the given . /// /// An object containing the values to overlay. public void Apply(T overlay) -- cgit v1.2.3 From bf6c8f8974239bf0fcf6d6b141ec674e66c137c6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Fri, 30 Jan 2026 17:57:02 -0600 Subject: avoid null refs --- src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/slskd/Common/Configuration') diff --git a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs index 318742e7..0d6debec 100644 --- a/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs +++ b/src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs @@ -64,6 +64,11 @@ namespace slskd.Configuration // note: pretty much the same as DefaultValueConfigurationProvider; the two should be updated in lockstep void Map(Type type, string path, object instance) { + if (instance is null) + { + return; + } + var props = type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in props) -- cgit v1.2.3