Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 289
Expand file tree
/
Copy pathapp.js
More file actions
Latest commit
110 lines (95 loc) · 2.77 KB
/
Copy pathapp.js
File metadata and controls
110 lines (95 loc) · 2.77 KB
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
constexpress=require('express');
constpath=require('path');
constfavicon=require('serve-favicon');
constlogger=require('morgan');
constbodyParser=require('body-parser');
consthttp=require('http');
constcompress=require('compression');
constcors=require('cors');
constdb=require('./models/db')(process.env.spec);
constindex=require('./routes/index');
constapi=require('./routes/api');
conststats=require('./routes/stats');
constsettings=require('./settings');
conststore=require('./store');
constapp=express();
constserver=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)=>{
leterr=newError('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()=>{
awaitrequire('./loadGenerators')();
startServer();
})();
store.set('clients',{});
letclients=store.get('clients');
// Client limit reset
setInterval(()=>{
letoffenders={};
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);
functionstartServer(){
server.listen(app.get('port'));
server.on('error',error=>{
letbind=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:
throwerror;
}
});
server.on('listening',()=>{
letaddr=server.address();
letbind=typeofaddr==='string'
? 'pipe '+addr
: 'port '+addr.port;
console.log('Listening on '+bind);
server.emit("serverStarted");
});
}
module.exports={
app,
server,
};