2017-04-02 23:56:47 +00:00
|
|
|
'use strict'
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
const yaml = require('js-yaml')
|
|
|
|
const _ = require('lodash')
|
2017-04-28 21:58:38 +00:00
|
|
|
const path = require('path')
|
2017-05-15 01:20:40 +00:00
|
|
|
const cfgHelper = require('../helpers/config')
|
2017-05-13 14:41:33 +00:00
|
|
|
|
2017-04-02 23:56:47 +00:00
|
|
|
/**
|
|
|
|
* Load Application Configuration
|
|
|
|
*
|
|
|
|
* @param {Object} confPaths Path to the configuration files
|
|
|
|
* @return {Object} Application Configuration
|
|
|
|
*/
|
|
|
|
module.exports = (confPaths) => {
|
|
|
|
confPaths = _.defaults(confPaths, {
|
2017-04-28 21:58:38 +00:00
|
|
|
config: path.join(ROOTPATH, 'config.yml'),
|
|
|
|
data: path.join(SERVERPATH, 'app/data.yml'),
|
|
|
|
dataRegex: path.join(SERVERPATH, 'app/regex.js')
|
2017-04-02 23:56:47 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
let appconfig = {}
|
|
|
|
let appdata = {}
|
|
|
|
|
|
|
|
try {
|
2017-05-13 18:44:04 +00:00
|
|
|
appconfig = yaml.safeLoad(
|
2017-05-15 01:20:40 +00:00
|
|
|
cfgHelper.parseConfigValue(
|
|
|
|
fs.readFileSync(confPaths.config, 'utf8')
|
2017-05-13 18:44:04 +00:00
|
|
|
)
|
|
|
|
)
|
2017-04-02 23:56:47 +00:00
|
|
|
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) {
|
2017-04-10 01:13:53 +00:00
|
|
|
console.error(ex)
|
2017-04-02 23:56:47 +00:00
|
|
|
process.exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merge with defaults
|
|
|
|
|
|
|
|
appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
|
|
|
|
|
2017-05-13 14:41:33 +00:00
|
|
|
// Check port
|
2017-04-19 00:31:07 +00:00
|
|
|
|
|
|
|
if (appconfig.port < 1) {
|
|
|
|
appconfig.port = process.env.PORT || 80
|
|
|
|
}
|
|
|
|
|
2017-08-06 15:17:53 +00:00
|
|
|
// Convert booleans
|
|
|
|
|
|
|
|
appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
|
|
|
|
|
2017-04-02 23:56:47 +00:00
|
|
|
// List authentication strategies
|
|
|
|
|
2017-05-13 14:41:33 +00:00
|
|
|
appconfig.authStrategies = {
|
|
|
|
list: _.filter(appconfig.auth, ['enabled', true]),
|
2017-08-11 01:00:18 +00:00
|
|
|
socialEnabled: (_.chain(appconfig.auth).omit(['local', 'ldap']).filter(['enabled', true]).value().length > 0)
|
2017-05-13 14:41:33 +00:00
|
|
|
}
|
|
|
|
if (appconfig.authStrategies.list.length < 1) {
|
|
|
|
console.error(new Error('You must enable at least 1 authentication strategy!'))
|
|
|
|
process.exit(1)
|
2017-04-02 23:56:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
config: appconfig,
|
|
|
|
data: appdata
|
|
|
|
}
|
|
|
|
}
|