From 94fe79bcd12f97c44506c332a46e9fa517e72306 Mon Sep 17 00:00:00 2001 From: Fred Heinecke Date: Thu, 24 Apr 2025 04:55:46 +0000 Subject: [PATCH] Support standard libpq environment variables for the Backend service Signed-off-by: Fred Heinecke --- back/.env.example | 15 +++++++ back/src/Kyoo.Postgresql/PostgresModule.cs | 47 +++++++++++++++++----- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/back/.env.example b/back/.env.example index 300fc286..ce1153e2 100644 --- a/back/.env.example +++ b/back/.env.example @@ -3,3 +3,18 @@ # http route prefix (will listen to $KYOO_PREFIX/movie for example) 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 diff --git a/back/src/Kyoo.Postgresql/PostgresModule.cs b/back/src/Kyoo.Postgresql/PostgresModule.cs index 012bd0c0..2558f36b 100644 --- a/back/src/Kyoo.Postgresql/PostgresModule.cs +++ b/back/src/Kyoo.Postgresql/PostgresModule.cs @@ -33,19 +33,46 @@ public static class PostgresModule { public static NpgsqlDataSource CreateDataSource(IConfiguration configuration) { - DbConnectionStringBuilder conBuilder = - new() + // Load the connection string from the environment variable, as well as standard libpq environment variables + // (PGUSER, PGPASSWORD, PGHOST, PGPORT, PGDATABASE, etc.) + NpgsqlConnectionStringBuilder conBuilder = + new(configuration.GetValue("POSTGRES_URL") ?? "") { - ["USER ID"] = configuration.GetValue("POSTGRES_USER", "KyooUser"), - ["PASSWORD"] = configuration.GetValue("POSTGRES_PASSWORD", "KyooPassword"), - ["SERVER"] = configuration.GetValue("POSTGRES_SERVER", "postgres"), - ["PORT"] = configuration.GetValue("POSTGRES_PORT", "5432"), - ["DATABASE"] = configuration.GetValue("POSTGRES_DB", "kyooDB"), - ["POOLING"] = "true", - ["MAXPOOLSIZE"] = "95", - ["TIMEOUT"] = "30" + Pooling = true, + MaxPoolSize = 95, + Timeout = 30 }; + string? oldVarUsername = configuration.GetValue("POSTGRES_USER"); + if (!string.IsNullOrEmpty(oldVarUsername)) + conBuilder.Username = oldVarUsername; + if (string.IsNullOrEmpty(conBuilder.Username)) + conBuilder.Username = "KyooUser"; + + string? oldVarPassword = configuration.GetValue("POSTGRES_PASSWORD"); + if (!string.IsNullOrEmpty(oldVarPassword)) + conBuilder.Password = oldVarPassword; + if (string.IsNullOrEmpty(conBuilder.Password)) + conBuilder.Password = "KyooPassword"; + + string? oldVarHost = configuration.GetValue("POSTGRES_SERVER"); + if (!string.IsNullOrEmpty(oldVarHost)) + conBuilder.Host = oldVarHost; + if (string.IsNullOrEmpty(conBuilder.Host)) + conBuilder.Host = "postgres"; + + int? oldVarPort = configuration.GetValue("POSTGRES_PORT"); + if (oldVarPort != null && oldVarPort != 0) + conBuilder.Port = oldVarPort.Value; + if (conBuilder.Port == 0) + conBuilder.Port = 5432; + + string? oldVarDatabase = configuration.GetValue("POSTGRES_DB"); + if (!string.IsNullOrEmpty(oldVarDatabase)) + conBuilder.Database = oldVarDatabase; + if (string.IsNullOrEmpty(conBuilder.Database)) + conBuilder.Database = "kyooDB"; + NpgsqlDataSourceBuilder dsBuilder = new(conBuilder.ConnectionString); dsBuilder.MapEnum(); dsBuilder.MapEnum();