mirror of https://github.com/requarks/wiki
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.
149 lines
3.9 KiB
149 lines
3.9 KiB
2 years ago
|
import { defaultsDeep, get, isPlainObject } from 'lodash-es'
|
||
|
import chalk from 'chalk'
|
||
|
import cfgHelper from '../helpers/config.mjs'
|
||
|
import regexData from '../app/regex.mjs'
|
||
|
import fs from 'node:fs/promises'
|
||
|
import path from 'node:path'
|
||
|
import yaml from 'js-yaml'
|
||
|
|
||
|
export default {
|
||
7 years ago
|
/**
|
||
|
* Load root config from disk
|
||
|
*/
|
||
2 years ago
|
async init(silent = false) {
|
||
|
const confPaths = {
|
||
7 years ago
|
config: path.join(WIKI.ROOTPATH, 'config.yml'),
|
||
2 years ago
|
data: path.join(WIKI.SERVERPATH, 'app/data.yml')
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
if (process.env.dockerdev) {
|
||
5 years ago
|
confPaths.config = path.join(WIKI.ROOTPATH, `dev/containers/config.yml`)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
if (process.env.CONFIG_FILE) {
|
||
|
confPaths.config = path.resolve(WIKI.ROOTPATH, process.env.CONFIG_FILE)
|
||
|
}
|
||
|
|
||
2 years ago
|
if (!silent) {
|
||
|
process.stdout.write(chalk.blue(`Loading configuration from ${confPaths.config}... `))
|
||
|
}
|
||
6 years ago
|
|
||
7 years ago
|
let appconfig = {}
|
||
|
let appdata = {}
|
||
|
|
||
|
try {
|
||
3 years ago
|
appconfig = yaml.load(
|
||
7 years ago
|
cfgHelper.parseConfigValue(
|
||
2 years ago
|
await fs.readFile(confPaths.config, 'utf8')
|
||
7 years ago
|
)
|
||
8 years ago
|
)
|
||
2 years ago
|
appdata = yaml.load(await fs.readFile(confPaths.data, 'utf8'))
|
||
|
appdata.regex = regexData
|
||
2 years ago
|
if (!silent) {
|
||
|
console.info(chalk.green.bold(`OK`))
|
||
|
}
|
||
6 years ago
|
} 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?`))
|
||
7 years ago
|
process.exit(1)
|
||
|
}
|
||
8 years ago
|
|
||
7 years ago
|
// Merge with defaults
|
||
8 years ago
|
|
||
2 years ago
|
appconfig = defaultsDeep(appconfig, appdata.defaults.config)
|
||
8 years ago
|
|
||
2 years ago
|
// Override port
|
||
|
|
||
6 years ago
|
if (appconfig.port < 1 || process.env.HEROKU) {
|
||
7 years ago
|
appconfig.port = process.env.PORT || 80
|
||
|
}
|
||
8 years ago
|
|
||
2 years ago
|
if (process.env.WIKI_PORT) {
|
||
|
appconfig.port = process.env.WIKI_PORT || 80
|
||
|
}
|
||
|
|
||
|
// Load package info
|
||
|
|
||
2 years ago
|
const packageInfo = JSON.parse(await fs.readFile(path.join(WIKI.SERVERPATH, 'package.json'), 'utf-8'))
|
||
6 years ago
|
|
||
5 years ago
|
// Load DB Password from Docker Secret File
|
||
|
if (process.env.DB_PASS_FILE) {
|
||
2 years ago
|
if (!silent) {
|
||
|
console.info(chalk.blue(`DB_PASS_FILE is defined. Will use secret from file.`))
|
||
|
}
|
||
5 years ago
|
try {
|
||
2 years ago
|
appconfig.db.pass = await fs.readFile(process.env.DB_PASS_FILE, 'utf8').trim()
|
||
5 years ago
|
} 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)
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
WIKI.config = appconfig
|
||
|
WIKI.data = appdata
|
||
6 years ago
|
WIKI.version = packageInfo.version
|
||
|
WIKI.releaseDate = packageInfo.releaseDate
|
||
5 years ago
|
WIKI.devMode = (packageInfo.dev === true)
|
||
7 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* Load config from DB
|
||
|
*/
|
||
7 years ago
|
async loadFromDb() {
|
||
2 years ago
|
const conf = await WIKI.db.settings.getConfig()
|
||
7 years ago
|
if (conf) {
|
||
2 years ago
|
WIKI.config = defaultsDeep(conf, WIKI.config)
|
||
7 years ago
|
} else {
|
||
2 years ago
|
WIKI.logger.warn('Missing DB Configuration!')
|
||
|
process.exit(1)
|
||
7 years ago
|
}
|
||
|
},
|
||
|
/**
|
||
|
* Save config to DB
|
||
|
*
|
||
7 years ago
|
* @param {Array} keys Array of keys to save
|
||
7 years ago
|
* @returns Promise
|
||
|
*/
|
||
5 years ago
|
async saveToDb(keys, propagate = true) {
|
||
7 years ago
|
try {
|
||
7 years ago
|
for (let key of keys) {
|
||
2 years ago
|
let value = get(WIKI.config, key, null)
|
||
|
if (!isPlainObject(value)) {
|
||
7 years ago
|
value = { v: value }
|
||
|
}
|
||
2 years ago
|
let affectedRows = await WIKI.db.settings.query().patch({ value }).where('key', key)
|
||
7 years ago
|
if (affectedRows === 0 && value) {
|
||
2 years ago
|
await WIKI.db.settings.query().insert({ key, value })
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
5 years ago
|
if (propagate) {
|
||
|
WIKI.events.outbound.emit('reloadConfig')
|
||
|
}
|
||
7 years ago
|
} catch (err) {
|
||
7 years ago
|
WIKI.logger.error(`Failed to save configuration to DB: ${err.message}`)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
|
return true
|
||
6 years ago
|
},
|
||
|
/**
|
||
|
* Apply Dev Flags
|
||
|
*/
|
||
|
async applyFlags() {
|
||
2 years ago
|
WIKI.db.knex.client.config.debug = WIKI.config.flags.sqlLog
|
||
5 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* Subscribe to HA propagation events
|
||
|
*/
|
||
|
subscribeToEvents() {
|
||
|
WIKI.events.inbound.on('reloadConfig', async () => {
|
||
|
await WIKI.configSvc.loadFromDb()
|
||
|
await WIKI.configSvc.applyFlags()
|
||
|
})
|
||
8 years ago
|
}
|
||
|
}
|