-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
96 lines (90 loc) · 2.19 KB
/
server.js
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright (c) 2015, Joyent, Inc.
*/
var redis = require('ioredis');
var bunyan = require('bunyan');
var config = require('./lib/config');
var DNSServer = require('./lib/dns-server');
var APIServer = require('./lib/api-server');
var restify = require('restify');
var path = require('path');
var cueball = require('cueball');
var EventEmitter = require('events').EventEmitter;
var confPath;
if (process.argv[2])
confPath = process.argv[2];
if (confPath === undefined)
confPath = path.join(__dirname, 'etc', 'config.json');
var conf = config.parse(confPath);
var log = bunyan.createLogger({
name: 'cns',
level: process.env.LOGLEVEL || 'debug',
serializers: {
req: restify.bunyan.serializers.req,
res: restify.bunyan.serializers.res
}
});
var res = cueball.resolverForIpOrDomain({ input: '127.0.0.1:6379' });
var redisPool = new cueball.ConnectionPool({
resolver: res,
domain: 'localhost',
service: '_redis._tcp',
defaultPort: 6379,
log: log,
spares: 4,
maximum: 12,
recovery: {
default: {
timeout: 2000,
delay: 500,
retries: 5
}
},
constructor: function (backend) {
var c = redis.createClient({
host: backend.address,
port: backend.port,
enableOfflineQueue: false,
connectTimeout: 30000,
dropBufferSupport: true
});
c.destroy = function () {
c.end(false);
};
c.emit = function () {
var args = arguments;
/*
* The redis client emits 'ready' when it's actually
* ready for use. It also emits 'connect', before
* it's ready but after the TCP socket connects. We
* don't care about that, so drop it.
*/
if (args[0] === 'connect')
return (this);
if (args[0] === 'ready')
args[0] = 'connect';
return (EventEmitter.prototype.emit.apply(this, args));
};
c.unref = function () {};
c.ref = function () {};
return (c);
}
});
res.start();
var s = new DNSServer({
redisPool: redisPool,
log: log,
config: conf,
port: 53
});
var api = new APIServer({
redisPool: redisPool,
log: log,
config: conf,
port: 80,
dnsServer: s
});