Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.