|
| 1 | +using G4.Converters; |
| 2 | + |
| 3 | +using Microsoft.Extensions.Configuration; |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.IO; |
| 8 | +using System.Text; |
| 9 | +using System.Text.Json; |
| 10 | +using System.Text.Json.Serialization; |
| 11 | + |
| 12 | +namespace ChromiumPeek.Settings |
| 13 | +{ |
| 14 | + /// <summary> |
| 15 | + /// Represents the application settings including configuration, JSON options, and LiteDB connection. |
| 16 | + /// </summary> |
| 17 | + public static class AppSettings |
| 18 | + { |
| 19 | + #region *** Constants *** |
| 20 | + /// <summary> |
| 21 | + /// The version of the API. |
| 22 | + /// </summary> |
| 23 | + public const string ApiVersion = "4"; |
| 24 | + #endregion |
| 25 | + |
| 26 | + #region *** Fields *** |
| 27 | + /// <summary> |
| 28 | + /// Gets the application configuration. |
| 29 | + /// </summary> |
| 30 | + public static readonly IConfigurationRoot Configuration = NewConfiguraion(); |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Gets the JSON serialization options. |
| 34 | + /// </summary> |
| 35 | + public static readonly JsonSerializerOptions JsonOptions = NewJsonOptions(); |
| 36 | + #endregion |
| 37 | + |
| 38 | + #region *** Methods *** |
| 39 | + // Creates a new instance of IConfigurationRoot by configuring it with settings from appsettings.json and environment variables. |
| 40 | + private static IConfigurationRoot NewConfiguraion() |
| 41 | + { |
| 42 | + // Create a new ConfigurationBuilder instance |
| 43 | + new ConfigurationBuilder() |
| 44 | + .SetBasePath(Directory.GetCurrentDirectory()) |
| 45 | + .AddJsonFile(path: "appsettings.json", optional: true, reloadOnChange: false) |
| 46 | + .AddEnvironmentVariables() |
| 47 | + .Build(); |
| 48 | + |
| 49 | + // Create a new ConfigurationBuilder instance |
| 50 | + var configurationBuilder = new ConfigurationBuilder(); |
| 51 | + |
| 52 | + // Set the base path for the configuration file to the current directory |
| 53 | + configurationBuilder.SetBasePath(Directory.GetCurrentDirectory()); |
| 54 | + |
| 55 | + // Add the appsettings.json file as a configuration source, if it exists (optional), without reloading it on change |
| 56 | + configurationBuilder.AddJsonFile(path: "appsettings.json", optional: true, reloadOnChange: false); |
| 57 | + |
| 58 | + // Add environment variables as a configuration source |
| 59 | + configurationBuilder.AddEnvironmentVariables(); |
| 60 | + |
| 61 | + // Build and return the IConfigurationRoot instance |
| 62 | + return configurationBuilder.Build(); |
| 63 | + } |
| 64 | + |
| 65 | + // Creates a new instance of JsonSerializerOptions with custom settings and converters. |
| 66 | + private static JsonSerializerOptions NewJsonOptions() |
| 67 | + { |
| 68 | + // Initialize JSON serialization options. |
| 69 | + var jsonOptions = new JsonSerializerOptions() |
| 70 | + { |
| 71 | + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, |
| 72 | + PropertyNameCaseInsensitive = true, |
| 73 | + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, |
| 74 | + WriteIndented = false |
| 75 | + }; |
| 76 | + |
| 77 | + // Add a custom exception converter |
| 78 | + jsonOptions.Converters.Add(new ExceptionConverter()); |
| 79 | + |
| 80 | + // Add a custom method base converter |
| 81 | + jsonOptions.Converters.Add(new MethodBaseConverter()); |
| 82 | + |
| 83 | + // Add a custom type converter |
| 84 | + jsonOptions.Converters.Add(new TypeConverter()); |
| 85 | + |
| 86 | + // Add a custom DateTime converter for ISO 8601 format (yyyy-MM-ddTHH:mm:ss.ffffffK) |
| 87 | + jsonOptions.Converters.Add(new DateTimeIso8601Converter()); |
| 88 | + |
| 89 | + // Return the JSON options with custom settings and converters added |
| 90 | + return jsonOptions; |
| 91 | + } |
| 92 | + |
| 93 | + // Retrieves a value from an environment variable and converts it to the specified type <typeparamref name="T"/>. |
| 94 | + // If the environment variable is not found, empty, or cannot be converted, returns the provided default value. |
| 95 | + private static T GetOrDefault<T>(string environmentParameter, T defaultValue) |
| 96 | + { |
| 97 | + // Attempt to read the environment variable value |
| 98 | + var envValue = Environment.GetEnvironmentVariable(environmentParameter); |
| 99 | + |
| 100 | + // If the environment variable is missing or blank, use the default value |
| 101 | + if (string.IsNullOrWhiteSpace(envValue)) |
| 102 | + { |
| 103 | + return defaultValue; |
| 104 | + } |
| 105 | + |
| 106 | + try |
| 107 | + { |
| 108 | + // Check if T is a nullable type and get the underlying type |
| 109 | + var underlyingType = Nullable.GetUnderlyingType(typeof(T)); |
| 110 | + var targetType = underlyingType ?? typeof(T); |
| 111 | + |
| 112 | + // Special handling for booleans to support "true", "false", "1", and "0" |
| 113 | + if (targetType == typeof(bool)) |
| 114 | + { |
| 115 | + if (bool.TryParse(envValue, out bool boolResult)) |
| 116 | + { |
| 117 | + return (T)(object)boolResult; |
| 118 | + } |
| 119 | + |
| 120 | + // Support numeric boolean representation |
| 121 | + if (envValue.Trim() == "1") return (T)(object)true; |
| 122 | + if (envValue.Trim() == "0") return (T)(object)false; |
| 123 | + } |
| 124 | + |
| 125 | + // Attempt to convert the string value to the target type using system conversion |
| 126 | + return (T)Convert.ChangeType(envValue.Trim(), targetType); |
| 127 | + } |
| 128 | + catch |
| 129 | + { |
| 130 | + // If conversion fails (e.g., invalid format), fall back to the default value |
| 131 | + return defaultValue; |
| 132 | + } |
| 133 | + } |
| 134 | + #endregion |
| 135 | + } |
| 136 | +} |
0 commit comments