config.js 3.75 KB
Newer Older
1
const _ = require('lodash')
2
const chalk = require('chalk')
NGPixel's avatar
NGPixel committed
3
const cfgHelper = require('../helpers/config')
4 5 6 7
const fs = require('fs')
const path = require('path')
const yaml = require('js-yaml')

8
/* global WIKI */
9

10 11 12 13 14 15
module.exports = {
  /**
   * Load root config from disk
   */
  init() {
    let confPaths = {
16 17 18
      config: path.join(WIKI.ROOTPATH, 'config.yml'),
      data: path.join(WIKI.SERVERPATH, 'app/data.yml'),
      dataRegex: path.join(WIKI.SERVERPATH, 'app/regex.js')
19 20
    }

21
    if (process.env.dockerdev) {
22
      confPaths.config = path.join(WIKI.ROOTPATH, `dev/containers/config.yml`)
23 24
    }

Nick's avatar
Nick committed
25 26 27 28
    if (process.env.CONFIG_FILE) {
      confPaths.config = path.resolve(WIKI.ROOTPATH, process.env.CONFIG_FILE)
    }

29 30
    process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))

31 32 33 34 35 36 37 38
    let appconfig = {}
    let appdata = {}

    try {
      appconfig = yaml.safeLoad(
        cfgHelper.parseConfigValue(
          fs.readFileSync(confPaths.config, 'utf8')
        )
39
      )
40 41
      appdata = yaml.safeLoad(fs.readFileSync(confPaths.data, 'utf8'))
      appdata.regex = require(confPaths.dataRegex)
42 43 44 45 46 47
      console.info(chalk.green.bold(`OK`))
    } catch (err) {
      console.error(chalk.red.bold(`FAILED`))
      console.error(err.message)

      console.error(chalk.red.bold(`>>> Unable to read configuration file! Did you create the config.yml file?`))
48 49
      process.exit(1)
    }
50

51
    // Merge with defaults
52

53
    appconfig = _.defaultsDeep(appconfig, appdata.defaults.config)
54

Nick's avatar
Nick committed
55
    if (appconfig.port < 1 || process.env.HEROKU) {
56 57
      appconfig.port = process.env.PORT || 80
    }
NGPixel's avatar
NGPixel committed
58

59 60
    const packageInfo = require(path.join(WIKI.ROOTPATH, 'package.json'))

61 62
    // Load DB Password from Docker Secret File
    if (process.env.DB_PASS_FILE) {
63
      console.info(chalk.blue(`DB_PASS_FILE is defined. Will use secret from file.`))
64
      try {
65
        appconfig.db.pass = fs.readFileSync(process.env.DB_PASS_FILE, 'utf8').trim()
66 67 68 69 70 71 72
      } catch (err) {
        console.error(chalk.red.bold(`>>> Failed to read Docker Secret File using path defined in DB_PASS_FILE env variable!`))
        console.error(err.message)
        process.exit(1)
      }
    }

73 74
    WIKI.config = appconfig
    WIKI.data = appdata
75 76
    WIKI.version = packageInfo.version
    WIKI.releaseDate = packageInfo.releaseDate
77
    WIKI.devMode = (packageInfo.dev === true)
78 79 80 81 82
  },

  /**
   * Load config from DB
   */
83
  async loadFromDb() {
84
    let conf = await WIKI.models.settings.getConfig()
85 86
    if (conf) {
      WIKI.config = _.defaultsDeep(conf, WIKI.config)
NGPixel's avatar
NGPixel committed
87
    } else {
88 89
      WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
      WIKI.config.setup = true
NGPixel's avatar
NGPixel committed
90 91 92 93 94
    }
  },
  /**
   * Save config to DB
   *
95
   * @param {Array} keys Array of keys to save
NGPixel's avatar
NGPixel committed
96 97
   * @returns Promise
   */
98
  async saveToDb(keys, propagate = true) {
NGPixel's avatar
NGPixel committed
99
    try {
100
      for (let key of keys) {
101 102 103 104
        let value = _.get(WIKI.config, key, null)
        if (!_.isPlainObject(value)) {
          value = { v: value }
        }
105
        let affectedRows = await WIKI.models.settings.query().patch({ value }).where('key', key)
106
        if (affectedRows === 0 && value) {
107
          await WIKI.models.settings.query().insert({ key, value })
108
        }
109
      }
110 111 112
      if (propagate) {
        WIKI.events.outbound.emit('reloadConfig')
      }
NGPixel's avatar
NGPixel committed
113
    } catch (err) {
114
      WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
NGPixel's avatar
NGPixel committed
115 116 117 118
      return false
    }

    return true
Nick's avatar
Nick committed
119 120 121 122 123 124
  },
  /**
   * Apply Dev Flags
   */
  async applyFlags() {
    WIKI.models.knex.client.config.debug = WIKI.config.flags.sqllog
125 126 127 128 129 130 131 132 133 134
  },

  /**
   * Subscribe to HA propagation events
   */
  subscribeToEvents() {
    WIKI.events.inbound.on('reloadConfig', async () => {
      await WIKI.configSvc.loadFromDb()
      await WIKI.configSvc.applyFlags()
    })
135 136
  }
}