-
Notifications
You must be signed in to change notification settings - Fork 10
Reduce IOptions usage #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
185b815
Initial work
hhvrc 7039364
Revert unrelated changes
hhvrc 53a4592
Merge branch 'develop' into feature/replace-ioptions
hhvrc 781568f
Fix up more stuff
hhvrc 164ac06
revert some stuff
hhvrc 8f98292
Improve exceptions
hhvrc 5f5cc26
Do some validation
hhvrc 54c342f
Fixed it
hhvrc 95b5d02
Remove all IOptions
hhvrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,106 @@ | ||
| using Microsoft.Extensions.Options; | ||
| using OpenShock.Common.Options; | ||
| using OpenShock.Common.Options; | ||
| using OpenShock.Common.Utils; | ||
| using StackExchange.Redis; | ||
|
|
||
| namespace OpenShock.Common.Extensions; | ||
|
|
||
| public static class ConfigurationExtensions | ||
| { | ||
| public static WebApplicationBuilder RegisterCommonOpenShockOptions(this WebApplicationBuilder builder) | ||
| public static DatabaseOptions RegisterDatabaseOptions(this WebApplicationBuilder builder) | ||
| { | ||
| if (builder.Environment.IsDevelopment()) | ||
| var options = builder.Configuration.GetRequiredSection("OpenShock:DB").Get<DatabaseOptions>(); | ||
| if (options is null) | ||
| throw new InvalidOperationException("Missing or invalid configuration for OpenShock:DB."); | ||
|
|
||
| if (string.IsNullOrEmpty(options.Conn)) throw new InvalidOperationException("Missing or invalid connection string (OpenShock:DB:Conn)."); | ||
|
|
||
| builder.Services.AddSingleton(options); | ||
| return options; | ||
| } | ||
|
|
||
| private sealed class RedisSection | ||
| { | ||
| public string? Conn { get; init; } | ||
| public string? User { get; init; } | ||
| public string? Password { get; init; } | ||
| public string? Host { get; init; } | ||
| public string? Port { get; init; } | ||
| } | ||
|
|
||
| public static ConfigurationOptions RegisterRedisOptions(this WebApplicationBuilder builder) | ||
| { | ||
| var section = builder.Configuration.GetRequiredSection("OpenShock:Redis").Get<RedisSection>(); | ||
| if (section is null) | ||
| throw new InvalidOperationException("Missing or invalid configuration for OpenShock:Redis."); | ||
|
|
||
| ConfigurationOptions options; | ||
|
|
||
| if (!string.IsNullOrWhiteSpace(section.Conn)) | ||
| { | ||
| Console.WriteLine(builder.Configuration.GetDebugView()); | ||
| options = ConfigurationOptions.Parse(section.Conn); | ||
| } | ||
| else | ||
| { | ||
| if (string.IsNullOrWhiteSpace(section.Host)) | ||
| throw new ArgumentException("Redis Host is required (OpenShock:Redis:Host)."); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(section.Password)) | ||
| throw new ArgumentException("Redis Password is required (OpenShock:Redis:Password)."); | ||
|
|
||
| // Parse port with sane default + validation | ||
| ushort port = 6379; | ||
| if (!string.IsNullOrWhiteSpace(section.Port)) | ||
| { | ||
| if (!ushort.TryParse(section.Port, out port) || port == 0) | ||
| throw new InvalidOperationException("Redis Port must be a number between 1 and 65535 (OpenShock:Redis:Port)."); | ||
| } | ||
|
|
||
| options = new ConfigurationOptions | ||
| { | ||
| User = section.User, | ||
| Password = section.Password, | ||
| Ssl = false, | ||
| }; | ||
| options.EndPoints.Add(section.Host!, port); | ||
| } | ||
|
|
||
| // Sensible defaults (adjust to taste) | ||
| options.AbortOnConnectFail = true; | ||
|
|
||
| builder.Services.AddSingleton(options); | ||
| return options; | ||
| } | ||
|
|
||
| public static MetricsOptions RegisterMetricsOptions(this WebApplicationBuilder builder) | ||
| { | ||
| var options = builder.Configuration.GetSection("OpenShock:Metrics").Get<MetricsOptions>() ?? new MetricsOptions | ||
| { | ||
| AllowedNetworks = TrustedProxiesFetcher.PrivateNetworks | ||
| }; | ||
|
|
||
| builder.Services.AddSingleton(options); | ||
| return options; | ||
| } | ||
|
|
||
| public static FrontendOptions RegisterFrontendOptions(this WebApplicationBuilder builder) | ||
| { | ||
| var section = builder.Configuration.GetRequiredSection("OpenShock:Frontend"); | ||
|
|
||
| builder.Services.Configure<DatabaseOptions>(builder.Configuration.GetRequiredSection(DatabaseOptions.SectionName)); | ||
| builder.Services.AddSingleton<IValidateOptions<DatabaseOptions>, DatabaseOptionsValidator>(); | ||
|
|
||
| builder.Services.Configure<RedisOptions>(builder.Configuration.GetRequiredSection(RedisOptions.SectionName)); | ||
| builder.Services.AddSingleton<IValidateOptions<RedisOptions>, RedisOptionsValidator>(); | ||
| var options = new FrontendOptions | ||
| { | ||
| BaseUrl = section.GetValue<Uri>("BaseUrl") ?? throw new InvalidOperationException("Frontend BaseUrl is required (OpenShock:Frontend:BaseUrl)."), | ||
| ShortUrl = section.GetValue<Uri>("ShortUrl") ?? throw new InvalidOperationException("Frontend ShortUrl is required (OpenShock:Frontend:ShortUrl)."), | ||
| CookieDomains = SplitCsv(section["CookieDomain"] ?? throw new InvalidOperationException("Frontend CookieDomain is required (OpenShock:Frontend:CookieDomain).")), | ||
| }; | ||
|
|
||
| builder.Services.Configure<MetricsOptions>(builder.Configuration.GetSection(MetricsOptions.SectionName)); | ||
| builder.Services.AddSingleton<IValidateOptions<MetricsOptions>, MetricsOptionsValidator>(); | ||
| if (options.CookieDomains.Count == 0) throw new InvalidOperationException("At least one cookie domain must be configured (OpenShock:Frontend:CookieDomain)."); | ||
|
|
||
| return builder; | ||
| builder.Services.AddSingleton(options); | ||
| return options; | ||
|
|
||
| static string[] SplitCsv(string csv) | ||
| { | ||
| return csv.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.