Init
This commit is contained in:
62
src/Band/Models/AppSettings.cs
Normal file
62
src/Band/Models/AppSettings.cs
Normal file
@ -0,0 +1,62 @@
|
||||
using System.Text.Json;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Band.Models;
|
||||
|
||||
public record AppSettings(string Company, Settings Settings, string URLs, string[] WithOrigins) {
|
||||
|
||||
public override string ToString() {
|
||||
string result = JsonSerializer.Serialize(this, AppSettingsSourceGenerationContext.Default.AppSettings);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void Verify(AppSettings appSettings) {
|
||||
if (appSettings?.Company is null) {
|
||||
throw new NullReferenceException(nameof(Company));
|
||||
}
|
||||
if (appSettings?.URLs is null) {
|
||||
throw new NullReferenceException(nameof(URLs));
|
||||
}
|
||||
if (appSettings?.WithOrigins is null) {
|
||||
throw new NullReferenceException(nameof(WithOrigins));
|
||||
}
|
||||
if (appSettings?.Settings?.Band is null) {
|
||||
throw new NullReferenceException(nameof(Settings.Band));
|
||||
}
|
||||
}
|
||||
|
||||
public static AppSettings Get(IConfigurationRoot configurationRoot) {
|
||||
AppSettings result;
|
||||
#pragma warning disable IL3050, IL2026
|
||||
string? urls = configurationRoot.GetSection(nameof(URLs)).Get<string>();
|
||||
string? company = configurationRoot.GetSection(nameof(Company)).Get<string>();
|
||||
Settings? settings = configurationRoot.GetSection(nameof(Settings)).Get<Settings>();
|
||||
string[]? withOrigins = configurationRoot.GetSection(nameof(WithOrigins)).Get<string[]>();
|
||||
#pragma warning restore IL3050, IL2026
|
||||
if (company is null || settings is null || urls is null || withOrigins is null) {
|
||||
List<string> paths = [];
|
||||
foreach (IConfigurationProvider configurationProvider in configurationRoot.Providers) {
|
||||
if (configurationProvider is not Microsoft.Extensions.Configuration.Json.JsonConfigurationProvider jsonConfigurationProvider) {
|
||||
continue;
|
||||
}
|
||||
if (jsonConfigurationProvider.Source.FileProvider is not Microsoft.Extensions.FileProviders.PhysicalFileProvider physicalFileProvider) {
|
||||
continue;
|
||||
}
|
||||
paths.Add(physicalFileProvider.Root);
|
||||
}
|
||||
throw new NotSupportedException($"Not found!{Environment.NewLine}{string.Join(Environment.NewLine, paths.Distinct())}");
|
||||
}
|
||||
result = new(Company: company,
|
||||
Settings: settings,
|
||||
URLs: urls,
|
||||
WithOrigins: withOrigins);
|
||||
Verify(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[JsonSourceGenerationOptions(WriteIndented = true)]
|
||||
[JsonSerializable(typeof(AppSettings))]
|
||||
internal partial class AppSettingsSourceGenerationContext : JsonSerializerContext {
|
||||
}
|
||||
Reference in New Issue
Block a user