//
// Copyright (c) alveus.dev. All rights reserved. Licensed under the MIT License.
//
using Astral.Core.Infrastructure;
using Astral.Services.Interfaces;
namespace Astral.ApiServer.HostedService;
///
/// Processes to be executed at startup.
///
public class StartupService : IHostedService
{
private readonly ILogger _logger;
private readonly IServiceScopeFactory _serviceScopeFactory;
///
/// 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;
}
}