2017-04-02 23:56:47 +00:00
|
|
|
const _ = require('lodash')
|
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')
|
|
|
|
|
|
|
|
/* 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 = {
|
|
|
|
config: path.join(wiki.ROOTPATH, 'config.yml'),
|
|
|
|
data: path.join(wiki.SERVERPATH, 'app/data.yml'),
|
|
|
|
dataRegex: path.join(wiki.SERVERPATH, 'app/regex.js')
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
} catch (ex) {
|
|
|
|
console.error(ex)
|
|
|
|
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
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
if (appconfig.port < 1) {
|
|
|
|
appconfig.port = process.env.PORT || 80
|
|
|
|
}
|
2017-04-19 00:31:07 +00:00
|
|
|
|
2017-09-10 05:41:22 +00:00
|
|
|
appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
|
2017-08-06 15:17:53 +00:00
|
|
|
|
2017-07-29 21:33:08 +00:00
|
|
|
wiki.config = appconfig
|
|
|
|
wiki.data = appdata
|
2017-10-23 01:58:07 +00:00
|
|
|
wiki.version = require(path.join(wiki.ROOTPATH, 'package.json')).version
|
2017-07-29 21:33:08 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load config from DB
|
|
|
|
*
|
|
|
|
* @param {Array} subsets Array of subsets to load
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
2017-12-17 04:41:16 +00:00
|
|
|
async loadFromDb(subsets) {
|
2017-07-29 21:33:08 +00:00
|
|
|
if (!_.isArray(subsets) || subsets.length === 0) {
|
2017-10-08 21:21:36 +00:00
|
|
|
subsets = wiki.data.configNamespaces
|
2017-07-29 21:33:08 +00:00
|
|
|
}
|
2017-04-02 23:56:47 +00:00
|
|
|
|
2017-12-17 04:41:16 +00:00
|
|
|
let results = await wiki.db.Setting.findAll({
|
2017-07-29 21:33:08 +00:00
|
|
|
attributes: ['key', 'config'],
|
|
|
|
where: {
|
|
|
|
key: {
|
|
|
|
$in: subsets
|
|
|
|
}
|
|
|
|
}
|
2017-12-17 04:41:16 +00:00
|
|
|
})
|
|
|
|
if (_.isArray(results) && results.length === subsets.length) {
|
|
|
|
results.forEach(result => {
|
|
|
|
wiki.config[result.key] = result.config
|
|
|
|
})
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
wiki.logger.warn('DB Configuration is empty or incomplete.')
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Save config to DB
|
|
|
|
*
|
|
|
|
* @param {Array} subsets Array of subsets to save
|
|
|
|
* @returns Promise
|
|
|
|
*/
|
|
|
|
async saveToDb(subsets) {
|
|
|
|
if (!_.isArray(subsets) || subsets.length === 0) {
|
|
|
|
subsets = wiki.data.configNamespaces
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
for (let set of subsets) {
|
|
|
|
await wiki.db.Setting.upsert({
|
|
|
|
key: set,
|
|
|
|
config: _.get(wiki.config, set, {})
|
2017-07-29 21:33:08 +00:00
|
|
|
})
|
|
|
|
}
|
2017-12-17 04:41:16 +00:00
|
|
|
} catch (err) {
|
|
|
|
wiki.logger.error(`Failed to save configuration to DB: ${err.message}`)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
2017-04-02 23:56:47 +00:00
|
|
|
}
|
|
|
|
}
|