wikijs-fork/libs/config.js

69 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-04-02 23:56:47 +00:00
'use strict'
const fs = require('fs')
const yaml = require('js-yaml')
const _ = require('lodash')
/**
* Load Application Configuration
*
* @param {Object} confPaths Path to the configuration files
* @return {Object} Application Configuration
*/
module.exports = (confPaths) => {
confPaths = _.defaults(confPaths, {
config: './config.yml',
2017-04-14 18:15:11 +00:00
data: './app/data.yml',
dataRegex: '../app/regex.js'
2017-04-02 23:56:47 +00:00
})
let appconfig = {}
let appdata = {}
try {
appconfig = yaml.safeLoad(fs.readFileSync(confPaths.config, 'utf8'))
appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
2017-04-14 18:15:11 +00:00
appdata.regex = require(confPaths.dataRegex)
2017-04-02 23:56:47 +00:00
} catch (ex) {
console.error(ex)
2017-04-02 23:56:47 +00:00
process.exit(1)
}
// Merge with defaults
appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
2017-04-19 00:31:07 +00:00
// Using ENV variables?
if (appconfig.port < 1) {
appconfig.port = process.env.PORT || 80
}
if (_.startsWith(appconfig.db, '$')) {
appconfig.db = process.env[appconfig.db.slice(1)]
}
2017-04-02 23:56:47 +00:00
// List authentication strategies
if (appdata.capabilities.manyAuthProviders) {
appconfig.authStrategies = {
list: _.filter(appconfig.auth, ['enabled', true]),
socialEnabled: (_.chain(appconfig.auth).omit('local').reject({ enabled: false }).value().length > 0)
}
if (appconfig.authStrategies.list.length < 1) {
console.error(new Error('You must enable at least 1 authentication strategy!'))
2017-04-02 23:56:47 +00:00
process.exit(1)
}
} else {
appconfig.authStrategies = {
list: { local: { enabled: true } },
socialEnabled: false
}
}
return {
config: appconfig,
data: appdata
}
}