Refactoring SetupConsole with DBFactory
This commit is contained in:
parent
8dd01d291d
commit
78d49a9829
20 changed files with 337 additions and 407 deletions
|
|
@ -4,6 +4,7 @@ using Microsoft.Extensions.DependencyInjection;
|
|||
using Microsoft.Extensions.Hosting;
|
||||
using Npgsql;
|
||||
using PlanTempus.Database.ConfigurationManagementSystem;
|
||||
using PlanTempus.Database.Core.ConnectionFactory;
|
||||
using PlanTempus.Database.Core.DCL;
|
||||
using PlanTempus.Database.Core.DDL;
|
||||
using System.Data;
|
||||
|
|
@ -25,7 +26,7 @@ namespace PlanTempus.SetupInfrastructure
|
|||
/// </summary>
|
||||
internal class Program
|
||||
{
|
||||
|
||||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
try
|
||||
|
|
@ -44,9 +45,9 @@ namespace PlanTempus.SetupInfrastructure
|
|||
await host.StartAsync();
|
||||
Console.WriteLine("Host has started.");
|
||||
|
||||
|
||||
var console = host.Services.GetRequiredService<ConsoleService>();
|
||||
console.Run();
|
||||
|
||||
var console = host.Services.GetRequiredService<ConsoleService>();
|
||||
console.Run();
|
||||
|
||||
await host.WaitForShutdownAsync();
|
||||
}
|
||||
|
|
@ -59,31 +60,30 @@ namespace PlanTempus.SetupInfrastructure
|
|||
public class ConsoleService
|
||||
{
|
||||
|
||||
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
||||
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
||||
static ConsoleColor _backgroundColor = Console.BackgroundColor;
|
||||
static ConsoleColor _foregroundColor = Console.ForegroundColor;
|
||||
private readonly IDbConnectionFactory _connectionFactory;
|
||||
private readonly SetupDbAdmin _setupDbAdmin;
|
||||
private readonly SetupIdentitySystem _setupIdentitySystem;
|
||||
private readonly SetupConfiguration _setupConfiguration;
|
||||
private readonly SetupApplicationUser _setupApplicationUser;
|
||||
|
||||
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)
|
||||
public ConsoleService(IDbConnectionFactory connectionFactory, SetupDbAdmin setupDbAdmin, SetupIdentitySystem setupIdentitySystem, SetupConfiguration setupConfiguration, SetupApplicationUser setupApplicationUser)
|
||||
{
|
||||
_setupDbAdmin = setupDbAdmin;
|
||||
_setupIdentitySystem = setupIdentitySystem;
|
||||
_setupConfiguration = setupConfiguration;
|
||||
_setupApplicationUser = setupApplicationUser;
|
||||
}
|
||||
static bool IsSuperAdmin()
|
||||
_connectionFactory = connectionFactory;
|
||||
_setupDbAdmin = setupDbAdmin;
|
||||
_setupIdentitySystem = setupIdentitySystem;
|
||||
_setupConfiguration = setupConfiguration;
|
||||
_setupApplicationUser = setupApplicationUser;
|
||||
}
|
||||
bool IsSuperAdmin(ConnectionStringParameters parameters)
|
||||
{
|
||||
//test db access
|
||||
Console.WriteLine("Testing db access...");
|
||||
|
||||
string query = @"SELECT usename, usesuper FROM pg_user WHERE usename = CURRENT_USER;";
|
||||
|
||||
var conn = new NpgsqlConnection();
|
||||
|
||||
var result = (dynamic)conn.QuerySql(query).Single();
|
||||
using var conn = _connectionFactory.Create(parameters);
|
||||
var result = (dynamic)conn.QuerySql(query).Single();
|
||||
|
||||
string username = result.usename;
|
||||
bool isSuperuser = (bool)result.usesuper;
|
||||
|
|
@ -91,12 +91,12 @@ namespace PlanTempus.SetupInfrastructure
|
|||
if ((bool)result.usesuper)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine();
|
||||
Console.BackgroundColor = ConsoleColor.Yellow;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("TEST SUCCESSFULLY");
|
||||
Console.WriteLine();
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.BackgroundColor = _backgroundColor;
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("-------------------------------");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Username: {username}");
|
||||
|
|
@ -118,7 +118,7 @@ namespace PlanTempus.SetupInfrastructure
|
|||
Console.WriteLine("TEST WAS NOT SUCCESSFULLY");
|
||||
Console.WriteLine();
|
||||
Console.ForegroundColor = ConsoleColor.White;
|
||||
Console.BackgroundColor = _backgroundColor;
|
||||
Console.BackgroundColor = ConsoleColor.Black;
|
||||
Console.WriteLine("-------------------------------");
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Username: {username}");
|
||||
|
|
@ -169,40 +169,38 @@ namespace PlanTempus.SetupInfrastructure
|
|||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
||||
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
||||
|
||||
//var ctp = new Startup.ConnectionStringTemplateParameters(
|
||||
// user: userPass.Split(":")[0],
|
||||
// pwd: userPass.Split(":")[1]
|
||||
//);
|
||||
//_container = new Startup().ConfigureContainer(ctp);
|
||||
var superUser = new ConnectionStringParameters(
|
||||
user: userPass.Split(":")[0],
|
||||
pwd: userPass.Split(":")[1]
|
||||
);
|
||||
|
||||
if (IsSuperAdmin())
|
||||
if (IsSuperAdmin(superUser))
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var sw = new Stopwatch();
|
||||
|
||||
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
||||
sw.Start();
|
||||
|
||||
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
_setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" }, superUser);
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
Console.WriteLine("::");
|
||||
Console.WriteLine("::");
|
||||
Console.Write("Database.Core.DDL.SetupIdentitySystem...");
|
||||
sw.Restart();
|
||||
|
||||
//create new container with application user, we use that role from now.
|
||||
//_container = new Startup().ConfigureContainer(new Startup.ConnectionStringTemplateParameters("heimdall", "3911"));
|
||||
//use application user, we use that role from now.
|
||||
var connParams = new ConnectionStringParameters(user: "heimdall", pwd: "3911");
|
||||
|
||||
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" });
|
||||
_setupIdentitySystem.With(new SetupIdentitySystem.Command { Schema = "system" }, connParams);
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
Console.WriteLine("::");
|
||||
Console.WriteLine("::");
|
||||
Console.Write("Database.ConfigurationManagementSystem.SetupConfiguration...");
|
||||
sw.Restart();
|
||||
_setupConfiguration.With(new SetupConfiguration.Command());
|
||||
_setupConfiguration.With(new SetupConfiguration.Command(), connParams);
|
||||
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
|
|
@ -210,8 +208,8 @@ namespace PlanTempus.SetupInfrastructure
|
|||
Console.WriteLine("::");
|
||||
Console.Write("Database.Core.DCL.SetupApplicationUser...");
|
||||
sw.Start();
|
||||
|
||||
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" });
|
||||
|
||||
_setupApplicationUser.With(new SetupApplicationUser.Command { Password = "3911", Schema = "system", User = "sathumper" }, superUser);
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
|
|
@ -227,6 +225,7 @@ namespace PlanTempus.SetupInfrastructure
|
|||
catch (Exception e)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(e);
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,50 +6,44 @@ using PlanTempus.Database.Core;
|
|||
|
||||
namespace PlanTempus.SetupInfrastructure
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appconfiguration.json")
|
||||
.Build();
|
||||
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appconfiguration.json")
|
||||
.Build();
|
||||
return configuration;
|
||||
}
|
||||
|
||||
return configuration;
|
||||
}
|
||||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
{
|
||||
|
||||
public void ConfigureContainer(ContainerBuilder builder)
|
||||
{
|
||||
var configuration = Configuration();
|
||||
|
||||
//var builder = new ContainerBuilder();
|
||||
var configuration = Configuration();
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
|
||||
builder.RegisterModule(new SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
||||
});
|
||||
|
||||
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||
.AsSelf();
|
||||
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
builder.RegisterType<ConsoleService>();
|
||||
|
||||
builder.RegisterModule(new SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
||||
});
|
||||
|
||||
|
||||
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||
.AsSelf();
|
||||
|
||||
builder.RegisterType<ConsoleService>();
|
||||
|
||||
|
||||
//return builder.Build();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
||||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;"
|
||||
},
|
||||
"ApplicationInsights": {
|
||||
"ConnectionString": "InstrumentationKey=07d2a2b9-5e8e-4924-836e-264f8438f6c5;IngestionEndpoint=https://northeurope-2.in.applicationinsights.azure.com/;LiveEndpoint=https://northeurope.livediagnostics.monitor.azure.com/;ApplicationId=56748c39-2fa3-4880-a1e2-24068e791548",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue