setting up docker build workflow
This commit is contained in:
43
6/Program.cs
43
6/Program.cs
@@ -1,13 +1,13 @@
|
||||
using Example.Data;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Npgsql;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
||||
?? throw new InvalidOperationException("Connection string 'DefaultConnection' was not found.");
|
||||
var configuredProvider = builder.Configuration["Database:Provider"]
|
||||
?? throw new InvalidOperationException("Database provider configuration 'Database:Provider' was not found.");
|
||||
var connectionString = ResolveConnectionString(builder, configuredProvider);
|
||||
|
||||
if (builder.Environment.IsDevelopment())
|
||||
{
|
||||
@@ -116,3 +116,42 @@ static void ValidateConnectionString(string connectionString, string configuredP
|
||||
|
||||
throw new InvalidOperationException($"Unsupported database provider '{configuredProvider}'.");
|
||||
}
|
||||
|
||||
static string ResolveConnectionString(WebApplicationBuilder builder, string configuredProvider)
|
||||
{
|
||||
var configuredConnectionString = builder.Configuration.GetConnectionString("DefaultConnection")
|
||||
?? throw new InvalidOperationException("Connection string 'DefaultConnection' was not found.");
|
||||
|
||||
if (builder.Environment.IsDevelopment() ||
|
||||
!string.Equals(configuredProvider, "Postgres", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
return configuredConnectionString;
|
||||
}
|
||||
|
||||
var passwordFile = builder.Configuration["POSTGRES_PASSWORD_FILE"];
|
||||
if (string.IsNullOrWhiteSpace(passwordFile) || !File.Exists(passwordFile))
|
||||
{
|
||||
return configuredConnectionString;
|
||||
}
|
||||
|
||||
var password = File.ReadAllText(passwordFile).Trim();
|
||||
if (string.IsNullOrWhiteSpace(password))
|
||||
{
|
||||
throw new InvalidOperationException($"The PostgreSQL password file '{passwordFile}' is empty.");
|
||||
}
|
||||
|
||||
var connectionStringBuilder = new NpgsqlConnectionStringBuilder(configuredConnectionString)
|
||||
{
|
||||
Host = builder.Configuration["POSTGRES_HOST"] ?? "db",
|
||||
Database = builder.Configuration["POSTGRES_DB"] ?? "db",
|
||||
Username = builder.Configuration["POSTGRES_USER"] ?? "user",
|
||||
Password = password
|
||||
};
|
||||
|
||||
if (int.TryParse(builder.Configuration["POSTGRES_PORT"], out var port))
|
||||
{
|
||||
connectionStringBuilder.Port = port;
|
||||
}
|
||||
|
||||
return connectionStringBuilder.ConnectionString;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user