diff --git a/web-server/app/middleware/proxy.ts b/web-server/app/middleware/proxy.ts index 581749f89..87d112b5f 100644 --- a/web-server/app/middleware/proxy.ts +++ b/web-server/app/middleware/proxy.ts @@ -13,14 +13,7 @@ module.exports = () => { ctx.app.config.envToHost = envToHost; } if(!ctx.app.config.sidToHost) { - let envToHost = ctx.app.config.envToHost||new Map(); - let sidToHost = new Map(); - let servers = await ServerlistModel.getAllServerList(); - for(let { id, env } of servers) { - let webHost = envToHost.get(env); - sidToHost.set(id.toString(), webHost); - } - ctx.app.config.sidToHost = sidToHost; + await getNewHost(ctx); } let options = {}; @@ -39,11 +32,16 @@ module.exports = () => { let url = getProxyUrl(ctx.request.url); if(url) { let sid = ctx.request.method == 'GET'? ctx.query.sid.toString(): ctx.request.body.sid.toString(); - options[url] = { - target: ctx.app.config.sidToHost.get(sid), - changeOrigin: true, - secure: true, - headers: { "is-proxy": "true" } + if(!ctx.app.config.sidToHost) { + await getNewHost(sid); + } + if(!!ctx.app.config.sidToHost.get(sid)) { + options[url] = { + target: ctx.app.config.sidToHost.get(sid), + changeOrigin: true, + secure: true, + headers: { "is-proxy": "true" } + } } } } @@ -52,6 +50,17 @@ module.exports = () => { }; }; + async function getNewHost(ctx) { + let envToHost = ctx.app.config.envToHost||new Map(); + let sidToHost = new Map(); + let servers = await ServerlistModel.getAllServerList(); + for(let { id, env } of servers) { + let webHost = envToHost.get(env); + sidToHost.set(id.toString(), webHost); + } + ctx.app.config.sidToHost = sidToHost; + } + function getProxyUrl(url: string) { const urls = [ '/cb/treatusername',