Initial commit carried over from private repo. This is V2.
This commit is contained in:
@ -0,0 +1,9 @@
|
||||
namespace MessengerApi.Configuration.Enums
|
||||
{
|
||||
public enum HousekeepingMessageStates
|
||||
{
|
||||
None,
|
||||
Delivered,
|
||||
Acknowledged
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
namespace MessengerApi.Configuration.Enums
|
||||
{
|
||||
public enum LoggingVerbosity
|
||||
{
|
||||
Normal,
|
||||
Debug,
|
||||
Trace
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,8 @@
|
||||
namespace MessengerApi.Configuration.Enums
|
||||
{
|
||||
public enum PersistenceTypes
|
||||
{
|
||||
Sql,
|
||||
PostgreSql
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>disable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@ -0,0 +1,98 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
using MessengerApi.Configuration.Model.Persistence.Base;
|
||||
using MessengerApi.Configuration.Parsers;
|
||||
using MessengerApi.Configuration.Sources.Environment;
|
||||
using Env = MessengerApi.Configuration.Constants.EnvironmentVariables;
|
||||
|
||||
namespace MessengerApi.Configuration.Model
|
||||
{
|
||||
public class MessengerConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// CORS origins.
|
||||
/// </summary>
|
||||
public string[] Origins { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// List of proxies that are trusted to provide forwarding headers.
|
||||
/// </summary>
|
||||
public string[] Proxies { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Persistence layer configs (database).
|
||||
/// </summary>
|
||||
public PersistenceConfiguration PersistenceConfiguration { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Limits rate of user calls to not DoS the service.
|
||||
/// </summary>
|
||||
public int RateLimitPerMinute { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Message lifetime unless set differently in message body.
|
||||
/// </summary>
|
||||
public int DefaultMessageLifetimeInMinutes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// If true, messages are periodically wiped to free up space.
|
||||
/// </summary>
|
||||
public bool HousekeepingEnabled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Messages older than value set will be deleted regardless of their delivery state.
|
||||
/// </summary>
|
||||
public int HousekeepingMessageAgeInMinutes { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Determines level of log messages displayed.
|
||||
/// </summary>
|
||||
public LoggingVerbosity Verbosity { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// In addition to <see cref="HousekeepingMessageAgeInMinutes"/> messages of certain state can also be deleted, increasing storage efficiency.
|
||||
/// </summary>
|
||||
public HousekeepingMessageStates HousekeepingMessageState { get; set; }
|
||||
|
||||
public MessengerConfiguration() { }
|
||||
|
||||
public MessengerConfiguration(string[] origins, PersistenceConfiguration persistenceConfiguration)
|
||||
{
|
||||
if(persistenceConfiguration == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(persistenceConfiguration));
|
||||
}
|
||||
|
||||
this.PersistenceConfiguration = persistenceConfiguration;
|
||||
this.Origins = origins ?? [];
|
||||
this.Proxies = [];
|
||||
this.RateLimitPerMinute = 120;
|
||||
this.DefaultMessageLifetimeInMinutes = 1;
|
||||
this.HousekeepingEnabled = true;
|
||||
this.HousekeepingMessageAgeInMinutes = 120;
|
||||
this.HousekeepingMessageState = HousekeepingMessageStates.None;
|
||||
this.Verbosity = LoggingVerbosity.Normal;
|
||||
}
|
||||
|
||||
public MessengerConfiguration(IEnvironmentConfigurationSource config) : this(
|
||||
CorsParser.Parse(config.GetValue<string>(Env.CORS_ORIGINS)),
|
||||
EnvironmentPersistenceConfigurationParser.Parse(config))
|
||||
{
|
||||
Populate<string>(config, Env.PROXIES, x => this.Proxies = ProxiesParser.Parse(x));
|
||||
Populate<int>(config, Env.QUERY_RATE_PER_MINUTE, x => this.RateLimitPerMinute = x);
|
||||
Populate<int>(config, Env.DEFAULT_MESSAGE_LIFETIME_IN_MINUTES, x => this.DefaultMessageLifetimeInMinutes = x);
|
||||
Populate<bool>(config, Env.HOUSEKEEPING_ENABLED, x => this.HousekeepingEnabled = x);
|
||||
Populate<int>(config, Env.HOUSEKEEPING_MESSAGE_AGE_IN_MINUTES, x => this.HousekeepingMessageAgeInMinutes = x);
|
||||
Populate<string>(config, Env.HOUSEKEEPING_MESSAGE_STATE, x => this.HousekeepingMessageState = HousekeepingMessageStateParser.Parse(x));
|
||||
Populate<string>(config, Env.LOGGING_VERBOSITY, x => this.Verbosity = LoggingVerbosityParser.Parse(x));
|
||||
|
||||
void Populate<T>(IEnvironmentConfigurationSource config, string key, Action<T> set)
|
||||
{
|
||||
if (config.HasKey(key))
|
||||
{
|
||||
var value = config.GetValue<T>(key);
|
||||
set(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
|
||||
namespace MessengerApi.Configuration.Model.Persistence.Base
|
||||
{
|
||||
public abstract class PersistenceConfiguration
|
||||
{
|
||||
public abstract PersistenceTypes PersistenceType { get; }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
using MessengerApi.Configuration.Model.Persistence.Base;
|
||||
using MessengerApi.Configuration.Sources.Environment;
|
||||
|
||||
namespace MessengerApi.Configuration.Model.Persistence
|
||||
{
|
||||
public class NpgPersistenceConfiguration : PersistenceConfiguration
|
||||
{
|
||||
public override PersistenceTypes PersistenceType => PersistenceTypes.PostgreSql;
|
||||
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public NpgPersistenceConfiguration(string connectionString)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public NpgPersistenceConfiguration(IEnvironmentConfigurationSource config) : this(config.GetValue<string>(Constants.EnvironmentVariables.NPG_CONNECTIONSTRING)) { }
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
using MessengerApi.Configuration.Model.Persistence.Base;
|
||||
using MessengerApi.Configuration.Sources.Environment;
|
||||
|
||||
namespace MessengerApi.Configuration.Model.Persistence
|
||||
{
|
||||
public class SqlPersistenceConfiguration : PersistenceConfiguration
|
||||
{
|
||||
public override PersistenceTypes PersistenceType => PersistenceTypes.Sql;
|
||||
|
||||
public string ConnectionString { get; }
|
||||
|
||||
public SqlPersistenceConfiguration(string connectionString)
|
||||
{
|
||||
ConnectionString = connectionString;
|
||||
}
|
||||
|
||||
public SqlPersistenceConfiguration(IEnvironmentConfigurationSource config) : this(config.GetValue<string>(Constants.EnvironmentVariables.SQL_CONNECTIONSTRING)) { }
|
||||
}
|
||||
}
|
||||
11
code/MessengerApi.Configuration/Parsers/CorsParser.cs
Normal file
11
code/MessengerApi.Configuration/Parsers/CorsParser.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class CorsParser
|
||||
{
|
||||
public static string[] Parse(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) return [];
|
||||
return value.Trim().Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,25 @@
|
||||
using MessengerApi.Configuration.Model.Persistence;
|
||||
using MessengerApi.Configuration.Model.Persistence.Base;
|
||||
using MessengerApi.Configuration.Sources.Environment;
|
||||
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class EnvironmentPersistenceConfigurationParser
|
||||
{
|
||||
public static PersistenceConfiguration Parse(IEnvironmentConfigurationSource config)
|
||||
{
|
||||
var type = PersistenceTypeParser.Parse(config.GetValue<string>(Constants.EnvironmentVariables.PERSISTENCE_TYPE));
|
||||
|
||||
if(type == Enums.PersistenceTypes.Sql)
|
||||
{
|
||||
return new SqlPersistenceConfiguration(config);
|
||||
}
|
||||
else if(type == Enums.PersistenceTypes.PostgreSql)
|
||||
{
|
||||
return new NpgPersistenceConfiguration(config);
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Unrecognized persistence type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class HousekeepingMessageStateParser
|
||||
{
|
||||
public static HousekeepingMessageStates Parse(string input)
|
||||
{
|
||||
return (HousekeepingMessageStates)Enum.Parse(typeof(HousekeepingMessageStates), input.Trim(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class LoggingVerbosityParser
|
||||
{
|
||||
public static LoggingVerbosity Parse(string value)
|
||||
{
|
||||
return (LoggingVerbosity)Enum.Parse(typeof(LoggingVerbosity), value.Trim(), true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,12 @@
|
||||
using MessengerApi.Configuration.Enums;
|
||||
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class PersistenceTypeParser
|
||||
{
|
||||
public static PersistenceTypes Parse(string value)
|
||||
{
|
||||
return (PersistenceTypes)Enum.Parse(typeof(PersistenceTypes), value, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
code/MessengerApi.Configuration/Parsers/ProxiesParser.cs
Normal file
11
code/MessengerApi.Configuration/Parsers/ProxiesParser.cs
Normal file
@ -0,0 +1,11 @@
|
||||
namespace MessengerApi.Configuration.Parsers
|
||||
{
|
||||
public static class ProxiesParser
|
||||
{
|
||||
public static string[] Parse(string value)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(value)) return [];
|
||||
return value.Trim().Split(",", StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,20 @@
|
||||
namespace MessengerApi.Configuration
|
||||
{
|
||||
public static partial class Constants
|
||||
{
|
||||
public static class EnvironmentVariables
|
||||
{
|
||||
public const string SQL_CONNECTIONSTRING = nameof(SQL_CONNECTIONSTRING);
|
||||
public const string NPG_CONNECTIONSTRING = nameof(NPG_CONNECTIONSTRING);
|
||||
public const string PERSISTENCE_TYPE = nameof(PERSISTENCE_TYPE);
|
||||
public const string CORS_ORIGINS = nameof(CORS_ORIGINS);
|
||||
public const string PROXIES = nameof(PROXIES);
|
||||
public const string QUERY_RATE_PER_MINUTE = nameof(QUERY_RATE_PER_MINUTE);
|
||||
public const string DEFAULT_MESSAGE_LIFETIME_IN_MINUTES = nameof(DEFAULT_MESSAGE_LIFETIME_IN_MINUTES);
|
||||
public const string HOUSEKEEPING_ENABLED = nameof(HOUSEKEEPING_ENABLED);
|
||||
public const string HOUSEKEEPING_MESSAGE_AGE_IN_MINUTES = nameof(HOUSEKEEPING_MESSAGE_AGE_IN_MINUTES);
|
||||
public const string HOUSEKEEPING_MESSAGE_STATE = nameof(HOUSEKEEPING_MESSAGE_STATE);
|
||||
public const string LOGGING_VERBOSITY = nameof(LOGGING_VERBOSITY);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,15 @@
|
||||
namespace MessengerApi.Configuration.Sources.Environment
|
||||
{
|
||||
public class EnvironmentConfigurationSource : IEnvironmentConfigurationSource
|
||||
{
|
||||
public bool HasKey(string key)
|
||||
{
|
||||
return !string.IsNullOrWhiteSpace(System.Environment.GetEnvironmentVariable(key));
|
||||
}
|
||||
|
||||
public T GetValue<T>(string key)
|
||||
{
|
||||
return (T)Convert.ChangeType(System.Environment.GetEnvironmentVariable(key), typeof(T));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,6 @@
|
||||
namespace MessengerApi.Configuration.Sources.Environment
|
||||
{
|
||||
public interface IEnvironmentConfigurationSource : IConfigurationSource
|
||||
{
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,9 @@
|
||||
namespace MessengerApi.Configuration.Sources
|
||||
{
|
||||
public interface IConfigurationSource
|
||||
{
|
||||
bool HasKey(string key);
|
||||
|
||||
T GetValue<T>(string key);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user