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..fbd2fc219 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..366c9c0df 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 @@ -21,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 5abe057b0..ee29a7219 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: @@ -72,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 @@ -82,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 4a7c260eb..a527b7a89 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_MYSQL_USER: "npm" - # DB_MYSQL_PASSWORD: "npm" # use secret instead - DB_MYSQL_PASSWORD__FILE: /run/secrets/MYSQL_PWD - DB_MYSQL_NAME: "npm" + DB_ENGINE: "mysql" + DB_HOST: "db" + DB_PORT: 3306 + DB_NAME: "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 # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' @@ -136,8 +137,8 @@ services: # MYSQL_ROOT_PASSWORD: "npm" # use secret instead MYSQL_ROOT_PASSWORD__FILE: /run/secrets/DB_ROOT_PWD MYSQL_DATABASE: "npm" - MYSQL_USER: "npm" - # MYSQL_PASSWORD: "npm" # use secret instead + MYSQL_USER: "npmpass" + # 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..e20c301cc 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: "npmpass" # Uncomment this if IPv6 is not enabled on your host # DISABLE_IPV6: 'true' volumes: @@ -86,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 @@ -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: @@ -132,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 ```