2025-09-24 20:58:23 -05:00
|
|
|
using System;
|
2025-09-29 19:49:09 -05:00
|
|
|
using System.IO;
|
2025-09-24 20:58:23 -05:00
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2025-10-06 19:24:18 -05:00
|
|
|
using LANCommander.SDK.Factories;
|
2025-09-24 20:58:23 -05:00
|
|
|
using LANCommander.SDK.Providers;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2025-09-29 19:49:09 -05:00
|
|
|
using YamlDotNet.Serialization;
|
|
|
|
|
using YamlDotNet.Serialization.NamingConventions;
|
|
|
|
|
using Settings = LANCommander.SDK.Models.Settings;
|
2025-09-24 20:58:23 -05:00
|
|
|
|
|
|
|
|
namespace LANCommander.SDK.Extensions;
|
|
|
|
|
|
|
|
|
|
public static class IConfigurationBuilderExtensions
|
|
|
|
|
{
|
2025-11-19 19:52:36 -06:00
|
|
|
public static IConfiguration ReadFromFile<TSettings>(this IConfigurationBuilder configurationBuilder) where TSettings : Settings, new()
|
2025-09-24 20:58:23 -05:00
|
|
|
{
|
2025-12-11 01:58:28 -06:00
|
|
|
var filePath = AppPaths.GetConfigPath(Settings.SETTINGS_FILE_NAME);
|
2025-11-19 19:52:36 -06:00
|
|
|
|
2025-09-29 19:49:09 -05:00
|
|
|
if (!File.Exists(filePath))
|
|
|
|
|
{
|
|
|
|
|
var settings = Activator.CreateInstance<TSettings>();
|
|
|
|
|
|
|
|
|
|
var serializer = new SerializerBuilder()
|
|
|
|
|
.WithNamingConvention(PascalCaseNamingConvention.Instance)
|
|
|
|
|
.Build();
|
|
|
|
|
|
|
|
|
|
File.WriteAllText(filePath, serializer.Serialize(settings));
|
|
|
|
|
}
|
2025-11-19 19:52:36 -06:00
|
|
|
|
2025-09-24 20:58:23 -05:00
|
|
|
var bootstrap = new ConfigurationBuilder()
|
2025-11-17 18:01:53 -06:00
|
|
|
.AddYamlFile(filePath, false, true)
|
2025-09-24 20:58:23 -05:00
|
|
|
.Build();
|
2025-11-19 19:52:36 -06:00
|
|
|
|
|
|
|
|
configurationBuilder.AddConfiguration(bootstrap);
|
2025-09-24 20:58:23 -05:00
|
|
|
|
2025-11-19 19:52:36 -06:00
|
|
|
return bootstrap;
|
2025-09-24 20:58:23 -05:00
|
|
|
}
|
2025-11-19 19:52:36 -06:00
|
|
|
|
|
|
|
|
public static IServerConfigurationRefresher ReadFromServer<TSettings>(this IConfigurationBuilder configurationBuilder, IConfiguration source)
|
|
|
|
|
where TSettings : Settings, new()
|
2025-09-24 20:58:23 -05:00
|
|
|
{
|
|
|
|
|
var src = new ServerConfigurationSource
|
|
|
|
|
{
|
2025-11-19 19:52:36 -06:00
|
|
|
Configuration = source,
|
2025-09-24 20:58:23 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
configurationBuilder.Add(src);
|
|
|
|
|
|
2025-11-19 19:52:36 -06:00
|
|
|
var refresher = new LazyRefresher(() => src.Provider);
|
2025-09-24 20:58:23 -05:00
|
|
|
|
2025-11-19 19:52:36 -06:00
|
|
|
return refresher;
|
2025-09-24 20:58:23 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sealed class LazyRefresher(Func<ServerConfigurationProvider?> getProvider) : IServerConfigurationRefresher
|
|
|
|
|
{
|
|
|
|
|
private readonly Func<ServerConfigurationProvider?> _getProvider = getProvider;
|
|
|
|
|
|
|
|
|
|
public async Task RefreshAsync(CancellationToken cancellationToken = default)
|
|
|
|
|
{
|
|
|
|
|
var provider = _getProvider() ??
|
|
|
|
|
throw new InvalidOperationException("Server configuration provider is not yet built");
|
|
|
|
|
|
|
|
|
|
await provider.RefreshAsync(cancellationToken);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|