Adds Configuration Manager + tests

This commit is contained in:
Janus C. H. Knudsen 2025-01-26 22:57:27 +01:00
parent 55e65a1b21
commit 384cc3c6fd
16 changed files with 657 additions and 137 deletions

View file

@ -0,0 +1,14 @@
namespace Configuration.Core;
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; }
}

View file

@ -0,0 +1,28 @@
using Configuration.Core;
using Microsoft.Extensions.Configuration;
namespace Core.Configurations.ConfigurationManager
{
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");
}
}
}

View file

@ -0,0 +1,24 @@
using System.Data;
using Insight.Database;
namespace Configuration.Core;
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);
}
}

View file

@ -0,0 +1,5 @@
namespace Configuration.Core;
public interface IConfigurationRepository
{
Task<IEnumerable<AppConfiguration>> GetActiveConfigurations();
}

View file

@ -0,0 +1,31 @@
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System.Data;
namespace Configuration.Core;
public class JsonConfiguration : IConfiguration
{
private readonly JObject _data;
private readonly IChangeToken _changeToken;
public JsonConfiguration(JObject data, IChangeToken changeToken)
{
_data = data;
_changeToken = changeToken;
}
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() => _changeToken;
}

View file

@ -0,0 +1,56 @@
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Primitives;
using System.Data;
namespace Configuration.Core;
public class JsonConfigurationSection : IConfigurationSection
{
private readonly JObject _data;
private readonly string _path;
public JsonConfigurationSection(JObject data, string path)
{
_data = data;
_path = path;
}
public string this[string key]
{
get => _data.SelectToken($"{_path.Replace(":", ".")}.{key.Replace(":", ".")}")?.ToString();
set => throw new NotImplementedException();
}
public string Key => _path.Split(':').Last();
public string Path => _path;
public string Value
{
get => _data.SelectToken(_path.Replace(":", "."))?.ToString();
set => throw new NotImplementedException();
}
public IConfigurationSection GetSection(string key) =>
new JsonConfigurationSection(_data, string.IsNullOrEmpty(_path) ? key : $"{_path}:{key}");
public JToken GetToken() => _data.SelectToken(_path.Replace(":", "."));
public IEnumerable<IConfigurationSection> GetChildren()
{
var token = _data.SelectToken(_path.Replace(":", "."));
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(_path.Replace(":", "."));
return token?.ToObject<T>();
}
public IChangeToken GetReloadToken() => new ConfigurationReloadToken();
}

View file

@ -0,0 +1,56 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Extensions.Configuration;
namespace Configuration.Core;
public class KeyValueConfigurationBuilder
{
private readonly IConfigurationRepository _repository;
private readonly JObject _rootObject = new();
private ConfigurationReloadToken _reloadToken = new();
private IConfiguration _configuration;
public KeyValueConfigurationBuilder(IConfigurationRepository repository)
{
_repository = repository;
}
public async Task LoadConfiguration()
{
var configurations = await _repository.GetActiveConfigurations();
foreach (var config in configurations)
{
AddKeyValue(config.Key, config.Value);
}
OnReload();
}
public void AddKeyValue(string key, string jsonValue)
{
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;
}
private void OnReload()
{
var previousToken = Interlocked.Exchange(ref _reloadToken, new ConfigurationReloadToken());
previousToken.OnReload();
_configuration = null;
}
public IConfiguration Build() =>
_configuration ??= new JsonConfiguration(_rootObject, _reloadToken);
}