添加redis和rpc功能
This commit is contained in:
32
utils/redis/driver.ts
Normal file
32
utils/redis/driver.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import Redis from 'ioredis'
|
||||
|
||||
const clients = new Map<string,RedisClientDriver>()
|
||||
|
||||
export class RedisClientDriver extends Redis {
|
||||
|
||||
constructor (
|
||||
/**
|
||||
* redis 客户端名字
|
||||
*/
|
||||
private readonly _name:string,
|
||||
host:string,
|
||||
port:number,
|
||||
password:string,
|
||||
db:number
|
||||
) {
|
||||
super(port,host,{
|
||||
password,
|
||||
db
|
||||
})
|
||||
clients.set(this.name,this)
|
||||
}
|
||||
|
||||
static getClient (name:string):RedisClientDriver|null {
|
||||
return clients.get(name) || null
|
||||
}
|
||||
|
||||
get name ():string {
|
||||
return this._name
|
||||
}
|
||||
|
||||
}
|
53
utils/redis/init.ts
Normal file
53
utils/redis/init.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { RedisClientDriver } from './driver'
|
||||
import { event } from '../../event'
|
||||
|
||||
interface EasyRedisConfig {
|
||||
host:string,
|
||||
port:number,
|
||||
db:number,
|
||||
password:string
|
||||
}
|
||||
|
||||
const config_cache = new Map<string,EasyRedisConfig>()
|
||||
|
||||
export function getConfig (prefix:string):EasyRedisConfig {
|
||||
|
||||
if(!config_cache.has(prefix)) {
|
||||
|
||||
const host = process.env[prefix + 'REDIS_HOST'] || ''
|
||||
const port = process.env[prefix + 'REDIS_PORT'] || '6379'
|
||||
const db = process.env[prefix + 'REDIS_DB'] || '0'
|
||||
const password = process.env[prefix + 'REDIS_PWD'] || ''
|
||||
|
||||
const cache = {
|
||||
host,
|
||||
port:parseInt(port,10),
|
||||
db:parseInt(db,10),
|
||||
password
|
||||
}
|
||||
|
||||
config_cache.set(prefix,cache)
|
||||
return cache
|
||||
}
|
||||
|
||||
return config_cache.get(prefix) as unknown as EasyRedisConfig
|
||||
}
|
||||
|
||||
function init () {
|
||||
const clients_connect = [{ name:'normal',prefix:'' }]
|
||||
clients_connect.forEach(item=>{
|
||||
let success_count = 0
|
||||
const config = getConfig(item.prefix)
|
||||
const client = new RedisClientDriver(item.name,config.host,config.port,config.password,config.db)
|
||||
|
||||
client.addListener('connect',()=>{
|
||||
success_count++
|
||||
if(success_count === clients_connect.length) {
|
||||
event.emit('redis-all-connected')
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
init()
|
59
utils/sys_conf.ts
Normal file
59
utils/sys_conf.ts
Normal file
@@ -0,0 +1,59 @@
|
||||
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<void> {
|
||||
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] || ''
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user