More work on this Console Host

This commit is contained in:
Janus Knudsen 2025-02-21 17:03:49 +01:00
parent 1501ff442a
commit 8dd01d291d
11 changed files with 336 additions and 344 deletions

View file

@ -1,6 +1,8 @@
using Autofac;
using Insight.Database;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Npgsql;
using PlanTempus.Database.ConfigurationManagementSystem;
using PlanTempus.Database.Core.DCL;
using PlanTempus.Database.Core.DDL;
@ -23,28 +25,28 @@ namespace PlanTempus.SetupInfrastructure
/// </summary>
internal class Program
{
static IContainer _container;
static ConsoleColor _backgroundColor = Console.BackgroundColor;
static ConsoleColor _foregroundColor = Console.ForegroundColor;
static async Task Main(string[] args)
{
try
{
var host = Host.CreateDefaultBuilder(args)
.UseEnvironment("Development")
.UseEnvironment("Dev")
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
{
var startup = new Startup();
startup.ConfigureContainer();
startup.ConfigureContainer(builder);
})
.Build();
await host.StartAsync();
Console.WriteLine("Host has started.");
Run();
var console = host.Services.GetRequiredService<ConsoleService>();
console.Run();
await host.WaitForShutdownAsync();
}
@ -52,10 +54,26 @@ namespace PlanTempus.SetupInfrastructure
{
Console.WriteLine($"Host failed to start: {ex}");
}
}
}
public class ConsoleService
{
static ConsoleColor _backgroundColor = Console.BackgroundColor;
static ConsoleColor _foregroundColor = Console.ForegroundColor;
private readonly SetupDbAdmin _setupDbAdmin;
private readonly SetupIdentitySystem _setupIdentitySystem;
private readonly SetupConfiguration _setupConfiguration;
private readonly SetupApplicationUser _setupApplicationUser;
public ConsoleService(SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
{
_setupDbAdmin = setupDbAdmin;
_setupIdentitySystem = setupIdentitySystem;
_setupConfiguration = setupConfiguration;
_setupApplicationUser = setupApplicationUser;
}
static bool IsSuperAdmin()
{
//test db access
@ -63,8 +81,9 @@ namespace PlanTempus.SetupInfrastructure
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
var conn = _container.Resolve<IDbConnection>();
var result = (dynamic)conn.QuerySql(query).Single();
var conn = new NpgsqlConnection();
var result = (dynamic)conn.QuerySql(query).Single();
string username = result.usename;
bool isSuperuser = (bool)result.usesuper;
@ -135,10 +154,8 @@ namespace PlanTempus.SetupInfrastructure
}
static void Run()
public void Run()
{
Welcome();
string userPass;
@ -165,8 +182,8 @@ namespace PlanTempus.SetupInfrastructure
Console.Write("Database.Core.DCL.SetupDbAdmin...");
sw.Start();
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -178,16 +195,14 @@ namespace PlanTempus.SetupInfrastructure
//create new container with application user, we use that role from now.
//_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
Console.WriteLine("::");
Console.WriteLine("::");
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
sw.Restart();
var setupConfigurationSystem = _container.Resolve<SetupConfiguration>();
setupConfigurationSystem.With(new SetupConfiguration.Command());
_setupConfiguration.With(new SetupConfiguration.Command());
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -195,8 +210,8 @@ namespace PlanTempus.SetupInfrastructure
Console.WriteLine("::");
Console.Write("Database.Core.DCL.SetupApplicationUser...");
sw.Start();
var setupApplicationUser = _container.Resolve<SetupApplicationUser>();
setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
@ -207,7 +222,6 @@ namespace PlanTempus.SetupInfrastructure
Console.ForegroundColor = _foregroundColor;
}
}
catch (Exception e)