config.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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(
  30. _.replace(
  31. fs.readFileSync(confPaths.config, 'utf8'),
  32. (/\$\([A-Z0-9_]+\)/g,
  33. (m) => { return process.env[m] })
  34. )
  35. )
  36. appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
  37. appdata.regex = require(confPaths.dataRegex)
  38. } catch (ex) {
  39. console.error(ex)
  40. process.exit(1)
  41. }
  42. // Merge with defaults
  43. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  44. // Check port
  45. if (appconfig.port < 1) {
  46. appconfig.port = process.env.PORT || 80
  47. }
  48. // List authentication strategies
  49. appconfig.authStrategies = {
  50. list: _.filter(appconfig.auth, ['enabled', true]),
  51. socialEnabled: (_.chain(appconfig.auth).omit('local').filter(['enabled', true]).value().length > 0)
  52. }
  53. if (appconfig.authStrategies.list.length < 1) {
  54. console.error(new Error('You must enable at least 1 authentication strategy!'))
  55. process.exit(1)
  56. }
  57. return {
  58. config: appconfig,
  59. data: appdata
  60. }
  61. }