-
Notifications
You must be signed in to change notification settings - Fork 1
/
API.js
53 lines (50 loc) · 1.44 KB
/
API.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
var r = require('rethinkdb'),
Promise = require('bluebird'),
ip2asn = require('ip2asn')(),
config = require('./config'),
bunyan = require('bunyan'),
stream = require('gelf-stream'),
discover = require('./lib/discover'),
log;
if (!!config.graylog) {
log = bunyan.createLogger({
name: 'API',
streams: [{
type: 'raw',
stream: stream.forBunyan(config.graylog.host, config.graylog.port)
}]
});
}else{
log = bunyan.createLogger({
name: 'API'
});
}
var endpoint = false;
discover().then(function(ip) {
if (ip !== null) {
config.rethinkdb.host = ip;
endpoint = ip;
}
r.connect(config.rethinkdb).then(function(conn) {
r.conn = conn;
var opts = { url: 'https://dermail.objectstore.co/files/GeoIPASNum2.zip' };
ip2asn.lastUpdated(function(err, t) {
if (err) {
log.error(e)
}else{
if (t > 29) {
opts.update = true;
}
ip2asn.load(opts);
}
});
ip2asn.on('ready', function() {
var app = require('./app')(r, ip2asn, endpoint);
var port = config.cluster.basePort;
var server = app.listen(port);
var io = require('socket.io')(server);
require('./lib/socket')(io, r, config);
log.info('Process ' + process.pid + ' is listening on port ' + port + ' to incoming API requests.')
})
});
})