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