config.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* global wiki */
  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. module.exports = {
  8. /**
  9. * Load root config from disk
  10. */
  11. init() {
  12. let confPaths = {
  13. config: path.join(wiki.ROOTPATH, 'config.yml'),
  14. data: path.join(wiki.SERVERPATH, 'app/data.yml'),
  15. dataRegex: path.join(wiki.SERVERPATH, 'app/regex.js')
  16. }
  17. let appconfig = {}
  18. let appdata = {}
  19. try {
  20. appconfig = yaml.safeLoad(
  21. cfgHelper.parseConfigValue(
  22. fs.readFileSync(confPaths.config, 'utf8')
  23. )
  24. )
  25. appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
  26. appdata.regex = require(confPaths.dataRegex)
  27. } catch (ex) {
  28. console.error(ex)
  29. process.exit(1)
  30. }
  31. // Merge with defaults
  32. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  33. // Check port
  34. if (appconfig.port < 1) {
  35. appconfig.port = process.env.PORT || 80
  36. }
  37. // Convert booleans
  38. appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
  39. // List authentication strategies
  40. wiki.config = appconfig
  41. wiki.data = appdata
  42. },
  43. /**
  44. * Load config from DB
  45. *
  46. * @param {Array} subsets Array of subsets to load
  47. * @returns Promise
  48. */
  49. loadFromDb(subsets) {
  50. if (!_.isArray(subsets) || subsets.length === 0) {
  51. subsets = wiki.data.configNamespaces
  52. }
  53. return wiki.db.Setting.findAll({
  54. attributes: ['key', 'config'],
  55. where: {
  56. key: {
  57. $in: subsets
  58. }
  59. }
  60. }).then(results => {
  61. if (_.isArray(results) && results.length === subsets.length) {
  62. results.forEach(result => {
  63. wiki.config[result.key] = result.config
  64. })
  65. return true
  66. } else {
  67. wiki.logger.warn('DB Configuration is empty or incomplete.')
  68. return false
  69. }
  70. })
  71. }
  72. }