Files
messengerapi/code/MessengerApi.Configuration/Model/MessengerConfiguration.cs
masiton 4393977389
All checks were successful
Build and Push Docker Image / build (push) Successful in 1m3s
Build and Push Docker Image / docker (push) Successful in 43s
Initial commit carried over from private repo. This is V2.
2025-07-04 21:24:12 +02:00

98 lines
4.0 KiB
C#

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);
}
}
}
}
}