1 var sys = require('sys');
2 var url = require('url');
3 var app = require('http').createServer(handler);
4 var fs = require('fs');
5 var util = require('util');
6 var ns = require('node-static');
7 var io = require('socket.io').listen(app);
8 var hpfeeds = require('hpfeeds');
9 var file = new(ns.Server)("../static/", { cache: 600 });
10 var sanitize = require('validator').sanitize;
12 eval(fs.readFileSync('server_hpfeeds_config.js').toString());
14 // Listen and drop privileges
15 app.listen(config.port);
16 process.setuid(config.uid);
18 // Production settings for socket.io
19 io.enable('browser client minification'); // send minified client
20 io.enable('browser client etag'); // apply etag caching logic based on version number
21 io.enable('browser client gzip'); // gzip the file
22 io.set('log level', 1); // reduce logging
25 var feedconn = new hpfeeds.HPC(
26 config.hpfeeds.server,
31 feedconn.onready(function() { feedconn.subscribe('geoloc.events'); });
33 // Serve static content
34 function handler (req, res) {
36 console.log('New request: ' + req.connection.remoteAddress + ': ' + url.parse(req.url).href);
37 req.addListener('end', function() {
38 file.serve(req, res, function(err, result) {
40 console.error('Error serving %s: %s', req.url, err.message);
41 if (err.status === 404 || err.status === 500) {
42 file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
44 res.writeHead(err.status, err.headers);
53 res.end('Internal Server Error');
57 // Push feed data to all connected sockets
58 feedconn.msgcb = function(id, chan, data) {
60 io.sockets.emit('marker', {
61 latitude: data.latitude, longitude: data.longitude,
62 countrycode: data.countrycode, country: data.country, city: data.city,
64 latitude2: data.latitude2, longitude2: data.longitude2,
65 countrycode2: data.countrycode2, country2: data.country2, city2: data.city2,
67 type: data.type ? sanitize(data.type).xss() : null,
68 md5: data.md5 ? sanitize(data.md5).xss() : null
73 io.sockets.on('connection', function (socket) {
74 socket.on('disconnect', function() {
75 delete socket.namespace.sockets[socket.id];