import { RedisClientDriver } from './redis/driver' import DebugFunc from 'debug' import { event } from '../event' const debug = DebugFunc('SystemConfig') interface SystemConfig{ /**WebSocketJsonRpc的服务端token */ WS_JSON_RPC_SERVER_TOKEN:string, } const config:SystemConfig = { WS_JSON_RPC_SERVER_TOKEN:'', } async function sync (client:RedisClientDriver,name:keyof SystemConfig,desc:string) { const val = await client.hget('config',name) if(!val) { const msg = `缺少系统配置项:${desc} [ ${name} ]` throw new Error(msg) } config[name] = val } function sync_error_handle (error:Error) { debug(error.message) return Promise.reject(error) } async function prepare (client:RedisClientDriver):Promise { await sync(client,'WS_JSON_RPC_SERVER_TOKEN','rpc密钥').catch(sync_error_handle) } event.on('redis-all-connected',async ()=>{ const client = RedisClientDriver.getClient('normal')! await prepare(client) event.emit('system-config-sync-complete') debug('系统配置完成',config) }) export default class { static all ():SystemConfig { return Object.freeze( Object.assign({},config) ) } static get (name:keyof SystemConfig):string { return this.all()[name] || '' } }