config.js 1.7 KB

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