Make serve-analysis.js capable of listening on a different port.
[remoteglot] / www / serve-analysis.js
index 948ea96..40d4f19 100644 (file)
@@ -13,6 +13,10 @@ var delta = require('./js/json_delta.js');
 // Constants.
 var JSON_FILENAME = '/srv/analysis.sesse.net/www/analysis.json';
 var HISTORY_TO_KEEP = 5;
+var MINIMUM_VERSION = null;
+
+// TCP port to listen on; can be overridden with flags.
+var port = 5000;
 
 // If set to 1, we are already processing a JSON update and should not
 // start a new one. If set to 2, we are _also_ having one in the queue.
@@ -93,8 +97,8 @@ var create_json_historic_diff = function(new_json, history_left, new_diff_json,
        zlib.gzip(diff_text, function(err, buffer) {
                if (err) throw err;
                new_diff_json[histobj.last_modified] = {
-                       plain: diff,
-                       text: diff_text,
+                       parsed: diff,
+                       plain: diff_text,
                        gzip: buffer,
                        last_modified: new_json.last_modified,
                };
@@ -182,19 +186,23 @@ var send_json = function(response, ims, accept_gzip, num_viewers) {
                'Content-Type': 'text/json',
                'X-RGLM': this_json.last_modified,
                'X-RGNV': num_viewers,
-               'Access-Control-Expose-Headers': 'X-RGLM, X-RGNV',
+               'Access-Control-Expose-Headers': 'X-RGLM, X-RGNV, X-RGMV',
                'Vary': 'Accept-Encoding',
        };
 
+       if (MINIMUM_VERSION) {
+               headers['X-RGMV'] = MINIMUM_VERSION;
+       }
+
        if (accept_gzip) {
                headers['Content-Length'] = this_json.gzip.length;
                headers['Content-Encoding'] = 'gzip';
                response.writeHead(200, headers);
                response.write(this_json.gzip);
        } else {
-               headers['Content-Length'] = this_json.text.length;
+               headers['Content-Length'] = this_json.plain.length;
                response.writeHead(200, headers);
-               response.write(this_json.text);
+               response.write(this_json.plain);
        }
        response.end();
 }
@@ -243,7 +251,7 @@ server.on('request', function(request, response) {
        var ims = (u.query)['ims'];
        var unique = (u.query)['unique'];
 
-       console.log((new Date).getTime()*1e-3 + " " + request.url);
+       console.log(((new Date).getTime()*1e-3).toFixed(3) + " " + request.url);
        if (u.pathname === '/override-num-viewers') {
                handle_viewer_override(request, u, response);
                return;
@@ -292,4 +300,8 @@ server.on('connection', function(socket) {
                }
        });
 });
-server.listen(5000);
+
+if (process.argv.length >= 3) {
+       port = parseInt(process.argv[2]);
+}
+server.listen(port);