From 03d0b906dbc508bc4e3b0bf16ee819f610d3ae77 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Thu, 24 Jul 2025 22:55:13 -0400 Subject: [PATCH 1/4] feat: Standardize environment variables for database connection. Add `DB_ENGINE` environment variable, and unify alls `DB_*` redundant environment variables. fixes https://github.com/NginxProxyManager/nginx-proxy-manager/issues/4677 --- backend/config/default.json | 4 +- backend/lib/config.js | 68 +++++++++++++++++++++++++-- docker/docker-compose.ci.mysql.yml | 11 +++-- docker/docker-compose.ci.postgres.yml | 11 +++-- docker/docker-compose.dev.yml | 23 +++++---- docs/src/advanced-config/index.md | 13 ++--- docs/src/setup/index.md | 30 ++++++------ 7 files changed, 115 insertions(+), 45 deletions(-) diff --git a/backend/config/default.json b/backend/config/default.json index 154e66e48..8654b4cff 100644 --- a/backend/config/default.json +++ b/backend/config/default.json @@ -2,9 +2,9 @@ "database": { "engine": "mysql2", "host": "db", + "port": 3306, "name": "npm", "user": "npm", - "password": "npm", - "port": 3306 + "password": "npm" } } diff --git a/backend/lib/config.js b/backend/lib/config.js index 23184f3e8..90d0d8ea0 100644 --- a/backend/lib/config.js +++ b/backend/lib/config.js @@ -5,8 +5,30 @@ const logger = require('../logger').global; const keysFile = '/data/keys.json'; const mysqlEngine = 'mysql2'; const postgresEngine = 'pg'; +const sqliteEngine = 'knex-native'; const sqliteClientName = 'sqlite3'; +const dataBaseEngines = { + mysql: { + engine: mysqlEngine, + host: '127.0.0.1', + port: 3306, + }, + mariadb: { + engine: mysqlEngine, + host: '127.0.0.1', + port: 3306, + }, + postgres: { + engine: postgresEngine, + host: '127.0.0.1', + port: 5432, + }, + sqlite: { + engine: sqliteEngine, + }, +} + let instance = null; // 1. Load from config file first (not recommended anymore) @@ -29,6 +51,45 @@ const configure = () => { } } + const envDataBaseEngne = process.env.DB_ENGINE || null; + if (envDataBaseEngne) { + if (envDataBaseEngne === 'sqlite') { + logger.info(`Using Sqlite: ${envSqliteFile}`); + const defaultConection = dataBaseEngines[envDataBaseEngne] + const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite'; + instance = { + database: { + engine: defaultConection.engine, + knex: { + client: sqliteClientName, + connection: { + filename: envSqliteFile + }, + useNullAsDefault: true + } + }, + keys: getKeys(), + }; + } else { + // we have enough mysql/mariadb/postgres creds to go with mysql/mariadb/postgres + logger.info(`Using ${envDataBaseEngne} configuration`); + const defaultConection = dataBaseEngines[envDataBaseEngne] + instance = { + database: { + engine: defaultConection.engine, + host: process.env.DB_HOST || defaultConection.host, + port: process.env.DB_PORT || defaultConection.port, + name: process.env.DB_NAME || 'npm', + user: process.env.DB_USER || 'npm', + password: process.env.DB_PASSWORD || 'npmpass', + }, + keys: getKeys(), + }; + } + return; + } + + // TODO: Remove this section in future versions; it is retained for backward compatibility. const envMysqlHost = process.env.DB_MYSQL_HOST || null; const envMysqlUser = process.env.DB_MYSQL_USER || null; const envMysqlName = process.env.DB_MYSQL_NAME || null; @@ -49,6 +110,7 @@ const configure = () => { return; } + // TODO: Remove this section in future versions; it is retained for backward compatibility. const envPostgresHost = process.env.DB_POSTGRES_HOST || null; const envPostgresUser = process.env.DB_POSTGRES_USER || null; const envPostgresName = process.env.DB_POSTGRES_NAME || null; @@ -73,7 +135,7 @@ const configure = () => { logger.info(`Using Sqlite: ${envSqliteFile}`); instance = { database: { - engine: 'knex-native', + engine: sqliteEngine, knex: { client: sqliteClientName, connection: { @@ -170,7 +232,7 @@ module.exports = { }, /** - * Is this a mysql configuration? + * Is this a MySQL/MariaDB configuration? * * @returns {boolean} */ @@ -178,7 +240,7 @@ module.exports = { instance === null && configure(); return instance.database.engine === mysqlEngine; }, - + /** * Is this a postgres configuration? * diff --git a/docker/docker-compose.ci.mysql.yml b/docker/docker-compose.ci.mysql.yml index 108a1dca3..427ebd9a4 100644 --- a/docker/docker-compose.ci.mysql.yml +++ b/docker/docker-compose.ci.mysql.yml @@ -3,11 +3,12 @@ services: fullstack: environment: - DB_MYSQL_HOST: 'db-mysql' - DB_MYSQL_PORT: '3306' - DB_MYSQL_USER: 'npm' - DB_MYSQL_PASSWORD: 'npmpass' - DB_MYSQL_NAME: 'npm' + DB_ENGINE: 'mysql' + DB_HOST: 'db-mysql' + DB_PORT: '3306' + DB_NAME: 'npm' + DB_USER: 'npm' + DB_PASSWORD: 'npmpass' depends_on: - db-mysql diff --git a/docker/docker-compose.ci.postgres.yml b/docker/docker-compose.ci.postgres.yml index c4468c68b..d9e5dfe2a 100644 --- a/docker/docker-compose.ci.postgres.yml +++ b/docker/docker-compose.ci.postgres.yml @@ -7,11 +7,12 @@ services: fullstack: environment: - DB_POSTGRES_HOST: 'db-postgres' - DB_POSTGRES_PORT: '5432' - DB_POSTGRES_USER: 'npm' - DB_POSTGRES_PASSWORD: 'npmpass' - DB_POSTGRES_NAME: 'npm' + DB_ENGINE: 'postgres' + DB_HOST: 'db-postgres' + DB_PORT: '5432' + DB_NAME: 'npm' + DB_USER: 'npm' + DB_PASSWORD: 'npmpass' depends_on: - db-postgres - authentik diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index 5abe057b0..9df2bac74 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -26,17 +26,20 @@ services: DEVELOPMENT: 'true' LE_STAGING: 'true' # db: - # DB_MYSQL_HOST: 'db' - # DB_MYSQL_PORT: '3306' - # DB_MYSQL_USER: 'npm' - # DB_MYSQL_PASSWORD: 'npm' - # DB_MYSQL_NAME: 'npm' + # DB_ENGINE: 'mysql' + # DB_HOST: 'db' + # DB_PORT: '3306' + # DB_NAME: 'npm' + # DB_USER: 'npm' + # DB_PASSWORD: 'npmpass' # db-postgres: - DB_POSTGRES_HOST: 'db-postgres' - DB_POSTGRES_PORT: '5432' - DB_POSTGRES_USER: 'npm' - DB_POSTGRES_PASSWORD: 'npmpass' - DB_POSTGRES_NAME: 'npm' + DB_ENGINE: 'postgres' + DB_HOST: 'db-postgres' + DB_PORT: '5432' + DB_NAME: 'npm' + DB_USER: 'npm' + DB_PASSWORD: 'npmpass' + # DB_ENGINE: 'sqlite' # DB_SQLITE_FILE: "/data/database.sqlite" # DISABLE_IPV6: "true" # Required for DNS Certificate provisioning testing: diff --git a/docs/src/advanced-config/index.md b/docs/src/advanced-config/index.md index 4a7c260eb..2dfd48f34 100644 --- a/docs/src/advanced-config/index.md +++ b/docs/src/advanced-config/index.md @@ -112,12 +112,13 @@ services: - '81:81' environment: # These are the settings to access your db - DB_MYSQL_HOST: "db" - DB_MYSQL_PORT: 3306 + DB_ENGINE: "mysql" + DB_HOST: "db" + DB_PORT: 3306 + DB_NAME: "npm" DB_MYSQL_USER: "npm" - # DB_MYSQL_PASSWORD: "npm" # use secret instead - DB_MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD - DB_MYSQL_NAME: "npm" + # DB_PASSWORD: "npmpass" # use secret instead + DB_PASSWORD__FILE: /run/secrets/MYSQL_PWD # If you would rather use Sqlite, remove all DB_MYSQL_* lines above # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' @@ -137,7 +138,7 @@ services: MYSQL_ROOT_PASSWORD__FILE: /run/secrets/DB_ROOT_PWD MYSQL_DATABASE: "npm" MYSQL_USER: "npm" - # MYSQL_PASSWORD: "npm" # use secret instead + # MYSQL_PASSWORD: "npmpass" # use secret instead MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD MARIADB_AUTO_UPGRADE: '1' volumes: diff --git a/docs/src/setup/index.md b/docs/src/setup/index.md index c2296da7f..663675d35 100644 --- a/docs/src/setup/index.md +++ b/docs/src/setup/index.md @@ -50,7 +50,7 @@ If you opt for the MySQL configuration you will have to provide the database ser It's easy to use another docker container for your database also and link it as part of the docker stack, so that's what the following examples are going to use. -Here is an example of what your `docker-compose.yml` will look like when using a MariaDB container: +Use `DB_ENGINE` environment variable with `mysql`/`mariadb` value, here is an example of what your `docker-compose.yml` will look like when using a MariaDB container: ```yml services: @@ -65,12 +65,13 @@ services: # Add any other Stream port you want to expose # - '21:21' # FTP environment: - # Mysql/Maria connection parameters: - DB_MYSQL_HOST: "db" - DB_MYSQL_PORT: 3306 - DB_MYSQL_USER: "npm" - DB_MYSQL_PASSWORD: "npm" - DB_MYSQL_NAME: "npm" + # MySQL/MariaDB connection parameters: + DB_ENGINE: 'mysql' # It also works with `mariadb` + DB_HOST: "db" + DB_PORT: 3306 + DB_NAME: "npm" + DB_USER: "npm" + DB_PASSWORD: "npm" # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' volumes: @@ -100,7 +101,7 @@ Please note, that `DB_MYSQL_*` environment variables will take precedent over `D ## Using Postgres database -Similar to the MySQL server setup: +Use `DB_ENGINE` environment variable with `postgres` value, similar to the MySQL server setup: ```yml services: @@ -115,12 +116,13 @@ services: # Add any other Stream port you want to expose # - '21:21' # FTP environment: - # Postgres parameters: - DB_POSTGRES_HOST: 'db' - DB_POSTGRES_PORT: '5432' - DB_POSTGRES_USER: 'npm' - DB_POSTGRES_PASSWORD: 'npmpass' - DB_POSTGRES_NAME: 'npm' + # PostgreSQL parameters: + DB_ENGINE: 'postgres' + DB_HOST: 'db' + DB_PORT: '5432' + DB_NAME: 'npm' + DB_USER: 'npm' + DB_PASSWORD: 'npmpass' # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' volumes: From a5c764418d1d63df112bf07ad1d63a464a0e3bae Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Thu, 24 Jul 2025 23:11:32 -0400 Subject: [PATCH 2/4] fix ci messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` /app/lib/config.js 5:1 error This group of assignments is not aligned align-assignments/align-assignments 14:9 error Missing space before value for key 'host' key-spacing 15:9 error Missing space before value for key 'port' key-spacing 19:9 error Missing space before value for key 'host' key-spacing 20:9 error Missing space before value for key 'port' key-spacing 24:9 error Missing space before value for key 'host' key-spacing 25:9 error Missing space before value for key 'port' key-spacing 30:2 error Missing semicolon semi 58:4 error This group of assignments is not aligned align-assignments/align-assignments 58:62 error Missing semicolon semi 76:4 error This group of assignments is not aligned align-assignments/align-assignments 76:62 error Missing semicolon semi ✖ 12 problems (12 errors, 0 warnings) 12 errors and 0 warnings potentially fixable with the `--fix` option. ``` --- backend/lib/config.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/backend/lib/config.js b/backend/lib/config.js index 90d0d8ea0..671a4a0f5 100644 --- a/backend/lib/config.js +++ b/backend/lib/config.js @@ -5,29 +5,29 @@ const logger = require('../logger').global; const keysFile = '/data/keys.json'; const mysqlEngine = 'mysql2'; const postgresEngine = 'pg'; -const sqliteEngine = 'knex-native'; +const sqliteEngine = 'knex-native'; const sqliteClientName = 'sqlite3'; const dataBaseEngines = { mysql: { engine: mysqlEngine, - host: '127.0.0.1', - port: 3306, + host: '127.0.0.1', + port: 3306, }, mariadb: { engine: mysqlEngine, - host: '127.0.0.1', - port: 3306, + host: '127.0.0.1', + port: 3306, }, postgres: { engine: postgresEngine, - host: '127.0.0.1', - port: 5432, + host: '127.0.0.1', + port: 5432, }, sqlite: { engine: sqliteEngine, }, -} +}; let instance = null; @@ -55,8 +55,8 @@ const configure = () => { if (envDataBaseEngne) { if (envDataBaseEngne === 'sqlite') { logger.info(`Using Sqlite: ${envSqliteFile}`); - const defaultConection = dataBaseEngines[envDataBaseEngne] - const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite'; + const defaultConection = dataBaseEngines[envDataBaseEngne]; + const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite'; instance = { database: { engine: defaultConection.engine, @@ -73,7 +73,7 @@ const configure = () => { } else { // we have enough mysql/mariadb/postgres creds to go with mysql/mariadb/postgres logger.info(`Using ${envDataBaseEngne} configuration`); - const defaultConection = dataBaseEngines[envDataBaseEngne] + const defaultConection = dataBaseEngines[envDataBaseEngne]; instance = { database: { engine: defaultConection.engine, From cfbc10e3e94413169bbc20aa40178b968d4e62d4 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Thu, 24 Jul 2025 23:30:57 -0400 Subject: [PATCH 3/4] fix with `yarn run eslint . --fix` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` /app/lib/config.js 58:4 error This group of assignments is not aligned align-assignments/align-assignments 76:4 error This group of assignments is not aligned align-assignments/align-assignments ✖ 2 problems (2 errors, 0 warnings) 2 errors and 0 warnings potentially fixable with the `--fix` option. ``` --- backend/lib/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/lib/config.js b/backend/lib/config.js index 671a4a0f5..fbd2fc219 100644 --- a/backend/lib/config.js +++ b/backend/lib/config.js @@ -57,7 +57,7 @@ const configure = () => { logger.info(`Using Sqlite: ${envSqliteFile}`); const defaultConection = dataBaseEngines[envDataBaseEngne]; const envSqliteFile = process.env.DB_SQLITE_FILE || '/data/database.sqlite'; - instance = { + instance = { database: { engine: defaultConection.engine, knex: { @@ -74,7 +74,7 @@ const configure = () => { // we have enough mysql/mariadb/postgres creds to go with mysql/mariadb/postgres logger.info(`Using ${envDataBaseEngne} configuration`); const defaultConection = dataBaseEngines[envDataBaseEngne]; - instance = { + instance = { database: { engine: defaultConection.engine, host: process.env.DB_HOST || defaultConection.host, From 66f7f864015893a4ba423b28ab4cf6ff2d26e519 Mon Sep 17 00:00:00 2001 From: EdwinBetanc0urt Date: Fri, 25 Jul 2025 13:05:31 -0400 Subject: [PATCH 4/4] fix env variables name. --- docker/docker-compose.ci.postgres.yml | 2 +- docker/docker-compose.dev.yml | 4 ++-- docs/src/advanced-config/index.md | 4 ++-- docs/src/setup/index.md | 6 +++--- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/docker/docker-compose.ci.postgres.yml b/docker/docker-compose.ci.postgres.yml index d9e5dfe2a..366c9c0df 100644 --- a/docker/docker-compose.ci.postgres.yml +++ b/docker/docker-compose.ci.postgres.yml @@ -22,9 +22,9 @@ services: db-postgres: image: postgres:latest environment: + POSTGRES_DB: 'npm' POSTGRES_USER: 'npm' POSTGRES_PASSWORD: 'npmpass' - POSTGRES_DB: 'npm' volumes: - psql_vol:/var/lib/postgresql/data - ./ci/postgres:/docker-entrypoint-initdb.d diff --git a/docker/docker-compose.dev.yml b/docker/docker-compose.dev.yml index 9df2bac74..ee29a7219 100644 --- a/docker/docker-compose.dev.yml +++ b/docker/docker-compose.dev.yml @@ -75,7 +75,7 @@ services: MYSQL_ROOT_PASSWORD: 'npm' MYSQL_DATABASE: 'npm' MYSQL_USER: 'npm' - MYSQL_PASSWORD: 'npm' + MYSQL_PASSWORD: 'npmpass' volumes: - db_data:/var/lib/mysql @@ -85,9 +85,9 @@ services: networks: - nginx_proxy_manager environment: + POSTGRES_DB: 'npm' POSTGRES_USER: 'npm' POSTGRES_PASSWORD: 'npmpass' - POSTGRES_DB: 'npm' volumes: - psql_data:/var/lib/postgresql/data - ./ci/postgres:/docker-entrypoint-initdb.d diff --git a/docs/src/advanced-config/index.md b/docs/src/advanced-config/index.md index 2dfd48f34..a527b7a89 100644 --- a/docs/src/advanced-config/index.md +++ b/docs/src/advanced-config/index.md @@ -116,7 +116,7 @@ services: DB_HOST: "db" DB_PORT: 3306 DB_NAME: "npm" - DB_MYSQL_USER: "npm" + DB_USER: "npm" # DB_PASSWORD: "npmpass" # use secret instead DB_PASSWORD__FILE: /run/secrets/MYSQL_PWD # If you would rather use Sqlite, remove all DB_MYSQL_* lines above @@ -137,7 +137,7 @@ services: # MYSQL_ROOT_PASSWORD: "npm" # use secret instead MYSQL_ROOT_PASSWORD__FILE: /run/secrets/DB_ROOT_PWD MYSQL_DATABASE: "npm" - MYSQL_USER: "npm" + MYSQL_USER: "npmpass" # MYSQL_PASSWORD: "npmpass" # use secret instead MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD MARIADB_AUTO_UPGRADE: '1' diff --git a/docs/src/setup/index.md b/docs/src/setup/index.md index 663675d35..e20c301cc 100644 --- a/docs/src/setup/index.md +++ b/docs/src/setup/index.md @@ -71,7 +71,7 @@ services: DB_PORT: 3306 DB_NAME: "npm" DB_USER: "npm" - DB_PASSWORD: "npm" + DB_PASSWORD: "npmpass" # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' volumes: @@ -87,7 +87,7 @@ services: MYSQL_ROOT_PASSWORD: 'npm' MYSQL_DATABASE: 'npm' MYSQL_USER: 'npm' - MYSQL_PASSWORD: 'npm' + MYSQL_PASSWORD: 'npmpass' MARIADB_AUTO_UPGRADE: '1' volumes: - ./mysql:/var/lib/mysql @@ -134,9 +134,9 @@ services: db: image: postgres:latest environment: + POSTGRES_DB: 'npm' POSTGRES_USER: 'npm' POSTGRES_PASSWORD: 'npmpass' - POSTGRES_DB: 'npm' volumes: - ./postgres:/var/lib/postgresql/data ```