Last active
January 5, 2016 15:06
-
-
Save jaumard/047f10b7c0563b4bd045 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Node.js proxy to redirect domain to correct server | |
* Also check is server is alive or send a maintenance page | |
*/ | |
var http = require('http'), | |
httpProxy = require('http-proxy'), | |
proxy = httpProxy.createProxyServer({}), | |
request = require('request'), | |
domains = { | |
"domain1.com" : "localhost:8080", | |
"dev.domain1.com" : "localhost:8081", | |
"domain2.com" : "localhost:8082", | |
"default" : "localhost:8080" | |
}; | |
var maintenance = "<!DOCTYPE html>"+ "<title>Site Maintenance</title>" + "<style>" + " body { text-align: center; padding: 150px; }" + " h1 { font-size: 50px; }" + " body { font: 20px Helvetica, sans-serif; color: #333; }" + " article { display: block; text-align: left; width: 650px; margin: 0 auto; }" + " a { color: #1bbc9b; text-decoration: none; }" + " a:hover { color: #109a7e; text-decoration: none; }" + "</style>" + "<article>" + " <h1>We’ll be back soon!</h1>" + " <div>" + " <p>Sorry for the inconvenience but we’re performing some maintenance at the moment. If you need to you can always <a href=\"mailto:[email protected]\">contact us</a>, otherwise we’ll be back online shortly!</p>" + " <p>— My Team</p>" + " </div>" + "</article>"; | |
//Check is server is alive, if not respond a maintenance page | |
var checkServerAvailability = function (req, res, url) | |
{ | |
request(url + '/isAlive', function (error, response, body) | |
{ | |
if (!error && response.statusCode == 200) | |
{ | |
proxy.web(req, res, {target : url}); | |
} | |
else | |
{ | |
res.writeHeader(503, {"Content-Type" : "text/html"}); | |
res.write(maintenance); | |
res.end(); | |
} | |
}); | |
}; | |
var server = http.createServer(function (req, res) | |
{ | |
var hostname = ""; | |
//If server call with domaine and not call with IP directly | |
if (req.headers.host) | |
{ | |
hostname = req.headers.host.split(":")[0]; | |
} | |
//If it's a know domain, redirect to correct server | |
if (domains[hostname]) | |
{ | |
checkServerAvailability(req, res, 'http://' + domains[hostname]); | |
} | |
else | |
{ | |
checkServerAvailability(req, res, 'http://' + domains["default"]); | |
} | |
}); | |
// Listen to the `upgrade` event and proxy the | |
// WebSocket requests as well. | |
server.on('upgrade', function (req, socket, head) | |
{ | |
var hostname = ""; | |
//If server call with domaine | |
if (req.headers.host) | |
{ | |
hostname = req.headers.host.split(":")[0]; | |
} | |
//If it's a know domain, redirect to correct server | |
if (domains[hostname]) | |
{ | |
proxy.web(req, socket, {target : 'ws://' + domains[hostname]}); | |
} | |
else | |
{ | |
proxy.web(req, socket, {target : 'ws://' + domains["default"]}); | |
} | |
}); | |
//Catch error to prevent proxy crash | |
proxy.on('error', function (err) | |
{ | |
console.error(err); | |
}); | |
server.listen(80, function () | |
{ | |
console.info('proxy listening on port 80'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment