From 3ed86c9bbc55ba7c2d52d93d60b3381b3cea1e7f Mon Sep 17 00:00:00 2001 From: luying Date: Sat, 15 Apr 2023 19:03:42 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(=E9=85=8D=E7=BD=AE):=20gm-serv?= =?UTF-8?q?er=E7=9A=84=E8=B7=B3=E8=BD=AC=E6=94=B9=E4=B8=BA=E6=9F=A5?= =?UTF-8?q?=E8=AF=A2=E6=95=B0=E6=8D=AE=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gm-server/app/middleware/proxy.ts | 28 ++++++++++++++++++++++++++- gm-server/config/config.default.ts | 31 ------------------------------ 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/gm-server/app/middleware/proxy.ts b/gm-server/app/middleware/proxy.ts index 24f086987..a148ea263 100644 --- a/gm-server/app/middleware/proxy.ts +++ b/gm-server/app/middleware/proxy.ts @@ -1,4 +1,30 @@ +import { RegionModel } from '@db/Region'; import proxy from 'egg-http-proxy-middleware'; -module.exports = proxy; +module.exports = () => { + return async function (ctx, next) { + if(!ctx.app.config.envToHost) { + let envToHost = new Map(); + let regions = await RegionModel.getAllRegion(); + for(let { env, gmLink } of regions) { + envToHost.set(env, gmLink); + } + ctx.app.config.envToHost = envToHost; + } + let options = {}; + for(let [env, domain] of ctx.app.config.envToHost) { + options[`/api/${env}/`] = { + target: domain, + changeOrigin: true, + secure: true, + pathRewrite: function(path) { + console.log('proxy', path, path.replace(`/api/${env}/`, '/api/')) + return path.replace(`/api/${env}/`, '/api/') + } + } + } + + return proxy(options)(ctx, next); + }; + }; \ No newline at end of file diff --git a/gm-server/config/config.default.ts b/gm-server/config/config.default.ts index 904e64de5..ae3aa4cc0 100644 --- a/gm-server/config/config.default.ts +++ b/gm-server/config/config.default.ts @@ -76,37 +76,6 @@ export default (appInfo: EggAppInfo) => { maxFileSize: 1024, }; - let regions = [ // 大区数据 - { env: 'stable', name: "常山少年", domain: 'http://zyz_gm.trgame.cn' }, - { env: 'alpha', name: "测试服", domain: 'http://pinus_gm_test.trgame.cn' }, - { env: 'dev', name: "开发服", domain: 'http://zyzdev_gm.trgame.cn' }, - { env: 'isbn', name: "办好福", domain: 'http://zyz_isbn_gm.trgame.cn' }, - { env: 'isbnhk', name: "海外审核服", domain: 'http://zyz_isbn_gm_hk.trgame.cn' }, - // 37测试服组 - { env: 'sq1', name: "37测试1服", domain: 'http://gm-sgzzyz.yev242.com' }, - { env: 'sq3', name: "小范围对外测试", domain: 'http://sq3-web-sgzzyz.yev242.com' }, - { env: 'sq4', name: "渠道二测", domain: 'http://sq6-web-sgzzyz.yev242.com' }, - { env: 'sq7', name: "精英测试", domain: 'http://sq8-web-sgzzyz.yev242.com' }, - { env: 'sq9', name: "渠道吸量", domain: 'http://sq10-web-yjz.yev242.com' }, - // 公测自营 - { env: 'zy1', name: "公测自营", domain: 'http://yjz-gm-1.yev242.com' }, - ]; - config.regions = regions; - - let httpProxy: any = {}; - for(let { env, domain } of regions) { - httpProxy[`/api/${env}/`] = { - target: domain, - changeOrigin: true, - secure: true, - pathRewrite: function(path) { - console.log('proxy', path, path.replace(`/api/${env}/`, '/api/')) - return path.replace(`/api/${env}/`, '/api/') - } - } - } - config.proxy = httpProxy; - // the return config will combines to EggAppConfig return { ...config,