Created
November 11, 2012 22:35
-
-
Save shripadk/4056550 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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