-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (37 loc) · 1.55 KB
/
server.js
File metadata and controls
44 lines (37 loc) · 1.55 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
var net = require('net');
var fs = require('fs');
var util = require('util');
var logging = require('./lib/logging'), logger = logging.logger;
var configFile = process.argv[2] || "config/config.json";
logger.info('using '+ configFile + ' as configuration source');
var config = JSON.parse(fs.readFileSync(configFile));
logger = logging.setupLogging(config.debug, config.loggers);
var RedisProxy = require('./lib/redis_proxy');
var redis_proxy = new RedisProxy(config);
var bindAddress = config.bind_address || "127.0.0.1",
listenPort = config.listen_port || 9999;
var server = net.createServer(function (socket) {
var id = socket.remoteAddress+':'+socket.remotePort
logger.debug('client connected ' + id);
socket.on('end', function() {
logger.info('client disconnected');
// Hack to get the connection identifier, so that we can release the connection
// the usual socket.remoteAddress, socket.remotePort don't seem to work after connection has ended.
if(this._peername){
redis_proxy.quit(this._peername.address+':'+this._peername.port);
}
});
socket.on('data', function(data) {
var command = data.toString('utf8'), id = socket.remoteAddress+':'+socket.remotePort;
redis_proxy.sendCommand(command, id, function(err, response) {
if( response) response.unpipe();
if(err){
return socket.write("-ERR Error Happened "+ err);
}
response.pipe(socket);
})
});
});
redis_proxy.watch();
server.listen(listenPort, bindAddress);
logger.info("Redis proxy is listening on " +bindAddress+" : " + listenPort);