Files
messengerapi/code/MessengerApi.Configuration/Model/MessengerConfiguration.cs
masiton a44912ac87
All checks were successful
Build and Push Docker Image / build (push) Successful in 55s
Build and Push Docker Image / docker (push) Successful in 36s
payloadLifespan/payloadLifetime and other name variants unified to Time To Live naming.
2025-07-05 17:07:53 +02:00

105 lines
4.3 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 DefaultMessageTimeToLiveInSeconds { 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; }
/// <summary>
/// File containing all user credentials.
/// </summary>
public FileInfo UsersConfig { get; set; }
public MessengerConfiguration() { }
public MessengerConfiguration(string[] origins, PersistenceConfiguration persistenceConfiguration)
{
if (persistenceConfiguration == null)
{
throw new ArgumentNullException(nameof(persistenceConfiguration));
}
this.PersistenceConfiguration = persistenceConfiguration;
this.UsersConfig = new FileInfo("./users.conf");
this.Origins = origins ?? [];
this.Proxies = [];
this.RateLimitPerMinute = 120;
this.DefaultMessageTimeToLiveInSeconds = 60;
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_TIME_TO_LIVE_IN_SECONDS, x => this.DefaultMessageTimeToLiveInSeconds = 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));
Populate<string>(config, Env.USERSCONFIG_FILE_PATH, x => this.UsersConfig = new FileInfo(x));
void Populate<T>(IEnvironmentConfigurationSource config, string key, Action<T> set)
{
if (config.HasKey(key))
{
var value = config.GetValue<T>(key);
set(value);
}
}
}
}
}