some refactoring

This commit is contained in:
Stanley Dimant
2024-05-06 14:05:24 +02:00
parent 880676de09
commit dc33858626
48 changed files with 448 additions and 112 deletions

View File

@@ -0,0 +1,22 @@
using System.Text;
namespace MareSynchronosShared.Utils.Configuration;
public class AuthServiceConfiguration : MareConfigurationBase
{
public string GeoIPDbCityFile { get; set; } = string.Empty;
public bool UseGeoIP { get; set; } = false;
public int FailedAuthForTempBan { get; set; } = 5;
public int TempBanDurationInMinutes { get; set; } = 5;
public List<string> WhitelistedIps { get; set; } = new();
public override string ToString()
{
StringBuilder sb = new();
sb.AppendLine(base.ToString());
sb.AppendLine($"{nameof(RedisPool)} => {RedisPool}");
sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}");
sb.AppendLine($"{nameof(UseGeoIP)} => {UseGeoIP}");
return sb.ToString();
}
}

View File

@@ -0,0 +1,13 @@
namespace MareSynchronosShared.Utils.Configuration;
public class CdnShardConfiguration
{
public List<string> Continents { get; set; }
public string FileMatch { get; set; }
public Uri CdnFullUrl { get; set; }
public override string ToString()
{
return CdnFullUrl.ToString() + "[" + string.Join(',', Continents) + "] == " + FileMatch;
}
}

View File

@@ -0,0 +1,8 @@
namespace MareSynchronosShared.Utils.Configuration;
public interface IMareConfiguration
{
T GetValueOrDefault<T>(string key, T defaultValue);
T GetValue<T>(string key);
string SerializeValue(string key, string defaultValue);
}

View File

@@ -0,0 +1,51 @@
using System.Reflection;
using System.Text;
using System.Text.Json;
namespace MareSynchronosShared.Utils.Configuration;
public class MareConfigurationBase : IMareConfiguration
{
public int DbContextPoolSize { get; set; } = 100;
public string Jwt { get; set; } = string.Empty;
public Uri MainServerAddress { get; set; }
public int RedisPool { get; set; } = 50;
public int MetricsPort { get; set; }
public string RedisConnectionString { get; set; } = string.Empty;
public string ShardName { get; set; } = string.Empty;
public T GetValue<T>(string key)
{
var prop = GetType().GetProperty(key);
if (prop == null) throw new KeyNotFoundException(key);
if (prop.PropertyType != typeof(T)) throw new ArgumentException($"Requested {key} with T:{typeof(T)}, where {key} is {prop.PropertyType}");
return (T)prop.GetValue(this);
}
public T GetValueOrDefault<T>(string key, T defaultValue)
{
var prop = GetType().GetProperty(key);
if (prop.PropertyType != typeof(T)) throw new ArgumentException($"Requested {key} with T:{typeof(T)}, where {key} is {prop.PropertyType}");
if (prop == null) return defaultValue;
return (T)prop.GetValue(this);
}
public string SerializeValue(string key, string defaultValue)
{
var prop = GetType().GetProperty(key);
if (prop == null) return defaultValue;
if (prop.GetCustomAttribute<RemoteConfigurationAttribute>() == null) return defaultValue;
return JsonSerializer.Serialize(prop.GetValue(this), prop.PropertyType);
}
public override string ToString()
{
StringBuilder sb = new();
sb.AppendLine(base.ToString());
sb.AppendLine($"{nameof(MainServerAddress)} => {MainServerAddress}");
sb.AppendLine($"{nameof(RedisConnectionString)} => {RedisConnectionString}");
sb.AppendLine($"{nameof(ShardName)} => {ShardName}");
sb.AppendLine($"{nameof(DbContextPoolSize)} => {DbContextPoolSize}");
return sb.ToString();
}
}

View File

@@ -0,0 +1,44 @@
using System.Text;
namespace MareSynchronosShared.Utils.Configuration;
public class ServerConfiguration : MareConfigurationBase
{
[RemoteConfiguration]
public Uri CdnFullUrl { get; set; } = null;
[RemoteConfiguration]
public Version ExpectedClientVersion { get; set; } = new Version(0, 0, 0);
[RemoteConfiguration]
public int MaxExistingGroupsByUser { get; set; } = 3;
[RemoteConfiguration]
public int MaxGroupUserCount { get; set; } = 100;
[RemoteConfiguration]
public int MaxJoinedGroupsByUser { get; set; } = 6;
[RemoteConfiguration]
public bool PurgeUnusedAccounts { get; set; } = false;
[RemoteConfiguration]
public int PurgeUnusedAccountsPeriodInDays { get; set; } = 14;
public string GeoIPDbCityFile { get; set; } = string.Empty;
public override string ToString()
{
StringBuilder sb = new();
sb.AppendLine(base.ToString());
sb.AppendLine($"{nameof(CdnFullUrl)} => {CdnFullUrl}");
sb.AppendLine($"{nameof(RedisConnectionString)} => {RedisConnectionString}");
sb.AppendLine($"{nameof(ExpectedClientVersion)} => {ExpectedClientVersion}");
sb.AppendLine($"{nameof(MaxExistingGroupsByUser)} => {MaxExistingGroupsByUser}");
sb.AppendLine($"{nameof(MaxJoinedGroupsByUser)} => {MaxJoinedGroupsByUser}");
sb.AppendLine($"{nameof(MaxGroupUserCount)} => {MaxGroupUserCount}");
sb.AppendLine($"{nameof(PurgeUnusedAccounts)} => {PurgeUnusedAccounts}");
sb.AppendLine($"{nameof(PurgeUnusedAccountsPeriodInDays)} => {PurgeUnusedAccountsPeriodInDays}");
sb.AppendLine($"{nameof(GeoIPDbCityFile)} => {GeoIPDbCityFile}");
return sb.ToString();
}
}

View File

@@ -0,0 +1,31 @@
using System.Text;
namespace MareSynchronosShared.Utils.Configuration;
public class ServicesConfiguration : MareConfigurationBase
{
public string DiscordBotToken { get; set; } = string.Empty;
public ulong? DiscordChannelForMessages { get; set; } = null;
public ulong? DiscordChannelForReports { get; set; } = null;
public ulong? DiscordChannelForCommands { get; set; } = null;
public ulong? DiscordRoleAprilFools2024 { get; set; } = null;
public Uri MainServerAddress { get; set; } = null;
public Dictionary<ulong, string> VanityRoles { get; set; } = new Dictionary<ulong, string>();
public override string ToString()
{
StringBuilder sb = new();
sb.AppendLine(base.ToString());
sb.AppendLine($"{nameof(DiscordBotToken)} => {DiscordBotToken}");
sb.AppendLine($"{nameof(MainServerAddress)} => {MainServerAddress}");
sb.AppendLine($"{nameof(DiscordChannelForMessages)} => {DiscordChannelForMessages}");
sb.AppendLine($"{nameof(DiscordChannelForReports)} => {DiscordChannelForReports}");
sb.AppendLine($"{nameof(DiscordChannelForCommands)} => {DiscordChannelForCommands}");
sb.AppendLine($"{nameof(DiscordRoleAprilFools2024)} => {DiscordRoleAprilFools2024}");
foreach (var role in VanityRoles)
{
sb.AppendLine($"{nameof(VanityRoles)} => {role.Key} = {role.Value}");
}
return sb.ToString();
}
}

View File

@@ -0,0 +1,46 @@
using MareSynchronosShared.Utils;
using System.Text;
namespace MareSynchronosShared.Utils.Configuration;
public class StaticFilesServerConfiguration : MareConfigurationBase
{
public bool IsDistributionNode { get; set; } = false;
public Uri MainFileServerAddress { get; set; } = null;
public Uri DistributionFileServerAddress { get; set; } = null;
public int ForcedDeletionOfFilesAfterHours { get; set; } = -1;
public double CacheSizeHardLimitInGiB { get; set; } = -1;
public int UnusedFileRetentionPeriodInDays { get; set; } = 14;
public string CacheDirectory { get; set; }
public int DownloadQueueSize { get; set; } = 50;
public int DownloadTimeoutSeconds { get; set; } = 5;
public int DownloadQueueReleaseSeconds { get; set; } = 15;
public int DownloadQueueClearLimit { get; set; } = 15000;
public int CleanupCheckInMinutes { get; set; } = 15;
public bool UseColdStorage { get; set; } = false;
public string ColdStorageDirectory { get; set; } = null;
public double ColdStorageSizeHardLimitInGiB { get; set; } = -1;
public int ColdStorageUnusedFileRetentionPeriodInDays { get; set; } = 30;
[RemoteConfiguration]
public Uri CdnFullUrl { get; set; } = null;
[RemoteConfiguration]
public List<CdnShardConfiguration> CdnShardConfiguration { get; set; } = new();
public override string ToString()
{
StringBuilder sb = new();
sb.AppendLine(base.ToString());
sb.AppendLine($"{nameof(MainFileServerAddress)} => {MainFileServerAddress}");
sb.AppendLine($"{nameof(ForcedDeletionOfFilesAfterHours)} => {ForcedDeletionOfFilesAfterHours}");
sb.AppendLine($"{nameof(CacheSizeHardLimitInGiB)} => {CacheSizeHardLimitInGiB}");
sb.AppendLine($"{nameof(UseColdStorage)} => {UseColdStorage}");
sb.AppendLine($"{nameof(ColdStorageDirectory)} => {ColdStorageDirectory}");
sb.AppendLine($"{nameof(ColdStorageSizeHardLimitInGiB)} => {ColdStorageSizeHardLimitInGiB}");
sb.AppendLine($"{nameof(ColdStorageUnusedFileRetentionPeriodInDays)} => {ColdStorageUnusedFileRetentionPeriodInDays}");
sb.AppendLine($"{nameof(UnusedFileRetentionPeriodInDays)} => {UnusedFileRetentionPeriodInDays}");
sb.AppendLine($"{nameof(CacheDirectory)} => {CacheDirectory}");
sb.AppendLine($"{nameof(DownloadQueueSize)} => {DownloadQueueSize}");
sb.AppendLine($"{nameof(DownloadQueueReleaseSeconds)} => {DownloadQueueReleaseSeconds}");
sb.AppendLine($"{nameof(CdnShardConfiguration)} => {string.Join(", ", CdnShardConfiguration)}");
return sb.ToString();
}
}