-
Notifications
You must be signed in to change notification settings - Fork 321
/
server.js
75 lines (65 loc) · 2.34 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
require.paths.unshift(__dirname + '/lib');
require.paths.unshift(__dirname);
var sys = require('sys'),
http = require('http'),
ws = require('deps/node-ws/ws'),
proxy = require('proxy'),
weekly = require('weekly'),
mongo = require('deps/node-mongodb-native/lib/mongodb'),
Hummingbird = require('hummingbird').Hummingbird;
var TRACKING_PORT = 8000,
WEB_SOCKET_PORT = 8080,
MONITOR_PORT = 8888;
var db = new mongo.Db('hummingbird', new mongo.Server('localhost', 27017, {}), {});
db.open(function(p_db) {
var hummingbird = new Hummingbird(db, function() {
http.createServer(function(req, res) {
try {
hummingbird.serveRequest(req, res);
} catch(e) {
hummingbird.handleError(req, res, e);
}
}).listen(TRACKING_PORT);
});
sys.puts('Tracking server running at http://localhost:' + TRACKING_PORT + '/tracking_pixel.gif');
// Websocket TCP server
ws.createServer(function (websocket) {
websocket.addListener("connect", function (resource) {
// emitted after handshake
sys.log("ws connect: " + resource);
if (resource === '/') {
hummingbird.addClient(websocket);
}
else if (resource === '/aggregates') {
hummingbird.serveAggregates(websocket);
}
}).addListener("close", function () {
// emitted when server or client closes connection
hummingbird.removeClient(websocket);
sys.log("ws close");
});
}).listen(WEB_SOCKET_PORT);
sys.puts('Web Socket server running at ws://localhost:' + WEB_SOCKET_PORT);
var staticAssets = require('static_assets');
try {
http.createServer(function(req, res) {
if(req.url.match(/\/sale_list/)) {
proxy.route("/sale_list",
"http://www.gilt.com/pagegen_service/sale/sale_list", req, res);
} else if(req.url.match(/\/week.json/)) {
if(req.url.match(/use_prod/)) {
proxy.route("/week.json",
"http://hummingbird.giltrunway.com:8088/week.json", req, res);
}
else {
weekly.serve(db, req, res);
}
} else {
staticAssets.serveStatic(req, res);
}
}).listen(MONITOR_PORT);
sys.puts('Analytics server running at http://localhost:' + MONITOR_PORT + '/');
} catch(e) {
sys.puts('Detected webserver already running on http://localhost:' + MONITOR_PORT + '.');
}
});