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.

122 lines
3.4 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/docker-${process.env.DEVDB}/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. },
  64. /**
  65. * Load config from DB
  66. */
  67. async loadFromDb() {
  68. let conf = await WIKI.models.settings.getConfig()
  69. if (conf) {
  70. WIKI.config = _.defaultsDeep(conf, WIKI.config)
  71. } else {
  72. WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
  73. WIKI.config.setup = true
  74. }
  75. },
  76. /**
  77. * Save config to DB
  78. *
  79. * @param {Array} keys Array of keys to save
  80. * @returns Promise
  81. */
  82. async saveToDb(keys) {
  83. try {
  84. for (let key of keys) {
  85. let value = _.get(WIKI.config, key, null)
  86. if (!_.isPlainObject(value)) {
  87. value = { v: value }
  88. }
  89. let affectedRows = await WIKI.models.settings.query().patch({ value }).where('key', key)
  90. if (affectedRows === 0 && value) {
  91. await WIKI.models.settings.query().insert({ key, value })
  92. }
  93. }
  94. } catch (err) {
  95. WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
  96. return false
  97. }
  98. return true
  99. },
  100. /**
  101. * Apply Dev Flags
  102. */
  103. async applyFlags() {
  104. WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
  105. }
  106. }