service-router
Version:
router to bounce requests to either api or web servers
29 lines (26 loc) • 951 B
JavaScript
var url = require('url');
var inspect = require('eyespect').inspector();
var path = require('path');
var serviceDown = require(path.join(__dirname, './serviceDown'));
module.exports = function(req, res, bounce, server) {
var parts = url.parse(req.url);
var urlPath = parts.path
var service = 'web';
var apiPattern = /^\/api/;
if (apiPattern.test(urlPath)) {
service = 'api'
}
var ps = server.query(service);
// inspect({service: service, req_headers: req.headers}, 'bouncing for service');
// no servers available for the given service
if (ps.length === 0) {
inspect(service, 'no servers available');
serviceDown(req, res, bounce, service);
return;
}
inspect(urlPath, 'bouncing to service: ' + service + ' for url')
// bounce api request to a random server registered with seaport
var serverIndex = ps[Math.floor(Math.random()*ps.length)];
inspect(serverIndex, 'bouncing now')
bounce(serverIndex)
};