Skip to content

Commit

Permalink
Support dynamic proxy host replacement through hostname pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
galbi101 committed Nov 28, 2017
1 parent da57634 commit d52e09d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ The configuration file should be of the following format:
"host": "hostname",
"port": 80
},
"hostPattern": null, // (OPTIONAL) extract the last match from this regex executed on the url hostname, and replace the target host with it
"pathPatterns": [] // e.g. ["^/api/"] or [{"^/api/": "/"}] (for path rewrite)
}
],
Expand Down
1 change: 1 addition & 0 deletions _swbConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"host": "hostname",
"port": 80
},
"hostPattern": null, // (OPTIONAL) extract the last match from this regex executed on the url hostname, and replace the target host with it
"pathPatterns": [] // e.g. ["^/api/"] or [{"^/api/": "/"}] (for path rewrite)
}
],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "server-with-benefits",
"author": "Gal Ben-Ishay",
"version": "0.15.0",
"version": "0.16.0",
"engines": {
"node": ">=0.10"
},
Expand Down
21 changes: 16 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var clc = require('cli-color');
var p = require('path');
var cwd = process.cwd();

const
const
ENV_VAR_KEY = 'SWB_CONF_FILE',
CONF_FILE = (process.argv[2] && p.resolve(cwd, process.argv[2])) || process.env[ENV_VAR_KEY] || p.resolve(cwd, './swbConfig.json'),
// Console text styling
Expand Down Expand Up @@ -74,14 +74,24 @@ var getFixtureCheckRequestHandler = function(fixtures) {
}
};
};
var getProxyCheckRequestHandler = function(pathRegExp, proxyTarget, pathRewrite) {
var getProxyCheckRequestHandler = function(pathRegExp, proxyTarget, hostRegExp, pathRewrite) {
return function(req, res, next) {
if (pathRegExp.test(req.url)) {
var target = proxyTarget;
if (pathRewrite != null) {
req.url = req.url.replace(new RegExp(pathRegExp), pathRewrite);
}
if (hostRegExp) {
var resArr = hostRegExp.exec(req.hostname);
if (resArr) {
target = {
host: resArr[resArr.length - 1],
port: proxyTarget.port
};
}
}
proxy.web(req, res, {
target: proxyTarget
target: target
});
}
else {
Expand Down Expand Up @@ -120,18 +130,19 @@ conf.servers.forEach(function(serverConf) {
serverConf.proxy.forEach(function(proxy) {
var simplePaths = [];
var pathRewrites = [];
var hostRegExp = proxy.hostPattern && new RegExp(proxy.hostPattern);
proxy.pathPatterns.forEach(function(path) {
(typeof path == 'string' ? simplePaths : pathRewrites).push(path);
});
if (simplePaths.length) {
app.use(getProxyCheckRequestHandler(new RegExp("(?:" + simplePaths.join(")|(?:") + ")"), proxy.target));
app.use(getProxyCheckRequestHandler(new RegExp("(?:" + simplePaths.join(")|(?:") + ")"), proxy.target, hostRegExp));
messages += "\n" + featureStyle("Redirecting") + " path patterns: " + boldStyle(simplePaths.join(", ")) + " to "
+ proxyStyle((typeof proxy.target == 'string' ? proxy.target : (proxy.target.host + ":" + proxy.target.port)) + " (same path)");
}
pathRewrites.forEach(function(pathObj) {
var pathRegExp = Object.keys(pathObj)[0];
var pathRewrite = pathObj[pathRegExp];
app.use(getProxyCheckRequestHandler(new RegExp(pathRegExp), proxy.target, pathRewrite));
app.use(getProxyCheckRequestHandler(new RegExp(pathRegExp), proxy.target, hostRegExp, pathRewrite));
messages += "\n" + featureStyle("Redirecting") + " path patterns: " + boldStyle(pathRegExp) + " to "
+ proxyStyle((typeof proxy.target == 'string' ? proxy.target : (proxy.target.host + ":" + proxy.target.port)) + "/" + pathRewrite);
});
Expand Down

0 comments on commit d52e09d

Please sign in to comment.