-
Notifications
You must be signed in to change notification settings - Fork 273
/
app.js
110 lines (95 loc) · 2.77 KB
/
app.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express');
const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const bodyParser = require('body-parser');
const http = require('http');
const compress = require('compression');
const cors = require('cors');
const db = require('./models/db')(process.env.spec);
const index = require('./routes/index');
const api = require('./routes/api');
const stats = require('./routes/stats');
const settings = require('./settings');
const store = require('./store');
const app = express();
const server = http.createServer(app);
require('./sockets.js')(server);
// view engine setup
app.set('views', path.join(__dirname, '.viewsMin'));
app.set('view engine', 'ejs');
// middleware
app.use(favicon(path.join(__dirname, 'public', 'img', 'favicon.png')));
app.set('port', settings.port);
//app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cors());
app.use(express.static(path.join(__dirname, 'public')));
app.use(compress());
// routes
app.use('/', index);
app.use('/api', api);
app.use('/getStats', stats);
// catch 404 and forward to error handler
app.use((req, res, next) => {
let err = new Error('Not Found');
err.status = 404;
next(err);
});
// production error handler
// no stacktraces leaked to user
app.use((err, req, res, next) => {
console.log(err);
res.sendStatus(err.status || 500);
});
// Load and initialize generators before starting server
(async() => {
await require('./loadGenerators')();
startServer();
})();
store.set('clients', {});
let clients = store.get('clients');
// Client limit reset
setInterval(() => {
let 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 = {};
store.set('clients', clients);
if (process.env.spec !== "true") global.gc();
}, settings.resetInterval);
function startServer() {
server.listen(app.get('port'));
server.on('error', error => {
let 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', () => {
let addr = server.address();
let bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
console.log('Listening on ' + bind);
server.emit("serverStarted");
});
}
module.exports = {
app,
server,
};