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.

136 lines
3.7 KiB

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