|
1 | | -using Microsoft.Extensions.Options; |
2 | | -using OpenShock.Common.Options; |
| 1 | +using OpenShock.Common.Options; |
| 2 | +using StackExchange.Redis; |
3 | 3 |
|
4 | 4 | namespace OpenShock.Common.Extensions; |
5 | 5 |
|
6 | 6 | public static class ConfigurationExtensions |
7 | 7 | { |
8 | | - public static WebApplicationBuilder RegisterCommonOpenShockOptions(this WebApplicationBuilder builder) |
| 8 | + // Reusable helper: bind or throw with a clear message |
| 9 | + private static T GetRequired<T>(this ConfigurationManager configuration, string path, string? example = null) where T : class |
9 | 10 | { |
10 | | - if (builder.Environment.IsDevelopment()) |
| 11 | + var section = configuration.GetSection(path); |
| 12 | + var value = section.Get<T>(); |
| 13 | + if (value is null) |
11 | 14 | { |
12 | | - Console.WriteLine(builder.Configuration.GetDebugView()); |
| 15 | + var hint = string.IsNullOrWhiteSpace(example) ? "" : $" Example: {example}"; |
| 16 | + throw new InvalidOperationException( |
| 17 | + $"Missing or invalid configuration at '{path}'.{hint}" |
| 18 | + ); |
13 | 19 | } |
14 | | - |
15 | | - builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetRequiredSection(DatabaseOptions.SectionName)); |
16 | | - builder.Services.AddSingleton<IValidateOptions<DatabaseOptions>, DatabaseOptionsValidator>(); |
17 | | - |
18 | | - builder.Services.Configure<RedisOptions>(builder.Configuration.GetRequiredSection(RedisOptions.SectionName)); |
19 | | - builder.Services.AddSingleton<IValidateOptions<RedisOptions>, RedisOptionsValidator>(); |
20 | | - |
21 | | - builder.Services.Configure<MetricsOptions>(builder.Configuration.GetSection(MetricsOptions.SectionName)); |
22 | | - builder.Services.AddSingleton<IValidateOptions<MetricsOptions>, MetricsOptionsValidator>(); |
23 | | - |
24 | | - return builder; |
| 20 | + return value; |
| 21 | + } |
| 22 | + |
| 23 | + public static DatabaseOptions RegisterDatabaseOptions(this WebApplicationBuilder builder) |
| 24 | + { |
| 25 | + // If DatabaseOptions has required fields, you can validate them after binding. |
| 26 | + var options = builder.Configuration.GetRequired<DatabaseOptions>( |
| 27 | + "OpenShock:DB", |
| 28 | + """{ "ConnectionString": "..." }""" |
| 29 | + ); |
| 30 | + |
| 31 | + builder.Services.AddSingleton(options); |
| 32 | + return options; |
| 33 | + } |
| 34 | + |
| 35 | + private sealed record RedisSection(string? Conn, string? User, string? Password, string? Host, string? Port); |
| 36 | + public static ConfigurationOptions RegisterRedisOptions(this WebApplicationBuilder builder) |
| 37 | + { |
| 38 | + var section = builder.Configuration.GetRequired<RedisSection>( |
| 39 | + "OpenShock:Redis", |
| 40 | + """{ "Conn": "redis://user:pass@host:6379" } or { "Host": "host", "Password": "...", "Port": "6379" }""" |
| 41 | + ); |
| 42 | + |
| 43 | + ConfigurationOptions options; |
| 44 | + |
| 45 | + if (!string.IsNullOrWhiteSpace(section.Conn)) |
| 46 | + { |
| 47 | + options = ConfigurationOptions.Parse(section.Conn); |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + if (string.IsNullOrWhiteSpace(section.Host)) |
| 52 | + throw new ArgumentException("Redis Host is required (OpenShock:Redis:Host)."); |
| 53 | + |
| 54 | + if (string.IsNullOrWhiteSpace(section.Password)) |
| 55 | + throw new ArgumentException("Redis Password is required (OpenShock:Redis:Password)."); |
| 56 | + |
| 57 | + // Parse port with sane default + validation |
| 58 | + ushort port = 6379; |
| 59 | + if (!string.IsNullOrWhiteSpace(section.Port)) |
| 60 | + { |
| 61 | + if (!ushort.TryParse(section.Port, out port) || port == 0) |
| 62 | + throw new ArgumentException("Redis Port must be a number between 1 and 65535 (OpenShock:Redis:Port)."); |
| 63 | + } |
| 64 | + |
| 65 | + options = new ConfigurationOptions |
| 66 | + { |
| 67 | + User = section.User, // optional; only if ACLs enabled |
| 68 | + Password = section.Password, |
| 69 | + Ssl = false, // flip via connection string if needed |
| 70 | + }; |
| 71 | + options.EndPoints.Add(section.Host!, port); |
| 72 | + } |
| 73 | + |
| 74 | + // Sensible defaults (adjust to taste) |
| 75 | + options.AbortOnConnectFail = true; |
| 76 | + |
| 77 | + builder.Services.AddSingleton(options); |
| 78 | + return options; |
| 79 | + } |
| 80 | + |
| 81 | + public static MetricsOptions RegisterMetricsOptions(this WebApplicationBuilder builder) |
| 82 | + { |
| 83 | + var options = builder.Configuration.GetRequired<MetricsOptions>( |
| 84 | + "OpenShock:Metrics" |
| 85 | + ); |
| 86 | + |
| 87 | + builder.Services.AddSingleton(options); |
| 88 | + return options; |
| 89 | + } |
| 90 | + |
| 91 | + public static FrontendOptions RegisterFrontendOptions(this WebApplicationBuilder builder) |
| 92 | + { |
| 93 | + var options = builder.Configuration.GetRequired<FrontendOptions>( |
| 94 | + "OpenShock:Frontend" |
| 95 | + ); |
| 96 | + |
| 97 | + builder.Services.AddSingleton(options); |
| 98 | + return options; |
25 | 99 | } |
26 | 100 | } |
0 commit comments