WIP on Config Handling
This commit is contained in:
parent
4dc03f2cbf
commit
8e6492e979
17 changed files with 314 additions and 288 deletions
14
Core/Configurations/SmartConfiguration/AppConfiguration.cs
Normal file
14
Core/Configurations/SmartConfiguration/AppConfiguration.cs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
namespace Core.Configurations.SmartConfiguration;
|
||||
public class AppConfiguration
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public string Key { get; set; }
|
||||
public string Value { get; set; }
|
||||
public string Label { get; set; }
|
||||
public string ContentType { get; set; }
|
||||
public DateTime? ValidFrom { get; set; }
|
||||
public DateTime? ExpiresAt { get; set; }
|
||||
public DateTime? CreatedAt { get; set; }
|
||||
public DateTime? ModifiedAt { get; set; }
|
||||
public Guid? Etag { get; set; }
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Core.Configurations.SmartConfiguration
|
||||
{
|
||||
public static class ConfigurationExtensions
|
||||
{
|
||||
public static T Get<T>(this IConfigurationSection section) where T : class
|
||||
{
|
||||
if (section is JsonConfigurationSection jsonSection)
|
||||
{
|
||||
var token = jsonSection.GetToken();
|
||||
return token?.ToObject<T>();
|
||||
}
|
||||
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
||||
}
|
||||
|
||||
public static T GetValue<T>(this IConfigurationSection section, string key)
|
||||
{
|
||||
if (section is JsonConfigurationSection jsonSection)
|
||||
{
|
||||
var token = jsonSection.GetToken().SelectToken(key.Replace(":", "."));
|
||||
return token.ToObject<T>();
|
||||
}
|
||||
throw new InvalidOperationException("Section is not a JsonConfigurationSection");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
using System.Data;
|
||||
using Insight.Database;
|
||||
|
||||
namespace Core.Configurations.SmartConfiguration;
|
||||
public class ConfigurationRepository : IConfigurationRepository
|
||||
{
|
||||
private readonly IDbConnection _connection;
|
||||
|
||||
public ConfigurationRepository(IDbConnection connection)
|
||||
{
|
||||
_connection = connection;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<AppConfiguration>> GetActiveConfigurations()
|
||||
{
|
||||
const string sql = @"
|
||||
SELECT id, key, value, label, content_type, valid_from, expires_at, created_at, modified_at, etag
|
||||
FROM prod.app_configuration
|
||||
WHERE (expires_at IS NULL OR expires_at > CURRENT_TIMESTAMP)
|
||||
AND (valid_from IS NULL OR valid_from < CURRENT_TIMESTAMP)";
|
||||
|
||||
return await _connection.QueryAsync<AppConfiguration>(sql);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
namespace Core.Configurations.SmartConfiguration;
|
||||
public interface IConfigurationRepository
|
||||
{
|
||||
Task<IEnumerable<AppConfiguration>> GetActiveConfigurations();
|
||||
}
|
||||
29
Core/Configurations/SmartConfiguration/JsonConfiguration.cs
Normal file
29
Core/Configurations/SmartConfiguration/JsonConfiguration.cs
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
using System.Data;
|
||||
|
||||
namespace Core.Configurations.SmartConfiguration;
|
||||
public class JsonConfiguration : IConfiguration
|
||||
{
|
||||
private readonly JObject _data;
|
||||
|
||||
public JsonConfiguration(JObject data)
|
||||
{
|
||||
_data = data;
|
||||
}
|
||||
|
||||
public string this[string key]
|
||||
{
|
||||
get => _data.SelectToken(key.Replace(":", "."))?.ToString();
|
||||
set => throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public IConfigurationSection GetSection(string key) =>
|
||||
new JsonConfigurationSection(_data, key);
|
||||
|
||||
public IEnumerable<IConfigurationSection> GetChildren() =>
|
||||
_data.Properties().Select(p => new JsonConfigurationSection(_data, p.Name));
|
||||
|
||||
public IChangeToken GetReloadToken() => throw new NotImplementedException();
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Primitives;
|
||||
|
||||
namespace Core.Configurations.SmartConfiguration
|
||||
{
|
||||
public class JsonConfigurationSection : IConfigurationSection
|
||||
{
|
||||
private readonly JObject _data;
|
||||
private readonly string _path;
|
||||
private readonly string _normalizedPath;
|
||||
|
||||
public JsonConfigurationSection(JObject data, string path)
|
||||
{
|
||||
_data = data ?? throw new ArgumentNullException(nameof(data));
|
||||
_path = path ?? throw new ArgumentNullException(nameof(path));
|
||||
_normalizedPath = NormalizePath(_path);
|
||||
}
|
||||
|
||||
public string this[string key]
|
||||
{
|
||||
get
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
var token = _data.SelectToken($"{_normalizedPath}.{NormalizePath(key)}");
|
||||
return token?.ToString();
|
||||
}
|
||||
set => throw new NotImplementedException("Setting values is not supported.");
|
||||
}
|
||||
|
||||
public string Key => _path.Split(':').Last();
|
||||
public string Path => _path;
|
||||
|
||||
public string Value
|
||||
{
|
||||
get
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
return token?.ToString();
|
||||
}
|
||||
set => throw new NotImplementedException("Setting values is not supported.");
|
||||
}
|
||||
|
||||
public IConfigurationSection GetSection(string key)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
|
||||
return new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
|
||||
}
|
||||
|
||||
public JToken GetToken() => _data.SelectToken(_normalizedPath);
|
||||
|
||||
public IEnumerable<IConfigurationSection> GetChildren()
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
if (token is JObject obj)
|
||||
{
|
||||
return obj.Properties()
|
||||
.Select(p => new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? p.Name : $"{_path}:{p.Name}"));
|
||||
}
|
||||
return Enumerable.Empty<IConfigurationSection>();
|
||||
}
|
||||
|
||||
public T Get<T>() where T : class
|
||||
{
|
||||
var token = _data.SelectToken(_normalizedPath);
|
||||
return token?.ToObject<T>();
|
||||
}
|
||||
|
||||
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
|
||||
|
||||
private static string NormalizePath(string path)
|
||||
{
|
||||
return path?.Replace(":", ".", StringComparison.Ordinal) ?? string.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Core.Configurations.SmartConfiguration
|
||||
{
|
||||
public class KeyValueConfigurationBuilder
|
||||
{
|
||||
private readonly IConfigurationRepository _repository;
|
||||
private readonly JObject _rootObject = new();
|
||||
private IConfiguration _configuration;
|
||||
private readonly object _configurationLock = new();
|
||||
|
||||
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
|
||||
{
|
||||
_repository = repository ?? throw new ArgumentNullException(nameof(repository));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads configurations from the repository and builds the configuration tree.
|
||||
/// </summary>
|
||||
public async Task LoadConfiguration()
|
||||
{
|
||||
try
|
||||
{
|
||||
var configurations = await _repository.GetActiveConfigurations();
|
||||
foreach (var config in configurations)
|
||||
AddKeyValue(config.Key, config.Value);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log the exception or handle it as needed
|
||||
throw new InvalidOperationException("Failed to load configurations.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a key-value pair to the configuration tree.
|
||||
/// </summary>
|
||||
/// <param name="key">The key to add.</param>
|
||||
/// <param name="jsonValue">The JSON value to add.</param>
|
||||
public void AddKeyValue(string key, string jsonValue)
|
||||
{
|
||||
if (string.IsNullOrEmpty(key))
|
||||
throw new ArgumentNullException(nameof(key));
|
||||
if (string.IsNullOrEmpty(jsonValue))
|
||||
throw new ArgumentNullException(nameof(jsonValue));
|
||||
|
||||
try
|
||||
{
|
||||
var valueObject = JsonConvert.DeserializeObject<JObject>(jsonValue);
|
||||
var parts = key.Split(':');
|
||||
|
||||
JObject current = _rootObject;
|
||||
for (int i = 0; i < parts.Length - 1; i++)
|
||||
{
|
||||
var part = parts[i];
|
||||
if (!current.ContainsKey(part))
|
||||
{
|
||||
current[part] = new JObject();
|
||||
}
|
||||
current = (JObject)current[part];
|
||||
}
|
||||
|
||||
current[parts[^1]] = valueObject;
|
||||
}
|
||||
catch (JsonException ex)
|
||||
{
|
||||
throw new ArgumentException("Invalid JSON value.", nameof(jsonValue), ex);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Builds the configuration instance.
|
||||
/// </summary>
|
||||
/// <returns>The built <see cref="IConfiguration"/> instance.</returns>
|
||||
public IConfiguration Build()
|
||||
{
|
||||
_configuration = new JsonConfiguration(_rootObject);
|
||||
return _configuration;
|
||||
}
|
||||
}
|
||||
}
|
||||
48
Core/Configurations/SmartConfiguration/SmartConfigManager.cs
Normal file
48
Core/Configurations/SmartConfiguration/SmartConfigManager.cs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
|
||||
namespace Core.Configurations
|
||||
{
|
||||
public class SmartConfigManager
|
||||
{
|
||||
private static IConfigurationBuilder _configurationBuilder;
|
||||
|
||||
/// <summary>
|
||||
/// This AppConfigBuilder assumes that AppConfigEndpoint and AppConfigLabelFilter are configured as Settings on Azure for the Application that needs them.
|
||||
/// AppConfigEndpoint would look like this: Endpoint=https://config-dec-test.azconfig.io;Id=0-l9-s0:foo;Secret=somesecret/bar
|
||||
/// </summary>
|
||||
/// <param name="localSettingsFile">
|
||||
/// Path relative to the base path stored in Microsoft.Extensions.Configuration.IConfigurationBuilder.Properties of builder.
|
||||
/// </param>
|
||||
public static IConfigurationBuilder AddSmartConfigBuilder(string localSettingsFile)
|
||||
{
|
||||
if (_configurationBuilder == null)
|
||||
{
|
||||
var envConfiguration = new ConfigurationBuilder()
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
|
||||
var appConfigEndpoint = envConfiguration["AppConfigEndpoint"];
|
||||
var appConfigLabel = envConfiguration["AppConfigLabelFilter"];
|
||||
|
||||
_configurationBuilder = new ConfigurationBuilder();
|
||||
if (!string.IsNullOrEmpty(appConfigEndpoint))
|
||||
{
|
||||
_configurationBuilder
|
||||
.AddAzureAppConfiguration(options =>
|
||||
{
|
||||
options.Connect(appConfigEndpoint);
|
||||
options.Select(keyFilter: "*", labelFilter: appConfigLabel);
|
||||
})
|
||||
.AddEnvironmentVariables();
|
||||
}
|
||||
else
|
||||
{
|
||||
_configurationBuilder.SetBasePath(Directory.GetCurrentDirectory());
|
||||
_configurationBuilder.AddJsonFile(localSettingsFile, optional: false);
|
||||
}
|
||||
}
|
||||
return _configurationBuilder;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue