Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Created May 20, 2015 21:01
Show Gist options
  • Save TrevorBasinger/2948e6aabe59e41f114b to your computer and use it in GitHub Desktop.
Save TrevorBasinger/2948e6aabe59e41f114b to your computer and use it in GitHub Desktop.
var R = require ('ramda'),
M = require ('control.monads'),
bp = require ('body-parser'),
fs = require ('fs'),
express = require ('express'),
log = console.log,
logI = console.logI,
THRESHOLD_MIN = 1,
isLessThanXMinutes = R.curry (function (x, now, machine) {
return now - machine.time < 1000 * 60 * x;
}),
nil = null;
(function main (args) {
log ("Starting...");
var i = 0;
var konstJson = R.curry (function (x, req, res) { res.json (x); });
var machines = {zk: [], masters: []};
var app = express ();
app.use (bp.json ());
app.post ('/soundoff', function (req, res) {
var role = req.body.role;
if (!role) { return res.code (400).send ("Bad Request. Need role."); }
var masters = machines.masters;
var zk = machines.zk;
var ip = req.connection.remoteAddress;
if (R.match (/master/i, role)) { masters = R.append ({host: ip, time: Date.now ()}, masters); }
if (R.match (/zk/i, role)) { zk = R.append ({host: ip, time: Date.now ()}, zk); }
machines.masters = masters;
machines.zk = zk;
return res.send ("");
});
app.get ('/masters', function (req,res) {
machines.masters = R.filter (isLessThanXMinutes (THRESHOLD_MIN, Date.now()), machines.masters);
res.send (machines.masters);
});
app.get ('/zk', function (req,res) {
machines.zk = R.filter (isLessThanXMinutes (THRESHOLD_MIN, Date.now()), machines.zk);
res.send (machines.zk);
});
app.listen (process.env.PORT || 8080);
})(process.argv);
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* This application is only supposed to be run once, behind a firewall.
* It should never be exposed to outside traffic.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment