From ffe3db8c083a5a9a1f2ef4ce4308b17b254020b8 Mon Sep 17 00:00:00 2001 From: Julian Reinhardt Date: Tue, 2 Nov 2021 12:22:39 +0100 Subject: [PATCH] Falls back to SQLite if no environment variables are provided --- backend/index.js | 133 ++++++++++++++++++++++++----------------------- 1 file changed, 67 insertions(+), 66 deletions(-) diff --git a/backend/index.js b/backend/index.js index f4f7951..8d42d09 100644 --- a/backend/index.js +++ b/backend/index.js @@ -44,84 +44,85 @@ async function appStart () { async function createDbConfigFromEnvironment() { return new Promise((resolve, reject) => { - const envMysqlHost = process.env.DB_MYSQL_HOST || null; - const envMysqlPort = process.env.DB_MYSQL_PORT || null; - const envMysqlUser = process.env.DB_MYSQL_USER || null; - const envMysqlName = process.env.DB_MYSQL_NAME || null; - const envSqliteFile = process.env.DB_SQLITE_FILE || null; + const envMysqlHost = process.env.DB_MYSQL_HOST || null; + const envMysqlPort = process.env.DB_MYSQL_PORT || null; + const envMysqlUser = process.env.DB_MYSQL_USER || null; + const envMysqlName = process.env.DB_MYSQL_NAME || null; + let envSqliteFile = process.env.DB_SQLITE_FILE || null; - if ((envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) || envSqliteFile) { - const fs = require('fs'); - const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json'; - let configData = {}; + const fs = require('fs'); + const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json'; + let configData = {}; - try { - configData = require(filename); - } catch (err) { - // do nothing - } + try { + configData = require(filename); + } catch (err) { + // do nothing + } - if (configData.database && configData.database.engine && !configData.database.fromEnv) { - logger.info('Manual db configuration already exists, skipping config creation from environment variables'); + if (configData.database && configData.database.engine && !configData.database.fromEnv) { + logger.info('Manual db configuration already exists, skipping config creation from environment variables'); + resolve(); + return; + } + + if ((!envMysqlHost || !envMysqlPort || !envMysqlUser || !envMysqlName) && !envSqliteFile){ + envSqliteFile = '/data/database.sqlite'; + logger.info(`No valid environment variables for database provided, using default SQLite file '${envSqliteFile}'`); + } + + if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) { + const newConfig = { + fromEnv: true, + engine: 'mysql', + host: envMysqlHost, + port: envMysqlPort, + user: envMysqlUser, + password: process.env.DB_MYSQL_PASSWORD, + name: envMysqlName, + }; + + if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { + // Config is unchanged, skip overwrite resolve(); return; } - if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) { - const newConfig = { - fromEnv: true, - engine: 'mysql', - host: envMysqlHost, - port: envMysqlPort, - user: envMysqlUser, - password: process.env.DB_MYSQL_PASSWORD, - name: envMysqlName, - }; + logger.info('Generating MySQL knex configuration from environment variables'); + configData.database = newConfig; - if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { - // Config is unchanged, skip overwrite - resolve(); - return; + } else { + const newConfig = { + fromEnv: true, + engine: 'knex-native', + knex: { + client: 'sqlite3', + connection: { + filename: envSqliteFile + }, + useNullAsDefault: true } - - logger.info('Generating MySQL db configuration from environment variables'); - configData.database = newConfig; - - } else { - const newConfig = { - fromEnv: true, - engine: 'knex-native', - knex: { - client: 'sqlite3', - connection: { - filename: envSqliteFile - }, - useNullAsDefault: true - } - }; - if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { - // Config is unchanged, skip overwrite - resolve(); - return; - } - - logger.info('Generating Sqlite db configuration from environment variables'); - configData.database = newConfig; + }; + if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { + // Config is unchanged, skip overwrite + resolve(); + return; } - // Write config - fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => { - if (err) { - logger.error('Could not write db config to config file: ' + filename); - reject(err); - } else { - logger.info('Wrote db configuration to config file: ' + filename); - resolve(); - } - }); - } else { - resolve(); + logger.info('Generating SQLite knex configuration'); + configData.database = newConfig; } + + // Write config + fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => { + if (err) { + logger.error('Could not write db config to config file: ' + filename); + reject(err); + } else { + logger.debug('Wrote db configuration to config file: ' + filename); + resolve(); + } + }); }); }