-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (61 loc) · 1.92 KB
/
index.js
File metadata and controls
70 lines (61 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var http = require('http');
var httpProxy = require('http-proxy');
var redis = require('redis');
var ready;
function buildProxy() {
var proxy = new httpProxy.RoutingProxy();
var cluster_prefix = require('./config').cluster_prefix;
var redis_opts = require('./config').redis_opts;
var client = redis.createClient(redis_opts.port, redis_opts.host);
client.on('ready', startServer);
function checkForCluster(cluster_id, callback) {
client.get(cluster_prefix + cluster_id, function(err, reply) {
if (reply === null) {
return callback(new Error("Cluster " + cluster_id + " not found"));
} else {
return callback(null, JSON.parse(reply).cluster);
}
});
}
function passOnToCluster(req, res, cluster) {
return proxy.proxyRequest(req, res, cluster.nodes[0]);
}
function handler(req, res){
var auth = req.headers.authorization;
var cluster_id = req.headers['x-cluster-id'];
if (auth && cluster_id){
checkForCluster(cluster_id, function(err, cluster) {
if (err) {
res.writeHead(404);
return res.end();
} else {
if (auth === cluster.authToken) {
return passOnToCluster(req, res, cluster);
} else {
res.writeHead(401);
return res.end();
}
}
});
} else {
res.writeHead(401);
res.end();
}
}
var port = process.env.RIAK_RIVER_PROXY_PORT || 8098;
function startServer() {
var server = http.createServer(handler).listen(port, function(){
if (process.env.NODE_ENV !== 'PRODUCTION' &&
process.env.NODE_ENV !== 'TESTING') {
console.log('Riak River Proxy running on', port);
console.log('Riak River Proxy Redis running on', redis_opts);
}
if (typeof ready === 'function') ready(port, redis_opts);
});
}
}
function exported(done) {
ready = done;
return buildProxy;
}
module.exports = exported;