This repository was archived by the owner on Jan 19, 2022. It is now read-only.
forked from bdotdub/hummingbird
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
101 lines (81 loc) · 3.02 KB
/
server.js
File metadata and controls
101 lines (81 loc) · 3.02 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
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
97
98
99
100
101
require.paths.unshift(__dirname + '/lib');
require.paths.unshift(__dirname);
var http = require('http'),
weekly = require('weekly'),
fs = require('fs'),
dgram = require('dgram'),
static = require('node-static'),
io = require('socket.io'),
mongo = require('mongodb'),
Hummingbird = require('hummingbird').Hummingbird;
try {
var configJSON = fs.readFileSync(__dirname + "/config/app.json");
} catch(e) {
console.log("File config/app.json not found. Try: `cp config/app.json.sample config/app.json`");
}
var config = JSON.parse(configJSON.toString());
if(process.env.DUOSTACK_DB_MYSQL) {
var parseUrl = require('url').parse;
var components = parseUrl(process.env['DUOSTACK_DB_MYSQL']);
config.mongo_host = components.hostname;
config.mongo_port = components.port;
config.mongo_name = components.pathname.substr(1);
config.mongo_user = components.auth.split(':')[0];
config.mongo_password = components.auth.split(':')[1];
}
db = new mongo.Db(config.mongo_name || 'hummingbird',
new mongo.Server(config.mongo_host, config.mongo_port, {}), {});
db.addListener("error", function(error) {
console.log("Error connecting to mongo -- perhaps it isn't running?");
});
db.open(function(p_db) {
if(config.mongo_user && config.mongo_password) {
db.authenticate(config.mongo_user, config.mongo_password,
function(err, success) {
if(success) {
callback(null, db);
} else {
console.log("Error authenticating with mongo");
}
});
}
var hummingbird = new Hummingbird();
hummingbird.init(db, function() {
var server = http.createServer(function(req, res) {
try {
hummingbird.serveRequest(req, res);
} catch(e) {
hummingbird.handleError(req, res, e);
}
});
server.listen(config.tracking_port, "0.0.0.0");
socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
client.on('disconnect', function(){ console.log("Lost ws client"); })
});
hummingbird.socket = socket;
hummingbird.addAllMetrics(socket, db);
console.log('Web Socket server running at ws://*:' + config.tracking_port);
if(config.udp_address) {
var udpServer = dgram.createSocket("udp4");
udpServer.on("message", function(message, rinfo) {
console.log("message from " + rinfo.address + " : " + rinfo.port);
var data = JSON.parse(message);
hummingbird.insertData(data);
});
udpServer.bind(config.udp_port, config.udp_address);
console.log('UDP server running on UDP port ' + config.udp_port);
}
});
console.log('Tracking server running at http://*:' + config.tracking_port + '/tracking_pixel.gif');
});
if(config.enable_dashboard) {
var file = new(static.Server)('./public');
http.createServer(function (request, response) {
request.addListener('end', function () {
file.serve(request, response);
});
}).listen(config.dashboard_port);
console.log('Dashboard server running at http://*:' + config.dashboard_port);
}