Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

First attempt at adding the ability to dynamically addHost(host,target) and removeHost(host) to the ProxyTable #195

Closed
wants to merge 2 commits into from

5 participants

@tglines

I think it needs more tests/validation that I didn't mess something else up here, but it's a first stab at it.

Travis Glines First attempt at adding the ability to dynamically addHost(host,targe…
…t) and removeHost(host) to the ProxyTable
5e38f74
@tglines

Adresses:

#111
#126
#60

@tglines

I think it also needs a conditional for determining if it's a routing proxy or http proxy ... I'll wait for some feedback.

@indexzero
Owner

lgtm. I'll try to get this merged in this week.

@AndreasMadsen

@indexzero could we get an update on this?

@tglines

@indexzero @Marak hearing crickets here ...

@indexzero
Owner

Sorry. Backlog of other work. I'll try to review this week, but Nodejitsu core is taking up the lion's share of the team's time.

@devrim

this would be pretty sweet - we will then replace all our nginx frontend with node-http-proxy

@dudemeister

this would be awesome! we're currently moving from apache proxying to node-http-proxy and we'll be including this on our version...

@tglines

Wanted to let everyone here know of a method that works just fine since nodejitsu didn't talke the pull request.

You can pass a string through into options.router that's a path to a file that has the routes as json. The process then watches this file for changes and updates the routing. Basically you can achieve the same effect I was trying to achieve with this pull by having a process that edits that file.

Routing file (router.json):

{
    "router": {
        "nodejitsu.com/doesnt-take-pull-reqs": "127.0.0.1:5000"
    }
}

Options passed

var options = {
    router: "./router.json"
};
@tglines tglines closed this
@indexzero indexzero referenced this pull request from a commit
@indexzero indexzero [api test] Manually merge #195 from @tglines since that fork was dele…
…ted. Update tests to use new macros. Fixes #195. Fixes #60.
5d515e4
@indexzero indexzero referenced this pull request
Closed

Modify proxy table #126

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 12, 2012
  1. First attempt at adding the ability to dynamically addHost(host,targe…

    Travis Glines authored
    …t) and removeHost(host) to the ProxyTable
This page is out of date. Refresh to see the latest.
View
24 lib/node-http-proxy.js
@@ -192,6 +192,28 @@ exports.createServer = function () {
});
}
+ server.changeHost = function (host, target){
+ this.addHost(host, target);
+ }
+
+ server.addHost = function (host, target){
+ if(!proxy instanceof RoutingProxy){
+ throw new Error('Cannot update ProxyTable routes on HttpProxy.');
+ }
+ else{
+ proxy.addHost(host, target);
+ }
+ }
+
+ server.removeHost = function (host){
+ if(!proxy instanceof RoutingProxy){
+ throw new Error('Cannot update ProxyTable routes on HttpProxy.');
+ }
+ else{
+ proxy.removeHost(host);
+ }
+ }
+
//
// Set the proxy on the server so it is available
// to the consumer of the server
@@ -392,4 +414,4 @@ exports._getBase = function _getBase (options) {
}
return result;
-};
+};
View
31 lib/node-http-proxy/proxy-table.js
@@ -81,6 +81,37 @@ var ProxyTable = exports.ProxyTable = function (options) {
util.inherits(ProxyTable, events.EventEmitter);
//
+// ### function addRoute (route, target)
+// #### @route {String} String containing route coming in
+// #### @target {String} String containing the target
+// Adds a host-based route to this instance.
+//
+ProxyTable.prototype.addRoute = function (route, target) {
+ if (!this.router) {
+ throw new Error('Cannot update ProxyTable routes without router.');
+ }
+
+ this.router[route] = target;
+
+ this.setRoutes(this.router);
+};
+
+//
+// ### function removeRoute (route)
+// #### @route {String} String containing route to remove
+// Removes a host-based route from this instance.
+//
+ProxyTable.prototype.removeRoute = function (route) {
+ if (!this.router) {
+ throw new Error('Cannot update ProxyTable routes without router.');
+ }
+
+ delete this.router[route];
+
+ this.setRoutes(this.router);
+};
+
+//
// ### function setRoutes (router)
// #### @router {Object} Object containing the host based routes
// Sets the host-based routes to be used by this instance.
View
23 lib/node-http-proxy/routing-proxy.js
@@ -105,6 +105,29 @@ RoutingProxy.prototype.remove = function (options) {
};
//
+// ### function addHost (host, target)
+// #### @host {String} Host to add to proxyTable
+// #### @target {String} Target to add to proxyTable
+// Adds a host to proxyTable
+//
+RoutingProxy.prototype.addHost = function (host, target) {
+ if (this.proxyTable) {
+ this.proxyTable.addRoute(host, target);
+ }
+};
+
+//
+// ### function removeHost (host)
+// #### @host {String} Host to remove from proxyTable
+// Removes a host to proxyTable
+//
+RoutingProxy.prototype.removeHost = function (host) {
+ if (this.proxyTable) {
+ this.proxyTable.removeRoute(host);
+ }
+};
+
+//
// ### function close()
// Cleans up any state left behind (sockets, timeouts, etc)
// associated with this instance.
View
24 test/http/routing-proxy-test.js
@@ -43,6 +43,18 @@ var hostnameOptions = {
}
};
+var staticOptions = {
+ hostnameOnly: true,
+ router: {
+ "static.com": "127.0.0.1:8121",
+ "removed.com": "127.0.0.1:8122",
+ "change.com": "127.0.0.1:8123"
+ }
+};
+
+var dynamicHost = "dynamic1.com";
+var dynamicTarget = "127.0.0.1:8125";
+
vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"When using server created by httpProxy.createServer()": {
"when passed a routing table": {
@@ -63,6 +75,18 @@ vows.describe('node-http-proxy/routing-proxy/' + testName).addBatch({
"an incoming request to foo.com": runner.assertProxied('foo.com', 8093, 8094),
"an incoming request to bar.com": runner.assertProxied('bar.com', 8093, 8095),
"an incoming request to unknown.com": runner.assertResponseCode(8093, 404)
+ },
+ "and adding a route by Hostname": {
+ topic: function () {
+ this.server = runner.startProxyServerWithTable(8120, staticOptions, this.callback);
+ this.server.removeHost('removed.com');
+ this.server.addHost(dynamicHost, dynamicTarget);
+ this.server.changeHost('change.com', '127.0.0.1:8124');
+ },
+ "an incoming request to static.com": runner.assertProxied('static.com', 8120, 8121),
+ "an incoming request to removed.com": !runner.assertProxied('removed.com', 8120, 8122),
+ "an incoming request to change.com": runner.assertProxied('change.com', 8120, 8124),
+ "an incoming request to dynamic1.com": runner.assertProxied('dynamic1.com', 8120, 8125)
}
},
"when passed a routing file": {
Something went wrong with that request. Please try again.