Setting up a db factory with logging
Important so we can log all commands
This commit is contained in:
parent
ad4ed12f00
commit
1501ff442a
10 changed files with 412 additions and 177 deletions
|
|
@ -29,82 +29,28 @@ namespace PlanTempus.SetupInfrastructure
|
|||
|
||||
static async Task Main(string[] args)
|
||||
{
|
||||
Welcome();
|
||||
string userPass;
|
||||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
Console.WriteLine("Input username:password for a superadmin role");
|
||||
userPass = Console.ReadLine() ?? string.Empty;
|
||||
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
||||
string.IsNullOrEmpty(userPass.Split(":")[0]) ||
|
||||
string.IsNullOrEmpty(userPass.Split(":")[1]));
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.UseEnvironment("Development")
|
||||
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
||||
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
||||
{
|
||||
var startup = new Startup();
|
||||
startup.ConfigureContainer();
|
||||
})
|
||||
|
||||
var ctp = new Startup.ConnectionStringTemplateParameters(
|
||||
user: userPass.Split(":")[0],
|
||||
pwd: userPass.Split(":")[1]
|
||||
);
|
||||
_container = new Startup().ConfigureContainer(ctp);
|
||||
|
||||
if (IsSuperAdmin())
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var sw = new Stopwatch();
|
||||
|
||||
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
||||
sw.Start();
|
||||
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
|
||||
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
||||
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"));
|
||||
|
||||
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
|
||||
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());
|
||||
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
Console.WriteLine("::");
|
||||
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" });
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
//and a lot of other tables that we haven't defined yet
|
||||
|
||||
// input configurations!!! TODO:Missing
|
||||
|
||||
Console.ForegroundColor = _foregroundColor;
|
||||
}
|
||||
.Build();
|
||||
|
||||
await host.StartAsync();
|
||||
Console.WriteLine("Host has started.");
|
||||
Run();
|
||||
|
||||
await host.WaitForShutdownAsync();
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(e);
|
||||
|
||||
Console.WriteLine($"Host failed to start: {ex}");
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -112,8 +58,6 @@ namespace PlanTempus.SetupInfrastructure
|
|||
|
||||
static bool IsSuperAdmin()
|
||||
{
|
||||
|
||||
|
||||
//test db access
|
||||
Console.WriteLine("Testing db access...");
|
||||
|
||||
|
|
@ -191,34 +135,90 @@ namespace PlanTempus.SetupInfrastructure
|
|||
|
||||
}
|
||||
|
||||
async Task Run(string[] args)
|
||||
{
|
||||
try
|
||||
{
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.UseServiceProviderFactory(new Autofac.Extensions.DependencyInjection.AutofacServiceProviderFactory())
|
||||
.ConfigureContainer<ContainerBuilder>((hostContext, builder) =>
|
||||
{
|
||||
var startup = new Startup();
|
||||
var connectionStringParams = new Startup.ConnectionStringTemplateParameters("your_user", "your_password");
|
||||
startup.ConfigureContainer(connectionStringParams);
|
||||
})
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
// Konfigurer andre services her (hvis nødvendigt)
|
||||
})
|
||||
.Build();
|
||||
static void Run()
|
||||
{
|
||||
|
||||
|
||||
Welcome();
|
||||
string userPass;
|
||||
|
||||
try
|
||||
{
|
||||
do
|
||||
{
|
||||
Console.WriteLine("Input username:password for a superadmin role");
|
||||
userPass = Console.ReadLine() ?? string.Empty;
|
||||
} while (!userPass.Contains(":") || userPass.Split(":").Length != 2 ||
|
||||
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);
|
||||
|
||||
if (IsSuperAdmin())
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
var sw = new Stopwatch();
|
||||
|
||||
Console.Write("Database.Core.DCL.SetupDbAdmin...");
|
||||
sw.Start();
|
||||
var setupDbAdmin = _container.Resolve<SetupDbAdmin>();
|
||||
setupDbAdmin.With(new SetupDbAdmin.Command { Password = "3911", Schema = "system", User = "heimdall" });
|
||||
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"));
|
||||
|
||||
var setupIdentitySystem = _container.Resolve<SetupIdentitySystem>();
|
||||
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());
|
||||
Console.Write($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
Console.WriteLine("::");
|
||||
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" });
|
||||
Console.WriteLine($"DONE, took: {sw.ElapsedMilliseconds} ms");
|
||||
|
||||
|
||||
//and a lot of other tables that we haven't defined yet
|
||||
|
||||
// input configurations!!! TODO:Missing
|
||||
|
||||
Console.ForegroundColor = _foregroundColor;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine(e);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
await host.StartAsync();
|
||||
Console.WriteLine("Host has started.");
|
||||
await host.WaitForShutdownAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Host failed to start: {ex}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,47 +6,48 @@ using PlanTempus.Database.Core;
|
|||
|
||||
namespace PlanTempus.SetupInfrastructure
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appconfiguration.json")
|
||||
.Build();
|
||||
public class Startup
|
||||
{
|
||||
|
||||
return configuration;
|
||||
}
|
||||
public virtual IConfigurationRoot Configuration()
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.AddJsonFile("appconfiguration.json")
|
||||
.Build();
|
||||
|
||||
public IContainer ConfigureContainer(ConnectionStringTemplateParameters ctp)
|
||||
{
|
||||
var builder = new ContainerBuilder();
|
||||
var configuration = Configuration();
|
||||
return configuration;
|
||||
}
|
||||
|
||||
public IContainer ConfigureContainer()
|
||||
{
|
||||
|
||||
var builder = new ContainerBuilder();
|
||||
var configuration = Configuration();
|
||||
|
||||
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection").Replace("{usr}", ctp.user).Replace("{pwd}", ctp.pwd)
|
||||
});
|
||||
builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
|
||||
{
|
||||
ConnectionString = configuration.GetConnectionString("DefaultConnection")
|
||||
});
|
||||
|
||||
builder.RegisterModule(new TelemetryModule
|
||||
{
|
||||
TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
|
||||
});
|
||||
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 SeqLoggingModule
|
||||
{
|
||||
SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<Core.Logging.SeqConfiguration>()
|
||||
});
|
||||
|
||||
|
||||
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||
.AsSelf();
|
||||
builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
|
||||
.AsClosedTypesOf(typeof(IDbConfigure<>))
|
||||
.AsSelf();
|
||||
|
||||
|
||||
return builder.Build();
|
||||
}
|
||||
return builder.Build();
|
||||
}
|
||||
|
||||
public record ConnectionStringTemplateParameters(string user, string pwd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@
|
|||
"DefaultConnection": "Host=192.168.1.57;Port=5432;Database=ptmain;User Id={usr};Password={pwd};"
|
||||
},
|
||||
"ApplicationInsights": {
|
||||
"ConnectionString": "InstrumentationKey=6d2e76ee-5343-4691-a5e3-81add43cb584;IngestionEndpoint=https://northeurope-0.in.applicationinsights.azure.com/",
|
||||
"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",
|
||||
"UseSeqLoggingTelemetryChannel": true
|
||||
},
|
||||
"SeqConfiguration": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue