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
{
///
/// CORS origins.
///
public string[] Origins { get; set; }
///
/// List of proxies that are trusted to provide forwarding headers.
///
public string[] Proxies { get; set; }
///
/// Persistence layer configs (database).
///
public PersistenceConfiguration PersistenceConfiguration { get; set; }
///
/// Limits rate of user calls to not DoS the service.
///
public int RateLimitPerMinute { get; set; }
///
/// Message lifetime unless set differently in message body.
///
public int DefaultMessageTimeToLiveInSeconds { get; set; }
///
/// If true, messages are periodically wiped to free up space.
///
public bool HousekeepingEnabled { get; set; }
///
/// Messages older than value set will be deleted regardless of their delivery state.
///
public int HousekeepingMessageAgeInMinutes { get; set; }
///
/// Determines level of log messages displayed.
///
public LoggingVerbosity Verbosity { get; set; }
///
/// In addition to messages of certain state can also be deleted, increasing storage efficiency.
///
public HousekeepingMessageStates HousekeepingMessageState { get; set; }
///
/// File containing all user credentials.
///
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(Env.CORS_ORIGINS)),
EnvironmentPersistenceConfigurationParser.Parse(config))
{
Populate(config, Env.PROXIES, x => this.Proxies = ProxiesParser.Parse(x));
Populate(config, Env.QUERY_RATE_PER_MINUTE, x => this.RateLimitPerMinute = x);
Populate(config, Env.DEFAULT_MESSAGE_TIME_TO_LIVE_IN_SECONDS, x => this.DefaultMessageTimeToLiveInSeconds = x);
Populate(config, Env.HOUSEKEEPING_ENABLED, x => this.HousekeepingEnabled = x);
Populate(config, Env.HOUSEKEEPING_MESSAGE_AGE_IN_MINUTES, x => this.HousekeepingMessageAgeInMinutes = x);
Populate(config, Env.HOUSEKEEPING_MESSAGE_STATE, x => this.HousekeepingMessageState = HousekeepingMessageStateParser.Parse(x));
Populate(config, Env.LOGGING_VERBOSITY, x => this.Verbosity = LoggingVerbosityParser.Parse(x));
Populate(config, Env.USERSCONFIG_FILE_PATH, x => this.UsersConfig = new FileInfo(x));
void Populate(IEnvironmentConfigurationSource config, string key, Action set)
{
if (config.HasKey(key))
{
var value = config.GetValue(key);
set(value);
}
}
}
}
}