forked from mrmikeo/nomppro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.js
56 lines (45 loc) · 1.61 KB
/
api.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
}
};
};