A:初版
This commit is contained in:
1 parent
237cbd8788
commit
ab3721912d
102 files changed
+7929
-20
No files matched your search
@@ -0,0 +1,45 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||
|
||||
namespace Config;
|
||||
|
||||
public interface ISystemConfigAccessor
|
||||
{
|
||||
SystemConfig Value { get; }
|
||||
}
|
||||
|
||||
internal sealed class SystemConfigAccessor : ISystemConfigAccessor, IDisposable
|
||||
{
|
||||
private readonly ConfigManager _cm;
|
||||
private readonly IDisposable _subscription;
|
||||
private SystemConfig _current;
|
||||
|
||||
public SystemConfigAccessor(ConfigManager cm)
|
||||
{
|
||||
_cm = cm;
|
||||
_current = cm.Get<SystemConfig>(section: "System", validate: false);
|
||||
_subscription = cm.OnReload(() =>
|
||||
{
|
||||
// Reload on change
|
||||
_current = _cm.Get<SystemConfig>(section: "System", validate: false);
|
||||
});
|
||||
}
|
||||
|
||||
public SystemConfig Value => _current;
|
||||
|
||||
public void Dispose() => _subscription.Dispose();
|
||||
}
|
||||
|
||||
public static class SystemConfigExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Registers SystemConfig accessor backed by ConfigManager under the "System" section.
|
||||
/// </summary>
|
||||
public static IServiceCollection AddSystemConfig(this IServiceCollection services, ConfigManager cm)
|
||||
{
|
||||
services.TryAddSingleton(cm);
|
||||
services.TryAddSingleton<ISystemConfigAccessor, SystemConfigAccessor>();
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Config;
|
||||
|
||||
/// <summary>
|
||||
/// Global system-level configuration, shared across modules.
|
||||
/// </summary>
|
||||
public sealed class SystemConfig
|
||||
{
|
||||
[Required]
|
||||
[RegularExpression(@"^([0-9]{1,3}\.){3}[0-9]{1,3}$|^localhost$|^[a-zA-Z0-9_.-]+$")]
|
||||
public string HTTPIp { get; set; } = "127.0.0.1";
|
||||
|
||||
[Range(1, 65535)]
|
||||
public int HttpPort { get; set; } = 8080;
|
||||
|
||||
[Range(1, 600)]
|
||||
public int TimeoutSeconds { get; set; } = 30;
|
||||
|
||||
public string Scheme { get; set; } = "http";
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user