perf: 远程配置
parent
38f6892660
commit
498c7cbec2
|
@ -3,8 +3,9 @@ const Shell = require('./shell')
|
||||||
const lodash = require('lodash')
|
const lodash = require('lodash')
|
||||||
const defConfig = require('./config/index.js')
|
const defConfig = require('./config/index.js')
|
||||||
const JSON5 = require('json5').default
|
const JSON5 = require('json5').default
|
||||||
|
const request = require('request')
|
||||||
console.log('JSON5', JSON5)
|
const path = require('path')
|
||||||
|
const log = require('./utils/util.log')
|
||||||
let configTarget = lodash.cloneDeep(defConfig)
|
let configTarget = lodash.cloneDeep(defConfig)
|
||||||
|
|
||||||
function get () {
|
function get () {
|
||||||
|
@ -24,6 +25,13 @@ function _deleteDisabledItem (target) {
|
||||||
const getDefaultConfigBasePath = function () {
|
const getDefaultConfigBasePath = function () {
|
||||||
return get().server.setting.userBasePath
|
return get().server.setting.userBasePath
|
||||||
}
|
}
|
||||||
|
function _getRemoteSavePath () {
|
||||||
|
const dir = getDefaultConfigBasePath()
|
||||||
|
if (!fs.existsSync(dir)) {
|
||||||
|
fs.mkdirSync(dir)
|
||||||
|
}
|
||||||
|
return path.join(dir, 'remote_config.json5')
|
||||||
|
}
|
||||||
function _getConfigPath () {
|
function _getConfigPath () {
|
||||||
const dir = getDefaultConfigBasePath()
|
const dir = getDefaultConfigBasePath()
|
||||||
if (!fs.existsSync(dir)) {
|
if (!fs.existsSync(dir)) {
|
||||||
|
@ -33,6 +41,9 @@ function _getConfigPath () {
|
||||||
}
|
}
|
||||||
|
|
||||||
function doMerge (defObj, newObj) {
|
function doMerge (defObj, newObj) {
|
||||||
|
if (newObj == null) {
|
||||||
|
return defObj
|
||||||
|
}
|
||||||
const defObj2 = { ...defObj }
|
const defObj2 = { ...defObj }
|
||||||
const newObj2 = {}
|
const newObj2 = {}
|
||||||
for (const key in newObj) {
|
for (const key in newObj) {
|
||||||
|
@ -69,11 +80,60 @@ function doMerge (defObj, newObj) {
|
||||||
})
|
})
|
||||||
return newObj2
|
return newObj2
|
||||||
}
|
}
|
||||||
|
let timer
|
||||||
const configApi = {
|
const configApi = {
|
||||||
|
startAutoDownloadRemoteConfig () {
|
||||||
|
if (timer != null) {
|
||||||
|
clearInterval(timer)
|
||||||
|
}
|
||||||
|
const download = async () => {
|
||||||
|
await configApi.downloadRemoteConfig()
|
||||||
|
configApi.reload()
|
||||||
|
}
|
||||||
|
download()
|
||||||
|
setInterval(download, 24 * 60 * 60 * 1000) // 1天
|
||||||
|
},
|
||||||
|
downloadRemoteConfig () {
|
||||||
|
if (get().app.remoteConfig.enabled !== true) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
const remoteConfigUrl = get().app.remoteConfig.url
|
||||||
|
// eslint-disable-next-line handle-callback-err
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
log.info('下载远程配置:', remoteConfigUrl)
|
||||||
|
request(remoteConfigUrl, (error, response, body) => {
|
||||||
|
if (error) {
|
||||||
|
log.error('下载远程配置失败', error)
|
||||||
|
reject(error)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if (response && response.statusCode === 200) {
|
||||||
|
fs.writeFileSync(_getRemoteSavePath(), body)
|
||||||
|
resolve()
|
||||||
|
} else {
|
||||||
|
const message = '下载远程配置失败:' + response.message + ',code:' + response.statusCode
|
||||||
|
log.error(message)
|
||||||
|
reject(new Error(message))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
},
|
||||||
|
readRemoteConfig () {
|
||||||
|
if (get().app.remoteConfig.enabled !== true) {
|
||||||
|
return {}
|
||||||
|
}
|
||||||
|
const path = _getRemoteSavePath()
|
||||||
|
if (fs.existsSync()) {
|
||||||
|
const file = fs.readFileSync(path)
|
||||||
|
log.info('读取合并远程配置文件:', path)
|
||||||
|
return JSON5.parse(file.toString())
|
||||||
|
}
|
||||||
|
return {}
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* 保存自定义的 config
|
* 保存自定义的 config
|
||||||
* @param newConfig
|
* @param newConfig
|
||||||
|
* @param remoteConfig //远程配置
|
||||||
*/
|
*/
|
||||||
save (newConfig) {
|
save (newConfig) {
|
||||||
// 对比默认config的异同
|
// 对比默认config的异同
|
||||||
|
@ -113,6 +173,7 @@ const configApi = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
lodash.mergeWith(merged, clone, customizer)
|
lodash.mergeWith(merged, clone, customizer)
|
||||||
|
lodash.mergeWith(merged, configApi.readRemoteConfig(), customizer)
|
||||||
lodash.mergeWith(merged, newConfig, customizer)
|
lodash.mergeWith(merged, newConfig, customizer)
|
||||||
_deleteDisabledItem(merged)
|
_deleteDisabledItem(merged)
|
||||||
configTarget = merged
|
configTarget = merged
|
||||||
|
|
|
@ -17,7 +17,7 @@ module.exports = {
|
||||||
},
|
},
|
||||||
remoteConfig: {
|
remoteConfig: {
|
||||||
enabled: true,
|
enabled: true,
|
||||||
url: 'https://gitee.com/docmirror/dev-sidecar/raw/master/packages/gui/extra/config_remote.json5'
|
url: 'https://gitee.com/docmirror/dev-sidecar/raw/master/packages/core/src/config/remote_config.json5'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
server: {
|
server: {
|
||||||
|
|
|
@ -150,6 +150,8 @@ export default {
|
||||||
|
|
||||||
// 合并用户配置
|
// 合并用户配置
|
||||||
DevSidecar.api.config.reload()
|
DevSidecar.api.config.reload()
|
||||||
|
// 开启自动下载远程配置
|
||||||
|
DevSidecar.api.config.startAutoDownloadRemoteConfig()
|
||||||
// 启动所有
|
// 启动所有
|
||||||
localApi.startup()
|
localApi.startup()
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in New Issue