refactor: remove config namespaces
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
/* global WIKI */
|
||||
|
||||
const _ = require('lodash')
|
||||
const passport = require('passport')
|
||||
const fs = require('fs-extra')
|
||||
const _ = require('lodash')
|
||||
const path = require('path')
|
||||
const autoload = require('auto-load')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = {
|
||||
strategies: {},
|
||||
@@ -30,34 +29,39 @@ module.exports = {
|
||||
})
|
||||
})
|
||||
|
||||
// Load authentication strategies
|
||||
|
||||
const modules = _.values(autoload(path.join(WIKI.SERVERPATH, 'modules/authentication')))
|
||||
_.forEach(modules, (strategy) => {
|
||||
const strategyConfig = _.get(WIKI.config.auth.strategies, strategy.key, { isEnabled: false })
|
||||
strategyConfig.callbackURL = `${WIKI.config.site.host}${WIKI.config.site.path}login/${strategy.key}/callback`
|
||||
strategy.config = strategyConfig
|
||||
if (strategyConfig.isEnabled) {
|
||||
try {
|
||||
strategy.init(passport, strategyConfig)
|
||||
} catch (err) {
|
||||
WIKI.logger.error(`Authentication Provider ${strategy.title}: [ FAILED ]`)
|
||||
WIKI.logger.error(err)
|
||||
}
|
||||
}
|
||||
fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
|
||||
strategy.icon = iconData
|
||||
}).catch(err => {
|
||||
if (err.code === 'ENOENT') {
|
||||
strategy.icon = '[missing icon]'
|
||||
} else {
|
||||
WIKI.logger.warn(err)
|
||||
}
|
||||
})
|
||||
this.strategies[strategy.key] = strategy
|
||||
WIKI.logger.info(`Authentication Provider ${strategy.title}: [ OK ]`)
|
||||
})
|
||||
|
||||
return this
|
||||
},
|
||||
async activateStrategies() {
|
||||
try {
|
||||
// Unload any active strategies
|
||||
WIKI.auth.strategies = []
|
||||
const currentStrategies = _.keys(passport._strategies)
|
||||
_.pull(currentStrategies, 'session')
|
||||
_.forEach(currentStrategies, stg => { passport.unuse(stg) })
|
||||
|
||||
// Load enable strategies
|
||||
const enabledStrategies = await WIKI.db.authentication.getEnabledStrategies()
|
||||
for (let idx in enabledStrategies) {
|
||||
const stg = enabledStrategies[idx]
|
||||
const strategy = require(`../modules/authentication/${stg.key}`)
|
||||
stg.config.callbackURL = `${WIKI.config.site.host}/login/${stg.key}/callback`
|
||||
strategy.init(passport, stg.config)
|
||||
|
||||
fs.readFile(path.join(WIKI.ROOTPATH, `assets/svg/auth-icon-${strategy.key}.svg`), 'utf8').then(iconData => {
|
||||
strategy.icon = iconData
|
||||
}).catch(err => {
|
||||
if (err.code === 'ENOENT') {
|
||||
strategy.icon = '[missing icon]'
|
||||
} else {
|
||||
WIKI.logger.warn(err)
|
||||
}
|
||||
})
|
||||
WIKI.auth.strategies[stg.key] = strategy
|
||||
WIKI.logger.info(`Authentication Strategy ${stg.title}: [ OK ]`)
|
||||
}
|
||||
} catch (err) {
|
||||
WIKI.logger.error(`Authentication Strategy: [ FAILED ]`)
|
||||
WIKI.logger.error(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -50,45 +50,32 @@ module.exports = {
|
||||
|
||||
/**
|
||||
* Load config from DB
|
||||
*
|
||||
* @param {Array} subsets Array of subsets to load
|
||||
* @returns Promise
|
||||
*/
|
||||
async loadFromDb(subsets) {
|
||||
if (!_.isArray(subsets) || subsets.length === 0) {
|
||||
subsets = WIKI.data.configNamespaces
|
||||
}
|
||||
|
||||
let results = await WIKI.db.settings.query().select(['key', 'value']).whereIn('key', subsets)
|
||||
if (_.isArray(results) && results.length === subsets.length) {
|
||||
results.forEach(result => {
|
||||
WIKI.config[result.key] = result.value
|
||||
})
|
||||
return true
|
||||
async loadFromDb() {
|
||||
let conf = await WIKI.db.settings.getConfig()
|
||||
if (conf) {
|
||||
WIKI.config = _.defaultsDeep(conf, WIKI.config)
|
||||
} else {
|
||||
WIKI.logger.warn('DB Configuration is empty or incomplete.')
|
||||
return false
|
||||
WIKI.logger.warn('DB Configuration is empty or incomplete. Switching to Setup mode...')
|
||||
WIKI.config.setup = true
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Save config to DB
|
||||
*
|
||||
* @param {Array} subsets Array of subsets to save
|
||||
* @param {Array} keys Array of keys to save
|
||||
* @returns Promise
|
||||
*/
|
||||
async saveToDb(subsets) {
|
||||
if (!_.isArray(subsets) || subsets.length === 0) {
|
||||
subsets = WIKI.data.configNamespaces
|
||||
}
|
||||
|
||||
async saveToDb(keys) {
|
||||
let trx = await WIKI.db.Objection.transaction.start(WIKI.db.knex)
|
||||
|
||||
try {
|
||||
for (let set of subsets) {
|
||||
console.info(set)
|
||||
await WIKI.db.settings.query(trx).patch({
|
||||
value: _.get(WIKI.config, set, {})
|
||||
}).where('key', set)
|
||||
for (let key of keys) {
|
||||
const value = _.get(WIKI.config, key, null)
|
||||
let affectedRows = await WIKI.db.settings.query(trx).patch({ value }).where('key', key)
|
||||
if (affectedRows === 0 && value) {
|
||||
await WIKI.db.settings.query(trx).insert({ key, value })
|
||||
}
|
||||
}
|
||||
await trx.commit()
|
||||
} catch (err) {
|
||||
|
@@ -53,6 +53,20 @@ module.exports = {
|
||||
client: dbClient,
|
||||
useNullAsDefault: true,
|
||||
connection: dbConfig,
|
||||
pool: {
|
||||
async afterCreate(conn, done) {
|
||||
// -> Set Connection App Name
|
||||
switch (WIKI.config.db.type) {
|
||||
case 'postgres':
|
||||
await conn.query(`set application_name = 'Wiki.js'`)
|
||||
done()
|
||||
break
|
||||
default:
|
||||
done()
|
||||
break
|
||||
}
|
||||
}
|
||||
},
|
||||
debug: WIKI.IS_DEBUG
|
||||
})
|
||||
|
||||
@@ -71,21 +85,13 @@ module.exports = {
|
||||
directory: path.join(WIKI.SERVERPATH, 'db/migrations'),
|
||||
tableName: 'migrations'
|
||||
})
|
||||
},
|
||||
// -> Set Connection App Name
|
||||
async setAppName() {
|
||||
switch (WIKI.config.db.type) {
|
||||
case 'postgres':
|
||||
return self.knex.raw(`set application_name = 'Wiki.js'`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let initTasksQueue = (WIKI.IS_MASTER) ? [
|
||||
initTasks.syncSchemas,
|
||||
initTasks.setAppName
|
||||
initTasks.syncSchemas
|
||||
] : [
|
||||
initTasks.setAppName
|
||||
() => { return Promise.resolve() }
|
||||
]
|
||||
|
||||
// Perform init tasks
|
||||
|
@@ -1,89 +1,53 @@
|
||||
const _ = require('lodash')
|
||||
const cluster = require('cluster')
|
||||
const Promise = require('bluebird')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = {
|
||||
numWorkers: 1,
|
||||
workers: [],
|
||||
init() {
|
||||
if (cluster.isMaster) {
|
||||
WIKI.logger.info('=======================================')
|
||||
WIKI.logger.info('= Wiki.js =============================')
|
||||
WIKI.logger.info('=======================================')
|
||||
async init() {
|
||||
WIKI.logger.info('=======================================')
|
||||
WIKI.logger.info('= Wiki.js =============================')
|
||||
WIKI.logger.info('=======================================')
|
||||
|
||||
WIKI.redis = require('./redis').init()
|
||||
WIKI.queue = require('./queue').init()
|
||||
WIKI.db = require('./db').init()
|
||||
WIKI.redis = require('./redis').init()
|
||||
WIKI.queue = require('./queue').init()
|
||||
|
||||
this.setWorkerLimit()
|
||||
this.bootMaster()
|
||||
} else {
|
||||
this.bootWorker()
|
||||
}
|
||||
await this.preBootMaster()
|
||||
this.bootMaster()
|
||||
},
|
||||
/**
|
||||
* Pre-Master Boot Sequence
|
||||
*/
|
||||
preBootMaster() {
|
||||
return Promise.mapSeries([
|
||||
() => { return WIKI.db.onReady },
|
||||
() => { return WIKI.configSvc.loadFromDb() },
|
||||
() => { return WIKI.queue.clean() }
|
||||
], fn => { return fn() })
|
||||
async preBootMaster() {
|
||||
try {
|
||||
await WIKI.db.onReady
|
||||
await WIKI.configSvc.loadFromDb()
|
||||
await WIKI.queue.clean()
|
||||
} catch (err) {
|
||||
WIKI.logger.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Boot Master Process
|
||||
*/
|
||||
bootMaster() {
|
||||
this.preBootMaster().then(sequenceResults => {
|
||||
if (_.every(sequenceResults, rs => rs === true) && WIKI.config.configMode !== 'setup') {
|
||||
this.postBootMaster()
|
||||
} else {
|
||||
WIKI.logger.info('Starting configuration manager...')
|
||||
async bootMaster() {
|
||||
try {
|
||||
if (WIKI.config.setup) {
|
||||
WIKI.logger.info('Starting setup wizard...')
|
||||
require('../setup')()
|
||||
} else {
|
||||
await require('../master')()
|
||||
this.postBootMaster()
|
||||
}
|
||||
return true
|
||||
}).catch(err => {
|
||||
} catch (err) {
|
||||
WIKI.logger.error(err)
|
||||
process.exit(1)
|
||||
})
|
||||
}
|
||||
},
|
||||
/**
|
||||
* Post-Master Boot Sequence
|
||||
*/
|
||||
async postBootMaster() {
|
||||
await require('../master')()
|
||||
|
||||
WIKI.queue.start()
|
||||
|
||||
cluster.on('exit', (worker, code, signal) => {
|
||||
if (!global.DEV) {
|
||||
WIKI.logger.info(`Background Worker #${worker.id} was terminated.`)
|
||||
}
|
||||
})
|
||||
},
|
||||
/**
|
||||
* Boot Worker Process
|
||||
*/
|
||||
bootWorker() {
|
||||
WIKI.logger.info(`Background Worker #${cluster.worker.id} is initializing...`)
|
||||
require('../worker')
|
||||
},
|
||||
/**
|
||||
* Spawn new Worker process
|
||||
*/
|
||||
spawnWorker() {
|
||||
this.workers.push(cluster.fork())
|
||||
},
|
||||
/**
|
||||
* Set Worker count based on config + system capabilities
|
||||
*/
|
||||
setWorkerLimit() {
|
||||
const numCPUs = require('os').cpus().length
|
||||
this.numWorkers = (WIKI.config.workers > 0) ? WIKI.config.workers : numCPUs
|
||||
if (this.numWorkers > numCPUs) {
|
||||
this.numWorkers = numCPUs
|
||||
}
|
||||
await WIKI.auth.activateStrategies()
|
||||
await WIKI.queue.start()
|
||||
}
|
||||
}
|
||||
|
@@ -17,7 +17,7 @@ module.exports = {
|
||||
ns: this.namespaces,
|
||||
defaultNS: 'common',
|
||||
saveMissing: false,
|
||||
lng: WIKI.config.site.lang,
|
||||
lng: WIKI.config.lang,
|
||||
fallbackLng: 'en'
|
||||
})
|
||||
|
||||
@@ -31,7 +31,7 @@ module.exports = {
|
||||
}
|
||||
|
||||
// Load current language
|
||||
this.loadLocale(WIKI.config.site.lang, { silent: true })
|
||||
this.loadLocale(WIKI.config.lang, { silent: true })
|
||||
|
||||
return this
|
||||
},
|
||||
@@ -55,6 +55,7 @@ module.exports = {
|
||||
const res = await WIKI.db.locales.query().findOne('code', locale)
|
||||
if (res) {
|
||||
if (_.isPlainObject(res.strings)) {
|
||||
console.info(res.strings)
|
||||
_.forOwn(res.strings, (data, ns) => {
|
||||
this.namespaces.push(ns)
|
||||
this.engine.addResourceBundle(locale, ns, data, true, true)
|
||||
|
Reference in New Issue
Block a user