30 lines
996 B
TypeScript
30 lines
996 B
TypeScript
import { RegionModel } from '@db/Region';
|
|
import proxy from 'egg-http-proxy-middleware';
|
|
|
|
module.exports = () => {
|
|
return async function (ctx, next) {
|
|
if(!ctx.app.config.envToHost) {
|
|
let envToHost = new Map<string, string>();
|
|
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);
|
|
};
|
|
}; |