config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. const _ = require('lodash')
  2. const chalk = require('chalk')
  3. const cfgHelper = require('../helpers/config')
  4. const fs = require('fs')
  5. const path = require('path')
  6. const yaml = require('js-yaml')
  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. if (process.env.dockerdev) {
  18. confPaths.config = path.join(WIKI.ROOTPATH, `dev/containers/config.yml`)
  19. }
  20. if (process.env.CONFIG_FILE) {
  21. confPaths.config = path.resolve(WIKI.ROOTPATH, process.env.CONFIG_FILE)
  22. }
  23. process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))
  24. let appconfig = {}
  25. let appdata = {}
  26. try {
  27. appconfig = yaml.load(
  28. cfgHelper.parseConfigValue(
  29. fs.readFileSync(confPaths.config, 'utf8')
  30. )
  31. )
  32. appdata = yaml.load(fs.readFileSync(confPaths.data, 'utf8'))
  33. appdata.regex = require(confPaths.dataRegex)
  34. console.info(chalk.green.bold(`OK`))
  35. } catch (err) {
  36. console.error(chalk.red.bold(`FAILED`))
  37. console.error(err.message)
  38. console.error(chalk.red.bold(`>>> Unable to read configuration file! Did you create the config.yml file?`))
  39. process.exit(1)
  40. }
  41. // Merge with defaults
  42. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  43. // Override port
  44. if (appconfig.port < 1 || process.env.HEROKU) {
  45. appconfig.port = process.env.PORT || 80
  46. }
  47. if (process.env.WIKI_PORT) {
  48. appconfig.port = process.env.WIKI_PORT || 80
  49. }
  50. // Load package info
  51. const packageInfo = require(path.join(WIKI.ROOTPATH, 'package.json'))
  52. // Load DB Password from Docker Secret File
  53. if (process.env.DB_PASS_FILE) {
  54. console.info(chalk.blue(`DB_PASS_FILE is defined. Will use secret from file.`))
  55. try {
  56. appconfig.db.pass = fs.readFileSync(process.env.DB_PASS_FILE, 'utf8').trim()
  57. } catch (err) {
  58. console.error(chalk.red.bold(`>>> Failed to read Docker Secret File using path defined in DB_PASS_FILE env variable!`))
  59. console.error(err.message)
  60. process.exit(1)
  61. }
  62. }
  63. WIKI.config = appconfig
  64. WIKI.data = appdata
  65. WIKI.version = packageInfo.version
  66. WIKI.releaseDate = packageInfo.releaseDate
  67. WIKI.devMode = (packageInfo.dev === true)
  68. },
  69. /**
  70. * Load config from DB
  71. */
  72. async loadFromDb() {
  73. let conf = await WIKI.db.settings.getConfig()
  74. if (conf) {
  75. WIKI.config = _.defaultsDeep(conf, WIKI.config)
  76. } else {
  77. WIKI.logger.warn('Missing DB Configuration!')
  78. process.exit(1)
  79. }
  80. },
  81. /**
  82. * Save config to DB
  83. *
  84. * @param {Array} keys Array of keys to save
  85. * @returns Promise
  86. */
  87. async saveToDb(keys, propagate = true) {
  88. try {
  89. for (let key of keys) {
  90. let value = _.get(WIKI.config, key, null)
  91. if (!_.isPlainObject(value)) {
  92. value = { v: value }
  93. }
  94. let affectedRows = await WIKI.db.settings.query().patch({ value }).where('key', key)
  95. if (affectedRows === 0 && value) {
  96. await WIKI.db.settings.query().insert({ key, value })
  97. }
  98. }
  99. if (propagate) {
  100. WIKI.events.outbound.emit('reloadConfig')
  101. }
  102. } catch (err) {
  103. WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
  104. return false
  105. }
  106. return true
  107. },
  108. /**
  109. * Apply Dev Flags
  110. */
  111. async applyFlags() {
  112. WIKI.db.knex.client.config.debug = WIKI.config.flags.sqllog
  113. },
  114. /**
  115. * Subscribe to HA propagation events
  116. */
  117. subscribeToEvents() {
  118. WIKI.events.inbound.on('reloadConfig', async () => {
  119. await WIKI.configSvc.loadFromDb()
  120. await WIKI.configSvc.applyFlags()
  121. })
  122. }
  123. }