node-open-mining-portal/libs/api.js
2018-01-30 18:49:38 -07:00

57 lines
1.6 KiB
JavaScript

var redis = require('redis');
var async = require('async');
var stats = require('./stats.js');
module.exports = function(logger, portalConfig, poolConfigs){
var _this = this;
var portalStats = this.stats = new stats(logger, portalConfig, poolConfigs);
this.liveStatConnections = {};
this.handleApiRequest = function(req, res, next){
switch(req.params.method){
case 'stats':
res.writeHead(200, { 'Content-Type: application/json' });
res.end(portalStats.statsString);
return;
case 'pool_stats':
res.writeHead(200, { 'Content-Type: application/json' });
res.end(JSON.stringify(portalStats.statPoolHistory));
return;
case 'live_stats':
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('\n');
var uid = Math.random().toString();
_this.liveStatConnections[uid] = res;
req.on("close", function() {
delete _this.liveStatConnections[uid];
});
return;
default:
next();
}
};
this.handleAdminApiRequest = function(req, res, next){
switch(req.params.method){
case 'pools': {
res.end(JSON.stringify({result: poolConfigs}));
return;
}
default:
next();
}
};
};