forked from RandomAPI/Randomuser.me-Node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
76 lines (69 loc) · 2.13 KB
/
server.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
var fs = require('fs');
var async = require('async');
var server = require('./app').server;
var app = require('./app').app;
Generator = {};
datasets = {};
injects = {};
clients = {};
// Load in all generators and datasets before starting the server
// Scan api folder for available versions
var versions = fs.readdirSync('./api').filter(dir => ['.DS_Store', '.nextRelease'].indexOf(dir) === -1);
async.forEachOf(versions, (value, key, callback) => {
require('./api/' + value + '/loadDatasets')(data => {
Generator[value] = require('./api/' + value + '/api');
datasets[value] = data;
callback();
});
}, function(err, results) {
var gKeys = Object.keys(Generator);
var dKeys = Object.keys(datasets);
console.log("Loaded " + gKeys.length + " generator" + (gKeys.length == 1 ? "" : "s") + ".");
gKeys.forEach(key => {
var nats = "", count = 0;;
Object.keys(datasets[key]).filter(key => ["common", "LEGO"].indexOf(key) === -1).forEach(function(dkey) {
nats += dkey + " ";
count++;
});
console.log("• v" + key + " [" + count + "]: " + nats);
});
console.log("");
startServer();
});
function startServer() {
server.listen(app.get('port'));
server.on('error', error => {
var bind = app.get('port');
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
});
server.on('listening', () => {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
});
// Client limit reset
setInterval(() => {
var offenders = {};
Object.keys(clients).forEach(client => {
if (clients[client] >= settings.limit) {
offenders[client] = clients[client];
}
});
if (Object.keys(offenders).length > 0) console.log(offenders);
clients = {};
global.gc();
}, settings.resetInterval);
}