diff options
| author | JP Dillingham <jp@dillingham.me> | 2026-01-27 19:48:55 -0600 |
|---|---|---|
| committer | JP Dillingham <jp@dillingham.me> | 2026-01-27 19:48:55 -0600 |
| commit | e8e3a4aa80fbcca9cf8c841c94ff73396c662352 (patch) | |
| tree | 4a22c65f99207d10f1f261ae7fc9da624a9bf768 /src | |
| parent | 8859b9a6c82b1490349f7ffb4f6699b502384f8a (diff) | |
| download | slskd-e8e3a4aa80fbcca9cf8c841c94ff73396c662352.tar.gz | |
wip
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Common/Configuration/RuntimePatchConfigurationSource.cs | 152 | ||||
| -rw-r--r-- | src/slskd/Common/Configuration/VolatileOverlayConfigurationSource.cs | 127 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 6 |
3 files changed, 132 insertions, 153 deletions
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 file="RuntimePatchConfigurationSource.cs" company="slskd Team"> -// 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/. -// </copyright> - -namespace slskd.Configuration -{ - using System; - using System.Collections.Generic; - using System.Reflection; - using Microsoft.Extensions.Configuration; - - /// <summary> - /// Extension methods for adding <see cref="RuntimePatchConfigurationProvider"/>. - /// </summary> - public static class RuntimePatchConfigurationExtensions - { - /// <summary> - /// Adds a runtime patch configuration source to <paramref name="builder"/>. - /// </summary> - /// <param name="builder">The <see cref="IConfigurationBuilder"/> to which to add.</param> - /// <param name="configureSource">Configures the source.</param> - /// <returns>The updated <see cref="IConfigurationBuilder"/>.</returns> - public static IConfigurationBuilder AddRuntimePatch( - this IConfigurationBuilder builder, - Action<RuntimePatchConfigurationSource> configureSource) - => builder.Add(configureSource); - } - - /// <summary> - /// A runtime patch <see cref="ConfigurationProvider"/>. - /// </summary> - public class RuntimePatchConfigurationProvider : ConfigurationProvider - { - /// <summary> - /// Initializes a new instance of the <see cref="RuntimePatchConfigurationProvider"/> class. - /// </summary> - /// <param name="source">The source settings.</param> - public RuntimePatchConfigurationProvider(RuntimePatchConfigurationSource source) - { - Source = source; - source.Provider = this; - } - - private RuntimePatchConfigurationSource Source { get; } - - /// <summary> - /// Loads patch values from the current <see cref="OptionsPatch"/> and maps them to the corresponding keys. - /// </summary> - public override void Load() - { - var data = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase); - - if (Source.Patch != null) - { - Flatten(Source.Patch, "slskd", data); - } - - Data = data; - } - - /// <summary> - /// Applies the specified <paramref name="patch"/> and triggers a configuration reload. - /// </summary> - /// <param name="patch">The patch to apply.</param> - public void ApplyPatch(OptionsPatch patch) - { - Source.Patch = patch; - Load(); - OnReload(); - } - - private void Flatten(object obj, string path, Dictionary<string, string> 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(); - } - } - } - } - - /// <summary> - /// Represents a runtime patch as an <see cref="IConfigurationSource"/>. - /// </summary> - public class RuntimePatchConfigurationSource : IConfigurationSource - { - /// <summary> - /// Gets or sets the provider instance. - /// </summary> - internal RuntimePatchConfigurationProvider Provider { get; set; } - - /// <summary> - /// Gets or sets the current patch. - /// </summary> - internal OptionsPatch Patch { get; set; } - - /// <summary> - /// Builds the <see cref="RuntimePatchConfigurationProvider"/> for this source. - /// </summary> - /// <param name="builder">The <see cref="IConfigurationBuilder"/>.</param> - /// <returns>A <see cref="RuntimePatchConfigurationProvider"/>.</returns> - public IConfigurationProvider Build(IConfigurationBuilder builder) - { - return new RuntimePatchConfigurationProvider(this); - } - - /// <summary> - /// Applies the specified <paramref name="patch"/> and triggers a configuration reload. - /// </summary> - /// <param name="patch">The patch to apply.</param> - 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 file="VolatileOverlayConfigurationSource.cs" company="slskd Team"> +// 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/. +// </copyright> + +namespace slskd.Configuration +{ + using System; + using System.Linq; + using System.Reflection; + using System.Text.Json; + using Microsoft.Extensions.Configuration; + + /// <summary> + /// A runtime patch <see cref="ConfigurationProvider"/>. + /// </summary> + public class VolatileOverlayConfigurationProvider<T> : ConfigurationProvider + { + // /// <summary> + // /// Initializes a new instance of the <see cref="VolatileOverlayConfigurationProvider"/> class. + // /// </summary> + // /// <param name="source">The source settings.</param> + // 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<T> : IConfigurationSource + { + public VolatileOverlayConfigurationSource() + { + Provider = new VolatileOverlayConfigurationProvider<T>(); + } + + private VolatileOverlayConfigurationProvider<T> 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<OptionsOverlay> VolatileOverlayConfigurationSource { get; set; } = new VolatileOverlayConfigurationSource<OptionsOverlay>(); [Argument('g', "generate-cert", "generate X509 certificate and password for HTTPs")] private static bool GenerateCertificate { get; set; } @@ -269,6 +270,8 @@ namespace slskd /// <param name="code">An optional exit code.</param> public static void Exit(int code = 1) => Environment.Exit(code); + public static void ApplyConfigurationOverlay(OptionsOverlay overlay) => VolatileOverlayConfigurationSource.Apply(overlay); + /// <summary> /// Entrypoint. /// </summary> @@ -1281,7 +1284,8 @@ namespace slskd .AddCommandLine( targetType: typeof(Options), multiValuedArguments, - commandLine: Environment.CommandLine); + commandLine: Environment.CommandLine) + .Add(VolatileOverlayConfigurationSource); } private static IServiceCollection AddDbContext<T>(this IServiceCollection services, string connectionString) |
