|
2 | 2 |
|
3 | 3 | const logger = require('./logger').global; |
4 | 4 |
|
5 | | -function appStart () { |
| 5 | +async function appStart () { |
| 6 | + // Create config file db settings if environment variables have been set |
| 7 | + await createDbConfigFromEnvironment(); |
| 8 | + |
6 | 9 | const migrate = require('./migrate'); |
7 | 10 | const setup = require('./setup'); |
8 | 11 | const app = require('./app'); |
9 | 12 | const apiValidator = require('./lib/validator/api'); |
10 | 13 | const internalCertificate = require('./internal/certificate'); |
11 | 14 | const internalIpRanges = require('./internal/ip_ranges'); |
12 | 15 |
|
| 16 | + |
13 | 17 | return migrate.latest() |
14 | 18 | .then(setup) |
15 | 19 | .then(() => { |
@@ -39,6 +43,87 @@ function appStart () { |
39 | 43 | }); |
40 | 44 | } |
41 | 45 |
|
| 46 | +async function createDbConfigFromEnvironment(){ |
| 47 | + return new Promise((resolve, reject) => { |
| 48 | + const envMysqlHost = process.env.DB_MYSQL_HOST; |
| 49 | + const envMysqlPort = process.env.DB_MYSQL_PORT; |
| 50 | + const envMysqlUser = process.env.DB_MYSQL_USER; |
| 51 | + const envMysqlName = process.env.DB_MYSQL_NAME; |
| 52 | + const envSqliteFile = process.env.DB_SQLITE_FILE; |
| 53 | + if ((envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) || envSqliteFile) { |
| 54 | + const fs = require('fs'); |
| 55 | + const filename = (process.env.NODE_CONFIG_DIR || './config') + '/' + (process.env.NODE_ENV || 'default') + '.json'; |
| 56 | + let configData = {}; |
| 57 | + |
| 58 | + try { |
| 59 | + configData = require(filename); |
| 60 | + } catch (err) { |
| 61 | + // do nothing |
| 62 | + } |
| 63 | + |
| 64 | + if (configData.database && configData.database.engine && !configData.database.fromEnv) { |
| 65 | + logger.info('Manual db configuration already exists, skipping config creation from environment variables'); |
| 66 | + resolve(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + if (envMysqlHost && envMysqlPort && envMysqlUser && envMysqlName) { |
| 71 | + const newConfig = { |
| 72 | + fromEnv: true, |
| 73 | + engine: 'mysql', |
| 74 | + host: envMysqlHost, |
| 75 | + port: envMysqlPort, |
| 76 | + user: envMysqlUser, |
| 77 | + password: process.env.DB_MYSQL_PASSWORD, |
| 78 | + name: envMysqlName, |
| 79 | + }; |
| 80 | + |
| 81 | + if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { |
| 82 | + // Config is unchanged, skip overwrite |
| 83 | + resolve(); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + logger.info('Generating MySQL db configuration from environment variables'); |
| 88 | + configData.database = newConfig; |
| 89 | + |
| 90 | + } else { |
| 91 | + const newConfig = { |
| 92 | + fromEnv: true, |
| 93 | + engine: 'knex-native', |
| 94 | + knex: { |
| 95 | + client: 'sqlite3', |
| 96 | + connection: { |
| 97 | + filename: envSqliteFile |
| 98 | + } |
| 99 | + } |
| 100 | + }; |
| 101 | + if (JSON.stringify(configData.database) === JSON.stringify(newConfig)) { |
| 102 | + // Config is unchanged, skip overwrite |
| 103 | + resolve(); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + logger.info('Generating Sqlite db configuration from environment variables'); |
| 108 | + configData.database = newConfig; |
| 109 | + } |
| 110 | + |
| 111 | + // Write config |
| 112 | + fs.writeFile(filename, JSON.stringify(configData, null, 2), (err) => { |
| 113 | + if (err) { |
| 114 | + logger.error('Could not write db config to config file: ' + filename); |
| 115 | + reject(err); |
| 116 | + } else { |
| 117 | + logger.info('Wrote db configuration to config file: ' + filename); |
| 118 | + resolve(); |
| 119 | + } |
| 120 | + }); |
| 121 | + } else { |
| 122 | + // resolve(); |
| 123 | + } |
| 124 | + }); |
| 125 | +} |
| 126 | + |
42 | 127 | try { |
43 | 128 | appStart(); |
44 | 129 | } catch (err) { |
|
0 commit comments