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.

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