2017-04-02 23:56:47 +00:00
|
|
|
const _ = require('lodash')
|
2018-08-06 03:03:46 +00:00
|
|
|
const chalk = require('chalk')
|
2017-05-15 01:20:40 +00:00
|
|
|
const cfgHelper = require('../helpers/config')
|
2018-02-04 05:53:13 +00:00
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
|
|
|
const yaml = require('js-yaml')
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-05-13 14:41:33 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
module.exports = {
|
|
|
|
/**
|
|
|
|
* Load root config from disk
|
|
|
|
*/
|
|
|
|
init() {
|
|
|
|
let confPaths = {
|
2018-03-05 20:49:36 +00:00
|
|
|
config: path.join(WIKI.ROOTPATH, 'config.yml'),
|
|
|
|
data: path.join(WIKI.SERVERPATH, 'app/data.yml'),
|
|
|
|
dataRegex: path.join(WIKI.SERVERPATH, 'app/regex.js')
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|
|
|
|
|
2018-08-28 04:23:05 +00:00
|
|
|
if (process.env.dockerdev) {
|
2020-03-07 22:55:52 +00:00
|
|
|
confPaths.config = path.join(WIKI.ROOTPATH, `dev/containers/config.yml`)
|
2018-08-28 04:23:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-22 20:43:47 +00:00
|
|
|
if (process.env.CONFIG_FILE) {
|
|
|
|
confPaths.config = path.resolve(WIKI.ROOTPATH, process.env.CONFIG_FILE)
|
|
|
|
}
|
|
|
|
|
2018-08-06 03:03:46 +00:00
|
|
|
process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))
|
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
let appconfig = {}
|
|
|
|
let appdata = {}
|
|
|
|
|
|
|
|
try {
|
|
|
|
appconfig = yaml.safeLoad(
|
|
|
|
cfgHelper.parseConfigValue(
|
|
|
|
fs.readFileSync(confPaths.config, 'utf8')
|
|
|
|
)
|
2017-05-13 18:44:04 +00:00
|
|
|
)
|
2017-07-29 21:33:08 +00:00
|
|
|
appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
|
|
|
|
appdata.regex = require(confPaths.dataRegex)
|
2018-08-06 03:03:46 +00:00
|
|
|
console.info(chalk.green.bold(`OK`))
|
|
|
|
} catch (err) {
|
|
|
|
console.error(chalk.red.bold(`FAILED`))
|
|
|
|
console.error(err.message)
|
|
|
|
|
|
|
|
console.error(chalk.red.bold(`>>> Unable to read configuration file! Did you create the config.yml file?`))
|
2017-07-29 21:33:08 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
// Merge with defaults
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2019-06-02 01:38:21 +00:00
|
|
|
if (appconfig.port < 1 || process.env.HEROKU) {
|
2017-07-29 21:33:08 +00:00
|
|
|
appconfig.port = process.env.PORT || 80
|
|
|
|
}
|
2017-04-19 00:31:07 +00:00
|
|
|
|
2019-01-13 20:37:45 +00:00
|
|
|
const packageInfo = require(path.join(WIKI.ROOTPATH, 'package.json'))
|
|
|
|
|
2019-09-21 05:10:45 +00:00
|
|
|
// Load DB Password from Docker Secret File
|
|
|
|
if (process.env.DB_PASS_FILE) {
|
2019-09-22 02:25:54 +00:00
|
|
|
console.info(chalk.blue(`DB_PASS_FILE is defined. Will use secret from file.`))
|
2019-09-21 05:10:45 +00:00
|
|
|
try {
|
2019-09-22 02:25:54 +00:00
|
|
|
appconfig.db.pass = fs.readFileSync(process.env.DB_PASS_FILE, 'utf8').trim()
|
2019-09-21 05:10:45 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(chalk.red.bold(`>>> Failed to read Docker Secret File using path defined in DB_PASS_FILE env variable!`))
|
|
|
|
console.error(err.message)
|
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.config = appconfig
|
|
|
|
WIKI.data = appdata
|
2019-01-13 20:37:45 +00:00
|
|
|
WIKI.version = packageInfo.version
|
|
|
|
WIKI.releaseDate = packageInfo.releaseDate
|
2019-12-19 04:45:19 +00:00
|
|
|
WIKI.devMode = (packageInfo.dev === true)
|
2017-07-29 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load config from DB
|
|
|
|
*/
|
2018-05-28 18:46:55 +00:00
|
|
|
async loadFromDb() {
|
2018-07-30 02:23:33 +00:00
|
|
|
let conf = await WIKI.models.settings.getConfig()
|
2018-05-28 18:46:55 +00:00
|
|
|
if (conf) {
|
|
|
|
WIKI.config = _.defaultsDeep(conf, WIKI.config)
|
2017-12-17 04:41:16 +00:00
|
|
|
} else {
|
2018-05-28 18:46:55 +00:00
|
|
|
WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
|
|
|
|
WIKI.config.setup = true
|
2017-12-17 04:41:16 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Save config to DB
|
|
|
|
*
|
2018-05-28 18:46:55 +00:00
|
|
|
* @param {Array} keys Array of keys to save
|
2017-12-17 04:41:16 +00:00
|
|
|
* @returns Promise
|
|
|
|
*/
|
2018-05-28 18:46:55 +00:00
|
|
|
async saveToDb(keys) {
|
2017-12-17 04:41:16 +00:00
|
|
|
try {
|
2018-05-28 18:46:55 +00:00
|
|
|
for (let key of keys) {
|
2018-05-28 23:36:35 +00:00
|
|
|
let value = _.get(WIKI.config, key, null)
|
|
|
|
if (!_.isPlainObject(value)) {
|
|
|
|
value = { v: value }
|
|
|
|
}
|
2018-07-30 02:23:33 +00:00
|
|
|
let affectedRows = await WIKI.models.settings.query().patch({ value }).where('key', key)
|
2018-05-28 18:46:55 +00:00
|
|
|
if (affectedRows === 0 && value) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.settings.query().insert({ key, value })
|
2018-05-28 18:46:55 +00:00
|
|
|
}
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|
2017-12-17 04:41:16 +00:00
|
|
|
} catch (err) {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
|
2017-12-17 04:41:16 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2019-02-23 23:22:25 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Apply Dev Flags
|
|
|
|
*/
|
|
|
|
async applyFlags() {
|
|
|
|
WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
|
2017-04-02 23:56:47 +00:00
|
|
|
}
|
|
|
|
}
|