refactor: code restructuring
This commit is contained in:
@ -0,0 +1,8 @@
|
||||
using System;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
public class RequiredSettingsAttribute : Attribute;
|
||||
|
||||
// todo: only with IIsConfigured. If possible add Roslyn Analyzer later
|
@ -0,0 +1,6 @@
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
|
||||
public interface IIsConfigured
|
||||
{
|
||||
bool IsConfigured();
|
||||
}
|
23
Endpoint/Configuration/Validation/Settings/CacheSettings.cs
Normal file
23
Endpoint/Configuration/Validation/Settings/CacheSettings.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Settings;
|
||||
|
||||
[RequiredSettings]
|
||||
public class CacheSettings : IIsConfigured
|
||||
{
|
||||
public enum CacheEnum
|
||||
{
|
||||
Memcached,
|
||||
Redis
|
||||
}
|
||||
|
||||
public CacheEnum TypeDatabase { get; set; }
|
||||
public string? ConnectionString { get; set; }
|
||||
|
||||
public bool IsConfigured()
|
||||
{
|
||||
return TypeDatabase == CacheEnum.Memcached ||
|
||||
!string.IsNullOrEmpty(ConnectionString);
|
||||
}
|
||||
}
|
33
Endpoint/Configuration/Validation/Settings/DbSettings.cs
Normal file
33
Endpoint/Configuration/Validation/Settings/DbSettings.cs
Normal file
@ -0,0 +1,33 @@
|
||||
using Mirea.Api.DataAccess.Persistence.Common;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Settings;
|
||||
|
||||
[RequiredSettings]
|
||||
public class DbSettings : IIsConfigured
|
||||
{
|
||||
public enum DatabaseEnum
|
||||
{
|
||||
Mysql,
|
||||
Sqlite,
|
||||
PostgresSql
|
||||
}
|
||||
public DatabaseEnum TypeDatabase { get; set; }
|
||||
public required string ConnectionStringSql { get; set; }
|
||||
|
||||
[JsonIgnore]
|
||||
public DatabaseProvider DatabaseProvider =>
|
||||
TypeDatabase switch
|
||||
{
|
||||
DatabaseEnum.PostgresSql => DatabaseProvider.Postgresql,
|
||||
DatabaseEnum.Mysql => DatabaseProvider.Mysql,
|
||||
DatabaseEnum.Sqlite => DatabaseProvider.Sqlite,
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
|
||||
public bool IsConfigured() =>
|
||||
!string.IsNullOrEmpty(ConnectionStringSql);
|
||||
}
|
23
Endpoint/Configuration/Validation/Settings/EmailSettings.cs
Normal file
23
Endpoint/Configuration/Validation/Settings/EmailSettings.cs
Normal file
@ -0,0 +1,23 @@
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Settings;
|
||||
|
||||
public class EmailSettings : IIsConfigured
|
||||
{
|
||||
public string? Server { get; set; }
|
||||
public string? User { get; set; }
|
||||
public string? Password { get; set; }
|
||||
public string? From { get; set; }
|
||||
public int? Port { get; set; }
|
||||
public bool? Ssl { get; set; }
|
||||
|
||||
public bool IsConfigured()
|
||||
{
|
||||
return !string.IsNullOrEmpty(Server) &&
|
||||
!string.IsNullOrEmpty(User) &&
|
||||
!string.IsNullOrEmpty(Password) &&
|
||||
!string.IsNullOrEmpty(From) &&
|
||||
Port.HasValue &&
|
||||
Ssl.HasValue;
|
||||
}
|
||||
}
|
19
Endpoint/Configuration/Validation/Settings/LogSettings.cs
Normal file
19
Endpoint/Configuration/Validation/Settings/LogSettings.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Settings;
|
||||
|
||||
[RequiredSettings]
|
||||
public class LogSettings : IIsConfigured
|
||||
{
|
||||
public bool EnableLogToFile { get; set; }
|
||||
public string? LogFilePath { get; set; }
|
||||
public string? LogFileName { get; set; }
|
||||
|
||||
public bool IsConfigured()
|
||||
{
|
||||
return !EnableLogToFile ||
|
||||
!string.IsNullOrEmpty(LogFilePath) &&
|
||||
!string.IsNullOrEmpty(LogFileName);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Settings;
|
||||
|
||||
[RequiredSettings]
|
||||
public class ScheduleSettings : IIsConfigured
|
||||
{
|
||||
public struct PairPeriodTime
|
||||
{
|
||||
public TimeOnly Start { get; set; }
|
||||
public TimeOnly End { get; set; }
|
||||
|
||||
public PairPeriodTime(TimeOnly t1, TimeOnly t2)
|
||||
{
|
||||
if (t1 > t2)
|
||||
{
|
||||
Start = t2;
|
||||
End = t1;
|
||||
}
|
||||
else
|
||||
{
|
||||
Start = t1;
|
||||
End = t2;
|
||||
}
|
||||
}
|
||||
|
||||
public PairPeriodTime(Dto.Common.PairPeriodTime time) : this(time.Start, time.End) { }
|
||||
}
|
||||
|
||||
public required string CronUpdateSchedule { get; set; }
|
||||
public DateOnly StartTerm { get; set; }
|
||||
public required IDictionary<int, PairPeriodTime> PairPeriod { get; set; }
|
||||
|
||||
public bool IsConfigured()
|
||||
{
|
||||
return !string.IsNullOrEmpty(CronUpdateSchedule) &&
|
||||
StartTerm != default &&
|
||||
PairPeriod.Count != 0 &&
|
||||
PairPeriod.Any();
|
||||
}
|
||||
}
|
28
Endpoint/Configuration/Validation/SetupTokenService.cs
Normal file
28
Endpoint/Configuration/Validation/SetupTokenService.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using Mirea.Api.Endpoint.Common.Interfaces;
|
||||
using System;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks;
|
||||
|
||||
public class SetupTokenService : ISetupToken
|
||||
{
|
||||
public ReadOnlyMemory<byte>? Token { get; private set; }
|
||||
|
||||
public bool MatchToken(ReadOnlySpan<byte> token)
|
||||
{
|
||||
if (Token == null || token.Length != Token.Value.Length)
|
||||
return false;
|
||||
|
||||
var token2 = Token.Value.Span;
|
||||
|
||||
int result = 0;
|
||||
for (int i = 0; i < Token.Value.Length; i++)
|
||||
result |= token2[i] ^ token[i];
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
|
||||
public void SetToken(ReadOnlySpan<byte> token)
|
||||
{
|
||||
Token = token.ToArray();
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Interfaces;
|
||||
using Mirea.Api.Endpoint.Configuration.Model;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.ConfigurationChecks.Validators;
|
||||
|
||||
public class SettingsRequiredValidator
|
||||
{
|
||||
private readonly GeneralConfig _generalConfig;
|
||||
|
||||
public SettingsRequiredValidator(IOptionsSnapshot<GeneralConfig> configuration) =>
|
||||
_generalConfig = configuration.Value;
|
||||
|
||||
public SettingsRequiredValidator(GeneralConfig configuration) =>
|
||||
_generalConfig = configuration;
|
||||
|
||||
public bool AreSettingsValid()
|
||||
{
|
||||
foreach (var property in _generalConfig
|
||||
.GetType()
|
||||
.GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
if (!Attribute.IsDefined(property.PropertyType, typeof(RequiredSettingsAttribute))) continue;
|
||||
|
||||
var value = property.GetValue(_generalConfig);
|
||||
if (value == null)
|
||||
return false;
|
||||
|
||||
var isConfigured = value as IIsConfigured;
|
||||
if (!isConfigured!.IsConfigured())
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user