Compare commits
2 Commits
ae0b9daefa
...
35eb1eab39
Author | SHA1 | Date | |
---|---|---|---|
35eb1eab39 | |||
08f13108d8 |
@ -0,0 +1,38 @@
|
||||
using Microsoft.Extensions.Options;
|
||||
using Mirea.Api.Endpoint.Configuration.General.Attributes;
|
||||
using Mirea.Api.Endpoint.Configuration.General.Interfaces;
|
||||
using System;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Mirea.Api.Endpoint.Configuration.General.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;
|
||||
}
|
||||
}
|
@ -12,6 +12,7 @@ using Mirea.Api.Endpoint.Common.Interfaces;
|
||||
using Mirea.Api.Endpoint.Common.Services;
|
||||
using Mirea.Api.Endpoint.Configuration;
|
||||
using Mirea.Api.Endpoint.Configuration.General;
|
||||
using Mirea.Api.Endpoint.Configuration.General.Validators;
|
||||
using Mirea.Api.Endpoint.Configuration.Swagger;
|
||||
using Mirea.Api.Endpoint.Middleware;
|
||||
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||
@ -100,8 +101,25 @@ public class Program
|
||||
Console.WriteLine($"{item.Key}:{item.Value}");
|
||||
#endif
|
||||
|
||||
var uber = app.Services.CreateScope().ServiceProvider.GetService<UberDbContext>();
|
||||
DbInitializer.Initialize(uber!);
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
var serviceProvider = scope.ServiceProvider;
|
||||
|
||||
var optionsSnapshot = serviceProvider.GetRequiredService<IOptionsSnapshot<GeneralConfig>>();
|
||||
var settingsValidator = new SettingsRequiredValidator(optionsSnapshot);
|
||||
var isDoneConfig = settingsValidator.AreSettingsValid();
|
||||
|
||||
if (isDoneConfig)
|
||||
{
|
||||
var uberDbContext = serviceProvider.GetRequiredService<UberDbContext>();
|
||||
var maintenanceModeService = serviceProvider.GetRequiredService<IMaintenanceModeNotConfigureService>();
|
||||
|
||||
maintenanceModeService.DisableMaintenanceMode();
|
||||
DbInitializer.Initialize(uberDbContext);
|
||||
|
||||
// todo: if admin not found
|
||||
}
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
|
Loading…
Reference in New Issue
Block a user