refactor: GeneralConfig

This commit is contained in:
Polianin Nikita 2024-07-04 23:45:33 +03:00
parent 05ca45db49
commit 9133b57a1b
3 changed files with 23 additions and 14 deletions

View File

@ -1,14 +1,32 @@
using Mirea.Api.Endpoint.Configuration.General.Settings; using Mirea.Api.Endpoint.Common.Services;
using Mirea.Api.Endpoint.Configuration.General.Settings;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Mirea.Api.Endpoint.Configuration.General; namespace Mirea.Api.Endpoint.Configuration.General;
public class GeneralConfig public class GeneralConfig
{ {
public const string FilePath = "Settings.json"; [JsonIgnore] private const string FileName = "Settings.json";
[JsonIgnore]
public static string FilePath => PathBuilder.Combine(FileName);
public DbSettings? DbSettings { get; set; } public DbSettings? DbSettings { get; set; }
public CacheSettings? CacheSettings { get; set; } public CacheSettings? CacheSettings { get; set; }
public ScheduleSettings? ScheduleSettings { get; set; } public ScheduleSettings? ScheduleSettings { get; set; }
public EmailSettings? EmailSettings { get; set; } public EmailSettings? EmailSettings { get; set; }
public LogSettings? LogSettings { get; set; } public LogSettings? LogSettings { get; set; }
public void SaveSetting()
{
File.WriteAllText(
FilePath,
JsonSerializer.Serialize(this, new JsonSerializerOptions
{
WriteIndented = true
})
);
}
} }

View File

@ -55,7 +55,7 @@ public partial class SetupController(
if (!notConfigureService.IsMaintenanceMode) if (!notConfigureService.IsMaintenanceMode)
throw new ControllerArgumentException( throw new ControllerArgumentException(
"The token cannot be generated because the server has been configured. " + "The token cannot be generated because the server has been configured. " +
$"If you need to restart the configuration, then delete the \"{PathBuilder.Combine(GeneralConfig.FilePath)}\" file and restart the application."); $"If you need to restart the configuration, then delete the \"{GeneralConfig.FilePath}\" file and restart the application.");
var token = new byte[32]; var token = new byte[32];
RandomNumberGenerator.Create().GetBytes(token); RandomNumberGenerator.Create().GetBytes(token);
@ -333,18 +333,9 @@ public partial class SetupController(
if (!cache.TryGetValue(CacheAdminKey, out Admin? admin) || admin == null) if (!cache.TryGetValue(CacheAdminKey, out Admin? admin) || admin == null)
throw new ControllerArgumentException("The administrator's data was not set."); throw new ControllerArgumentException("The administrator's data was not set.");
if (System.IO.File.Exists(PathBuilder.Combine(GeneralConfig.FilePath)))
System.IO.File.Delete(PathBuilder.Combine(GeneralConfig.FilePath));
System.IO.File.WriteAllText(PathBuilder.Combine(Admin.PathToSave), JsonSerializer.Serialize(admin)); System.IO.File.WriteAllText(PathBuilder.Combine(Admin.PathToSave), JsonSerializer.Serialize(admin));
System.IO.File.WriteAllText( GeneralConfig.SaveSetting();
PathBuilder.Combine(GeneralConfig.FilePath),
JsonSerializer.Serialize(GeneralConfig, new JsonSerializerOptions
{
WriteIndented = true
})
);
return true; return true;
} }

View File

@ -36,7 +36,7 @@ public class Program
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment()); builder.Configuration.AddConfiguration(EnvironmentConfiguration.GetEnvironment());
builder.Configuration.AddJsonFile(PathBuilder.Combine(GeneralConfig.FilePath), optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(GeneralConfig.FilePath, optional: true, reloadOnChange: true);
builder.Services.Configure<GeneralConfig>(builder.Configuration); builder.Services.Configure<GeneralConfig>(builder.Configuration);
builder.Configuration.AddJsonFile(PathBuilder.Combine(Admin.PathToSave), optional: true, reloadOnChange: true); builder.Configuration.AddJsonFile(PathBuilder.Combine(Admin.PathToSave), optional: true, reloadOnChange: true);
builder.Services.Configure<Admin>(builder.Configuration); builder.Services.Configure<Admin>(builder.Configuration);