Support standard libpq environment variables for the Backend service

Signed-off-by: Fred Heinecke <fred.heinecke@yahoo.com>
This commit is contained in:
Fred Heinecke 2025-04-24 04:55:46 +00:00 committed by Zoe Roux
parent 5ced62aab3
commit 94fe79bcd1
2 changed files with 52 additions and 10 deletions

View File

@ -3,3 +3,18 @@
# http route prefix (will listen to $KYOO_PREFIX/movie for example) # http route prefix (will listen to $KYOO_PREFIX/movie for example)
KYOO_PREFIX="" KYOO_PREFIX=""
# POSTGRES_URL=postgres://user:password@hostname:port/dbname?sslmode=verify-full&sslrootcert=/path/to/server.crt&sslcert=/path/to/client.crt&sslkey=/path/to/client.key
# The behavior of the below variables match what is documented here:
# https://www.postgresql.org/docs/current/libpq-envars.html
PGUSER=kyoo
PGPASSWORD=password
PGDB=kyooDB
PGSERVER=postgres
PGPORT=5432
# PGOPTIONS=-c search_path=kyoo,public
# PGPASSFILE=/my/password # Takes precedence over PGPASSWORD. New line characters are not trimmed.
# PGSSLMODE=verify-full
# PGSSLROOTCERT=/my/serving.crt
# PGSSLCERT=/my/client.crt
# PGSSLKEY=/my/client.key

View File

@ -33,19 +33,46 @@ public static class PostgresModule
{ {
public static NpgsqlDataSource CreateDataSource(IConfiguration configuration) public static NpgsqlDataSource CreateDataSource(IConfiguration configuration)
{ {
DbConnectionStringBuilder conBuilder = // Load the connection string from the environment variable, as well as standard libpq environment variables
new() // (PGUSER, PGPASSWORD, PGHOST, PGPORT, PGDATABASE, etc.)
NpgsqlConnectionStringBuilder conBuilder =
new(configuration.GetValue<string>("POSTGRES_URL") ?? "")
{ {
["USER ID"] = configuration.GetValue("POSTGRES_USER", "KyooUser"), Pooling = true,
["PASSWORD"] = configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"), MaxPoolSize = 95,
["SERVER"] = configuration.GetValue("POSTGRES_SERVER", "postgres"), Timeout = 30
["PORT"] = configuration.GetValue("POSTGRES_PORT", "5432"),
["DATABASE"] = configuration.GetValue("POSTGRES_DB", "kyooDB"),
["POOLING"] = "true",
["MAXPOOLSIZE"] = "95",
["TIMEOUT"] = "30"
}; };
string? oldVarUsername = configuration.GetValue<string>("POSTGRES_USER");
if (!string.IsNullOrEmpty(oldVarUsername))
conBuilder.Username = oldVarUsername;
if (string.IsNullOrEmpty(conBuilder.Username))
conBuilder.Username = "KyooUser";
string? oldVarPassword = configuration.GetValue<string>("POSTGRES_PASSWORD");
if (!string.IsNullOrEmpty(oldVarPassword))
conBuilder.Password = oldVarPassword;
if (string.IsNullOrEmpty(conBuilder.Password))
conBuilder.Password = "KyooPassword";
string? oldVarHost = configuration.GetValue<string>("POSTGRES_SERVER");
if (!string.IsNullOrEmpty(oldVarHost))
conBuilder.Host = oldVarHost;
if (string.IsNullOrEmpty(conBuilder.Host))
conBuilder.Host = "postgres";
int? oldVarPort = configuration.GetValue<int>("POSTGRES_PORT");
if (oldVarPort != null && oldVarPort != 0)
conBuilder.Port = oldVarPort.Value;
if (conBuilder.Port == 0)
conBuilder.Port = 5432;
string? oldVarDatabase = configuration.GetValue<string>("POSTGRES_DB");
if (!string.IsNullOrEmpty(oldVarDatabase))
conBuilder.Database = oldVarDatabase;
if (string.IsNullOrEmpty(conBuilder.Database))
conBuilder.Database = "kyooDB";
NpgsqlDataSourceBuilder dsBuilder = new(conBuilder.ConnectionString); NpgsqlDataSourceBuilder dsBuilder = new(conBuilder.ConnectionString);
dsBuilder.MapEnum<Status>(); dsBuilder.MapEnum<Status>();
dsBuilder.MapEnum<Genre>(); dsBuilder.MapEnum<Genre>();