config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/docker-${process.env.DEVDB}/config.yml`)
  20. }
  21. process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))
  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. console.info(chalk.green.bold(`OK`))
  33. } catch (err) {
  34. console.error(chalk.red.bold(`FAILED`))
  35. console.error(err.message)
  36. console.error(chalk.red.bold(`>>> Unable to read configuration file! Did you create the config.yml file?`))
  37. process.exit(1)
  38. }
  39. // Merge with defaults
  40. appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
  41. if (appconfig.port < 1 || process.env.HEROKU) {
  42. appconfig.port = process.env.PORT || 80
  43. }
  44. const packageInfo = require(path.join(WIKI.ROOTPATH, 'package.json'))
  45. // Load DB Password from Docker Secret File
  46. if (process.env.DB_PASS_FILE) {
  47. console.info(chalk.blue(`DB_PASS_FILE is defined. Will use secret from file.`))
  48. try {
  49. appconfig.db.pass = fs.readFileSync(process.env.DB_PASS_FILE, 'utf8').trim()
  50. } catch (err) {
  51. console.error(chalk.red.bold(`>>> Failed to read Docker Secret File using path defined in DB_PASS_FILE env variable!`))
  52. console.error(err.message)
  53. process.exit(1)
  54. }
  55. }
  56. WIKI.config = appconfig
  57. WIKI.data = appdata
  58. WIKI.version = packageInfo.version
  59. WIKI.releaseDate = packageInfo.releaseDate
  60. },
  61. /**
  62. * Load config from DB
  63. */
  64. async loadFromDb() {
  65. let conf = await WIKI.models.settings.getConfig()
  66. if (conf) {
  67. WIKI.config = _.defaultsDeep(conf, WIKI.config)
  68. } else {
  69. WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
  70. WIKI.config.setup = true
  71. }
  72. },
  73. /**
  74. * Save config to DB
  75. *
  76. * @param {Array} keys Array of keys to save
  77. * @returns Promise
  78. */
  79. async saveToDb(keys) {
  80. try {
  81. for (let key of keys) {
  82. let value = _.get(WIKI.config, key, null)
  83. if (!_.isPlainObject(value)) {
  84. value = { v: value }
  85. }
  86. let affectedRows = await WIKI.models.settings.query().patch({ value }).where('key', key)
  87. if (affectedRows === 0 && value) {
  88. await WIKI.models.settings.query().insert({ key, value })
  89. }
  90. }
  91. } catch (err) {
  92. WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
  93. return false
  94. }
  95. return true
  96. },
  97. /**
  98. * Apply Dev Flags
  99. */
  100. async applyFlags() {
  101. WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
  102. }
  103. }