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.

68 lines
1.6 KiB

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