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;
+
+// Filename to serve.
+var json_filename = '/srv/analysis.sesse.net/www/analysis.json';
+if (process.argv.length >= 3) {
+ json_filename = process.argv[2];
+}
+
+// TCP port to listen on.
+var port = 5000;
+if (process.argv.length >= 4) {
+ port = parseInt(process.argv[3]);
+}
// 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.
}
var reread_file = function(event, filename) {
- if (filename != path.basename(JSON_FILENAME)) {
+ if (filename != path.basename(json_filename)) {
return;
}
if (json_lock >= 2) {
}
json_lock = 1;
- console.log("Rereading " + JSON_FILENAME);
- fs.open(JSON_FILENAME, 'r+', function(err, fd) {
+ console.log("Rereading " + json_filename);
+ fs.open(json_filename, 'r+', function(err, fd) {
if (err) throw err;
fs.fstat(fd, function(err, st) {
if (err) throw err;
touch_timer = setTimeout(function() {
console.log("Touching analysis.json due to no other activity");
var now = Date.now() / 1000;
- fs.utimes(JSON_FILENAME, now, now);
+ fs.utimes(json_filename, now, now);
}, 30000);
}
var possibly_wakeup_clients = function() {
'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';
// Set up a watcher to catch changes to the file, then do an initial read
// to make sure we have a copy.
-fs.watch(path.dirname(JSON_FILENAME), reread_file);
-reread_file(null, path.basename(JSON_FILENAME));
+fs.watch(path.dirname(json_filename), reread_file);
+reread_file(null, path.basename(json_filename));
var server = http.createServer();
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;
}
});
});
-server.listen(5000);
+
+server.listen(port);