Skip to content

Instantly share code, notes, and snippets.

@shripadk
Created November 11, 2012 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shripadk/4056550 to your computer and use it in GitHub Desktop.
Save shripadk/4056550 to your computer and use it in GitHub Desktop.
var fs = require('fs');
function removeSockFile(file) {
if(fs.existsSync(__dirname+"/tmp/"+file)) {
fs.unlinkSync(__dirname+"/tmp/"+file);
}
}
removeSockFile('app.sock');
removeSockFile('main.sock');
removeSockFile('agent.sock');
var http = require('http');
var cp = require('child_process');
var httpProxy = require('http-proxy');
var sentinelChild = cp.spawn('node', ['app.js'], {stdio: 'inherit'});
var agentChild = cp.spawn('node', ['agent/agent.js'], {stdio: 'inherit'});
var agent = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: ':./tmp/agent.sock'
}
});
var sentinel = new httpProxy.HttpProxy({
target: {
host: 'localhost',
port: ':./tmp/app.sock'
}
});
var server = httpProxy.createServer(function (req, res) {
if(req.url.match(new RegExp("^\/agent"))) {
agent.proxyRequest(req, res);
} else {
sentinel.proxyRequest(req, res);
}
});
server.on('upgrade', function(req, socket, head) {
if(req.url.match(new RegExp("^\/agent"))) {
agent.proxyWebSocketRequest(req, socket, head);
} else {
sentinel.proxyWebSocketRequest(req, socket, head);
}
});
server.listen(3000);
process.on('exit', function() {
sentinelChild.kill();
agentChild.kill();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment