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.

67 lines
1.6 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. // Convert booleans
  40. appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
  41. // List authentication strategies
  42. appconfig.authStrategies = {
  43. list: _.filter(appconfig.auth, ['enabled', true]),
  44. socialEnabled: (_.chain(appconfig.auth).omit('local').filter(['enabled', true]).value().length > 0)
  45. }
  46. if (appconfig.authStrategies.list.length < 1) {
  47. console.error(new Error('You must enable at least 1 authentication strategy!'))
  48. process.exit(1)
  49. }
  50. return {
  51. config: appconfig,
  52. data: appdata
  53. }
  54. }