mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.5 KiB
106 lines
2.5 KiB
8 years ago
|
const _ = require('lodash')
|
||
8 years ago
|
const cfgHelper = require('../helpers/config')
|
||
7 years ago
|
const fs = require('fs')
|
||
|
const path = require('path')
|
||
|
const yaml = require('js-yaml')
|
||
|
|
||
|
/* global wiki */
|
||
8 years ago
|
|
||
8 years ago
|
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')
|
||
|
)
|
||
8 years ago
|
)
|
||
8 years ago
|
appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
|
||
|
appdata.regex = require(confPaths.dataRegex)
|
||
|
} catch (ex) {
|
||
|
console.error(ex)
|
||
|
process.exit(1)
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
// Merge with defaults
|
||
8 years ago
|
|
||
8 years ago
|
appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
|
||
8 years ago
|
|
||
8 years ago
|
if (appconfig.port < 1) {
|
||
|
appconfig.port = process.env.PORT || 80
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
|
||
8 years ago
|
|
||
8 years ago
|
wiki.config = appconfig
|
||
|
wiki.data = appdata
|
||
7 years ago
|
wiki.version = require(path.join(wiki.ROOTPATH, 'package.json')).version
|
||
8 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* Load config from DB
|
||
|
*
|
||
|
* @param {Array} subsets Array of subsets to load
|
||
|
* @returns Promise
|
||
|
*/
|
||
7 years ago
|
async loadFromDb(subsets) {
|
||
8 years ago
|
if (!_.isArray(subsets) || subsets.length === 0) {
|
||
7 years ago
|
subsets = wiki.data.configNamespaces
|
||
8 years ago
|
}
|
||
8 years ago
|
|
||
7 years ago
|
let results = await wiki.db.Setting.findAll({
|
||
8 years ago
|
attributes: ['key', 'config'],
|
||
|
where: {
|
||
|
key: {
|
||
|
$in: subsets
|
||
|
}
|
||
|
}
|
||
7 years ago
|
})
|
||
|
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, {})
|
||
8 years ago
|
})
|
||
|
}
|
||
7 years ago
|
} catch (err) {
|
||
|
wiki.logger.error(`Failed to save configuration to DB: ${err.message}`)
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
8 years ago
|
}
|
||
|
}
|