using Galaeth.Core.Infrastructure;
using Galaeth.Services.Interfaces;
namespace Galaeth.ApiServer.HostedServices;
///
/// Processes to be executed at startup.
///
public class StartupService : IHostedService
{
private readonly IServiceScopeFactory _serviceScopeFactory;
private readonly ILogger _logger;
///
/// Initializes a new instance of the class.
///
/// Instance of .
/// Instance of .
public StartupService(
IServiceScopeFactory serviceScopeFactory,
ILogger logger)
{
_serviceScopeFactory = serviceScopeFactory;
_logger = logger;
}
///
public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Running startup processes");
using var scope = _serviceScopeFactory.CreateScope();
// Database migrations.
var dbMigrator = scope.ServiceProvider.GetRequiredService();
await dbMigrator.MigrateDatabaseAsync(AppDomain.CurrentDomain.BaseDirectory + "Migrations");
// DAL setup.
var dalSetup = scope.ServiceProvider.GetService();
if (dalSetup is not null)
{
await dalSetup.Setup();
}
// Email address domain blacklist update.
var emDomainBlacklist = scope.ServiceProvider.GetRequiredService();
await emDomainBlacklist.UpdateBlacklist();
// Initial user setup.
var initialUserSetup = scope.ServiceProvider.GetRequiredService();
await initialUserSetup.CreateFirstUserIfRequiredAsync();
}
///
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}