| | | 1 | | using ArturRios.Configuration.Enums; |
| | | 2 | | using ArturRios.Extensions; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Util.WebApi.Configuration; |
| | | 5 | | |
| | | 6 | | /// <summary>Startup parameters parsed from command-line arguments (e.g. <c>Environment:Production</c>, |
| | | 7 | | /// <c>EnableSwaggerDocs:false</c>), controlling how <see cref="WebApiStartup"/> configures the application.</summary> |
| | | 8 | | public class WebApiParameters |
| | | 9 | | { |
| | 0 | 10 | | private readonly string[] _defaultSwaggerEnvironments = |
| | 0 | 11 | | [nameof(EnvironmentType.Development), nameof(EnvironmentType.Local)]; |
| | | 12 | | |
| | | 13 | | /// <summary>Parses <paramref name="args"/> into the corresponding properties. Unrecognized or malformed |
| | | 14 | | /// entries are ignored, leaving the default values in place.</summary> |
| | | 15 | | /// <param name="args">The command-line arguments, each in <c>Key:Value</c> form.</param> |
| | 0 | 16 | | public WebApiParameters(string[] args) |
| | 0 | 17 | | { |
| | 0 | 18 | | if (args.IsEmpty()) |
| | 0 | 19 | | { |
| | 0 | 20 | | return; |
| | | 21 | | } |
| | | 22 | | |
| | 0 | 23 | | foreach (var arg in args) |
| | 0 | 24 | | { |
| | 0 | 25 | | var parts = arg.Split(':', 2, StringSplitOptions.TrimEntries); |
| | | 26 | | |
| | 0 | 27 | | if (parts.Length != 2) |
| | 0 | 28 | | { |
| | 0 | 29 | | continue; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | var key = parts[0].Trim(); |
| | 0 | 33 | | var value = parts[1].Trim(); |
| | | 34 | | |
| | 0 | 35 | | switch (key) |
| | | 36 | | { |
| | | 37 | | case "Environment": |
| | 0 | 38 | | EnvironmentName = value.IsValidEnumValue<EnvironmentType>() ? value : string.Empty; |
| | 0 | 39 | | break; |
| | | 40 | | case "EnableSwaggerDocs": |
| | 0 | 41 | | EnableSwaggerDocs = value.ParseToBoolOrDefault(true)!.Value; |
| | 0 | 42 | | break; |
| | | 43 | | case "UseAppSetting": |
| | 0 | 44 | | UseAppSettings = value.ParseToBoolOrDefault(true)!.Value; |
| | 0 | 45 | | break; |
| | | 46 | | case "UseEnvFile": |
| | 0 | 47 | | UseEnvFile = value.ParseToBoolOrDefault(true)!.Value; |
| | 0 | 48 | | break; |
| | | 49 | | case "SwaggerEnvironments": |
| | 0 | 50 | | if (value.StartsWith('[') && value.EndsWith(']')) |
| | 0 | 51 | | { |
| | 0 | 52 | | var envs = value.Trim('[', ']').Split( |
| | 0 | 53 | | ',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| | 0 | 54 | | SwaggerEnvironments = envs; |
| | 0 | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | break; |
| | | 58 | | } |
| | 0 | 59 | | } |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary>The environment name (e.g. <c>Development</c>, <c>Production</c>), if a valid one was supplied.</summar |
| | 0 | 63 | | public string EnvironmentName { get; set; } = string.Empty; |
| | | 64 | | |
| | | 65 | | /// <summary>Whether <c>appsettings.json</c> should be loaded. Defaults to <c>true</c>.</summary> |
| | 0 | 66 | | public bool UseAppSettings { get; set; } = true; |
| | | 67 | | |
| | | 68 | | /// <summary>Whether a <c>.env</c> file should be loaded. Defaults to <c>true</c>.</summary> |
| | 0 | 69 | | public bool UseEnvFile { get; set; } = true; |
| | | 70 | | |
| | | 71 | | /// <summary>Parsed from the <c>EnableSwaggerDocs</c> argument. Reserved and not currently consulted by |
| | | 72 | | /// <see cref="WebApiStartup"/>; Swagger is gated by environment (see <see cref="SwaggerEnvironments"/> and |
| | | 73 | | /// <see cref="GetSwaggerEnvironments"/>). Defaults to <c>true</c>.</summary> |
| | 0 | 74 | | public bool EnableSwaggerDocs { get; set; } = true; |
| | | 75 | | |
| | | 76 | | /// <summary>The environment names in which Swagger should be enabled, as parsed from the <c>SwaggerEnvironments</c> |
| | 0 | 77 | | public string[] SwaggerEnvironments { get; set; } = []; |
| | | 78 | | |
| | | 79 | | /// <summary>Returns the configured Swagger environments, falling back to <c>Development</c> and <c>Local</c> |
| | | 80 | | /// when none were supplied or none are valid.</summary> |
| | | 81 | | public string[] GetSwaggerEnvironments() |
| | 0 | 82 | | { |
| | 0 | 83 | | if (SwaggerEnvironments.IsEmpty()) |
| | 0 | 84 | | { |
| | 0 | 85 | | return _defaultSwaggerEnvironments; |
| | | 86 | | } |
| | | 87 | | |
| | 0 | 88 | | var validEnvs = SwaggerEnvironments.Where(env => env.IsValidEnumValue<EnvironmentType>()).ToArray(); |
| | | 89 | | |
| | 0 | 90 | | return validEnvs.IsNotEmpty() ? validEnvs : _defaultSwaggerEnvironments; |
| | 0 | 91 | | } |
| | | 92 | | } |