2
0

config.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. 'use strict'
  2. const fs = require('fs')
  3. const yaml = require('js-yaml')
  4. const _ = require('lodash')
  5. const path = require('path')
  6. const deepMap = (obj, iterator, context) => {
  7. return _.transform(obj, (result, val, key) => {
  8. result[key] = _.isObject(val)
  9. ? deepMap(val, iterator, context)
  10. : iterator.call(context, val, key, obj)
  11. })
  12. }
  13. _.mixin({ deepMap })
  14. /**
  15. * Load Application Configuration
  16. *
  17. * @param {Object} confPaths Path to the configuration files
  18. * @return {Object} Application Configuration
  19. */
  20. module.exports = (confPaths) => {
  21. confPaths = _.defaults(confPaths, {
  22. config: path.join(ROOTPATH, 'config.yml'),
  23. data: path.join(SERVERPATH, 'app/data.yml'),
  24. dataRegex: path.join(SERVERPATH, 'app/regex.js')
  25. })
  26. let appconfig = {}
  27. let appdata = {}
  28. try {
  29. appconfig = yaml.safeLoad(_.deepMap(fs.readFileSync(confPaths.config, 'utf8'), c => {
  30. return _.replace(c, (/\$\([A-Z0-9_]+\)/g, (m) => { return process.env[m] }))
  31. }))
  32. appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
  33. appdata.regex = require(confPaths.dataRegex)
  34. } catch (ex) {
  35. console.error(ex)
  36. process.exit(1)
  37. }
  38. // Merge with defaults
  39. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  40. // Check port
  41. if (appconfig.port < 1) {
  42. appconfig.port = process.env.PORT || 80
  43. }
  44. // List authentication strategies
  45. appconfig.authStrategies = {
  46. list: _.filter(appconfig.auth, ['enabled', true]),
  47. socialEnabled: (_.chain(appconfig.auth).omit('local').filter(['enabled', true]).value().length > 0)
  48. }
  49. if (appconfig.authStrategies.list.length < 1) {
  50. console.error(new Error('You must enable at least 1 authentication strategy!'))
  51. process.exit(1)
  52. }
  53. return {
  54. config: appconfig,
  55. data: appdata
  56. }
  57. }