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.

88 lines
2.0 KiB

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