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.

63 lines
1.5 KiB

  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 cfgHelper = require('../helpers/config')
  7. /**
  8. * Load Application Configuration
  9. *
  10. * @param {Object} confPaths Path to the configuration files
  11. * @return {Object} Application Configuration
  12. */
  13. module.exports = (confPaths) => {
  14. confPaths = _.defaults(confPaths, {
  15. config: path.join(ROOTPATH, 'config.yml'),
  16. data: path.join(SERVERPATH, 'app/data.yml'),
  17. dataRegex: path.join(SERVERPATH, 'app/regex.js')
  18. })
  19. let appconfig = {}
  20. let appdata = {}
  21. try {
  22. appconfig = yaml.safeLoad(
  23. cfgHelper.parseConfigValue(
  24. fs.readFileSync(confPaths.config, 'utf8')
  25. )
  26. )
  27. appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
  28. appdata.regex = require(confPaths.dataRegex)
  29. } catch (ex) {
  30. console.error(ex)
  31. process.exit(1)
  32. }
  33. // Merge with defaults
  34. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  35. // Check port
  36. if (appconfig.port < 1) {
  37. appconfig.port = process.env.PORT || 80
  38. }
  39. // List authentication strategies
  40. appconfig.authStrategies = {
  41. list: _.filter(appconfig.auth, ['enabled', true]),
  42. socialEnabled: (_.chain(appconfig.auth).omit('local').filter(['enabled', true]).value().length > 0)
  43. }
  44. if (appconfig.authStrategies.list.length < 1) {
  45. console.error(new Error('You must enable at least 1 authentication strategy!'))
  46. process.exit(1)
  47. }
  48. return {
  49. config: appconfig,
  50. data: appdata
  51. }
  52. }