| | | 1 | | using ArturRios.Configuration.Enums; |
| | | 2 | | using ArturRios.Extensions; |
| | | 3 | | using Microsoft.AspNetCore.Builder; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Util.WebApi.Configuration; |
| | | 6 | | |
| | | 7 | | /// <summary>Startup parameters parsed from command-line arguments (e.g. <c>Environment:Production</c>, |
| | | 8 | | /// <c>EnableSwaggerDocs:false</c>), controlling how the application is configured by <see cref="WebApiStartup"/> or |
| | | 9 | | /// by the <see cref="WebApplicationBuilderExtensions"/> it is built from.</summary> |
| | | 10 | | public class WebApiParameters |
| | | 11 | | { |
| | 1 | 12 | | private static readonly string[] DefaultSwaggerEnvironments = |
| | 1 | 13 | | [nameof(EnvironmentType.Development), nameof(EnvironmentType.Local)]; |
| | | 14 | | |
| | | 15 | | /// <summary>Parses <paramref name="args"/> into the corresponding properties. Keys are case-insensitive; |
| | | 16 | | /// unrecognized or malformed entries are ignored, leaving the default values in place.</summary> |
| | | 17 | | /// <param name="args">The command-line arguments, each in <c>Key:Value</c> form.</param> |
| | 34 | 18 | | public WebApiParameters(string[] args) |
| | | 19 | | { |
| | 34 | 20 | | Args = args; |
| | | 21 | | |
| | 34 | 22 | | if (args.IsEmpty()) |
| | | 23 | | { |
| | 5 | 24 | | return; |
| | | 25 | | } |
| | | 26 | | |
| | 198 | 27 | | foreach (var arg in args) |
| | | 28 | | { |
| | 70 | 29 | | var parts = arg.Split(':', 2, StringSplitOptions.TrimEntries); |
| | | 30 | | |
| | 70 | 31 | | if (parts.Length != 2) |
| | | 32 | | { |
| | | 33 | | continue; |
| | | 34 | | } |
| | | 35 | | |
| | 70 | 36 | | var key = parts[0]; |
| | 70 | 37 | | var value = parts[1]; |
| | | 38 | | |
| | 70 | 39 | | switch (key.ToLowerInvariant()) |
| | | 40 | | { |
| | | 41 | | case "environment": |
| | 21 | 42 | | EnvironmentName = value.IsValidEnumValue<EnvironmentType>() ? value : string.Empty; |
| | 21 | 43 | | break; |
| | | 44 | | case "enableswaggerdocs": |
| | 2 | 45 | | EnableSwaggerDocs = value.ParseToBoolOrDefault(true)!.Value; |
| | 2 | 46 | | break; |
| | | 47 | | case "useappsettings": |
| | | 48 | | case "useappsetting": |
| | 23 | 49 | | UseAppSettings = value.ParseToBoolOrDefault(true)!.Value; |
| | 23 | 50 | | break; |
| | | 51 | | case "useenvfile": |
| | 22 | 52 | | UseEnvFile = value.ParseToBoolOrDefault(true)!.Value; |
| | 22 | 53 | | break; |
| | | 54 | | case "swaggerenvironments": |
| | 2 | 55 | | if (value.StartsWith('[') && value.EndsWith(']')) |
| | | 56 | | { |
| | 2 | 57 | | SwaggerEnvironments = value.Trim('[', ']').Split( |
| | 2 | 58 | | ',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | break; |
| | | 62 | | } |
| | | 63 | | } |
| | 29 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <summary>The command-line arguments these parameters were parsed from.</summary> |
| | 22 | 67 | | public string[] Args { get; } |
| | | 68 | | |
| | | 69 | | /// <summary>The environment name (e.g. <c>Development</c>, <c>Production</c>), if a valid one was supplied. |
| | | 70 | | /// When set, <see cref="ToWebApplicationOptions"/> makes it the host environment, overriding |
| | | 71 | | /// <c>ASPNETCORE_ENVIRONMENT</c>.</summary> |
| | 99 | 72 | | public string EnvironmentName { get; set; } = string.Empty; |
| | | 73 | | |
| | | 74 | | /// <summary>Whether <c>appsettings.json</c> should be loaded. Defaults to <c>true</c>. Parsed from the |
| | | 75 | | /// <c>UseAppSettings</c> argument (or its legacy spelling, <c>UseAppSetting</c>).</summary> |
| | 81 | 76 | | public bool UseAppSettings { get; set; } = true; |
| | | 77 | | |
| | | 78 | | /// <summary>Whether a <c>.env</c> file should be loaded. Defaults to <c>true</c>.</summary> |
| | 78 | 79 | | public bool UseEnvFile { get; set; } = true; |
| | | 80 | | |
| | | 81 | | /// <summary>Parsed from the <c>EnableSwaggerDocs</c> argument. When <c>false</c>, Swagger is neither generated |
| | | 82 | | /// nor served in any environment, whatever <see cref="SwaggerEnvironments"/> says (see |
| | | 83 | | /// <see cref="IsSwaggerAllowed"/>). Defaults to <c>true</c>, leaving Swagger gated by environment.</summary> |
| | 63 | 84 | | public bool EnableSwaggerDocs { get; set; } = true; |
| | | 85 | | |
| | | 86 | | /// <summary>The environment names in which Swagger should be enabled, as parsed from the <c>SwaggerEnvironments</c> |
| | 62 | 87 | | public string[] SwaggerEnvironments { get; set; } = []; |
| | | 88 | | |
| | | 89 | | /// <summary>Returns the configured Swagger environments, falling back to <c>Development</c> and <c>Local</c> |
| | | 90 | | /// when none were supplied or none are valid.</summary> |
| | | 91 | | public string[] GetSwaggerEnvironments() |
| | | 92 | | { |
| | 24 | 93 | | if (SwaggerEnvironments.IsEmpty()) |
| | | 94 | | { |
| | 23 | 95 | | return [.. DefaultSwaggerEnvironments]; |
| | | 96 | | } |
| | | 97 | | |
| | 3 | 98 | | var validEnvs = SwaggerEnvironments.Where(env => env.IsValidEnumValue<EnvironmentType>()).ToArray(); |
| | | 99 | | |
| | 1 | 100 | | return validEnvs.IsNotEmpty() ? validEnvs : [.. DefaultSwaggerEnvironments]; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | /// <summary>Whether Swagger is allowed in <paramref name="environmentName"/>. Never when the |
| | | 104 | | /// <c>EnableSwaggerDocs:false</c> argument was supplied; otherwise when the environment is in |
| | | 105 | | /// <paramref name="allowedEnvironments"/>, if non-empty, or else in <see cref="GetSwaggerEnvironments"/>.</summary> |
| | | 106 | | /// <param name="environmentName">The current host environment name.</param> |
| | | 107 | | /// <param name="allowedEnvironments">Optional explicit list of environments in which Swagger is allowed.</param> |
| | | 108 | | public bool IsSwaggerAllowed(string environmentName, EnvironmentType[]? allowedEnvironments = null) |
| | | 109 | | { |
| | 25 | 110 | | if (!EnableSwaggerDocs) |
| | | 111 | | { |
| | 2 | 112 | | return false; |
| | | 113 | | } |
| | | 114 | | |
| | 23 | 115 | | var environments = allowedEnvironments.IsNotEmpty() |
| | 2 | 116 | | ? allowedEnvironments!.Select(env => env.ToString()) |
| | 23 | 117 | | : GetSwaggerEnvironments(); |
| | | 118 | | |
| | 23 | 119 | | return environments.Contains(environmentName, StringComparer.OrdinalIgnoreCase); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | /// <summary>Builds the <see cref="WebApplicationOptions"/> for <c>WebApplication.CreateBuilder</c>: the |
| | | 123 | | /// original <see cref="Args"/>, and <see cref="EnvironmentName"/> as the environment when one was supplied.</summar |
| | | 124 | | public WebApplicationOptions ToWebApplicationOptions() => |
| | 22 | 125 | | new() |
| | 22 | 126 | | { |
| | 22 | 127 | | Args = Args, |
| | 22 | 128 | | EnvironmentName = string.IsNullOrWhiteSpace(EnvironmentName) ? null : EnvironmentName |
| | 22 | 129 | | }; |
| | | 130 | | } |