config.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. module.exports = {
  9. SUBSETS: ['auth', 'features', 'git', 'logging', 'site', 'theme', 'uploads'],
  10. /**
  11. * Load root config from disk
  12. *
  13. * @param {any} confPaths
  14. * @returns
  15. */
  16. init() {
  17. let confPaths = {
  18. config: path.join(wiki.ROOTPATH, 'config.yml'),
  19. data: path.join(wiki.SERVERPATH, 'app/data.yml'),
  20. dataRegex: path.join(wiki.SERVERPATH, 'app/regex.js')
  21. }
  22. let appconfig = {}
  23. let appdata = {}
  24. try {
  25. appconfig = yaml.safeLoad(
  26. cfgHelper.parseConfigValue(
  27. fs.readFileSync(confPaths.config, 'utf8')
  28. )
  29. )
  30. appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
  31. appdata.regex = require(confPaths.dataRegex)
  32. } catch (ex) {
  33. console.error(ex)
  34. process.exit(1)
  35. }
  36. // Merge with defaults
  37. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  38. // Check port
  39. if (appconfig.port < 1) {
  40. appconfig.port = process.env.PORT || 80
  41. }
  42. // Convert booleans
  43. appconfig.public = (appconfig.public === true || _.toLower(appconfig.public) === 'true')
  44. // List authentication strategies
  45. wiki.config = appconfig
  46. wiki.data = appdata
  47. // List authentication strategies
  48. // appconfig.authStrategies = {
  49. // list: _.filter(appconfig.auth, ['enabled', true]),
  50. // socialEnabled: (_.chain(appconfig.auth).omit('local').filter(['enabled', true]).value().length > 0)
  51. // }
  52. // if (appconfig.authStrategies.list.length < 1) {
  53. // console.error(new Error('You must enable at least 1 authentication strategy!'))
  54. // process.exit(1)
  55. // }
  56. },
  57. /**
  58. * Load config from DB
  59. *
  60. * @param {Array} subsets Array of subsets to load
  61. * @returns Promise
  62. */
  63. loadFromDb(subsets) {
  64. if (!_.isArray(subsets) || subsets.length === 0) {
  65. subsets = this.SUBSETS
  66. }
  67. return wiki.db.Setting.findAll({
  68. attributes: ['key', 'config'],
  69. where: {
  70. key: {
  71. $in: subsets
  72. }
  73. }
  74. }).then(results => {
  75. if (_.isArray(results) && results.length > 0) {
  76. results.forEach(result => {
  77. wiki.config[result.key] = result.config
  78. })
  79. return true
  80. } else {
  81. return Promise.reject(new Error('Invalid DB Configuration result set'))
  82. }
  83. })
  84. }
  85. }