forked from pishifat/qat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
154 lines (137 loc) · 4.84 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
const express = require('express');
const http = require('http');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const mongoose = require('mongoose');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const config = require('./config.json');
require('express-async-errors');
// Return the 'new' updated object by default when doing findByIdAndUpdate
mongoose.plugin(schema => {
schema.pre('findOneAndUpdate', function() {
if (!('new' in this.options)) {
this.setOptions({ new: true });
}
});
});
const notifications = require('./helpers/notifications');
const indexRouter = require('./routes/index');
const bnAppRouter = require('./routes/bnApp');
const reportsRouter = require('./routes/reports');
const appEvalRouter = require('./routes/evaluations/appEval');
const bnEvalRouter = require('./routes/evaluations/bnEval').router;
const evalArchiveRouter = require('./routes/evaluations/evalArchive');
const dataCollectionRouter = require('./routes/dataCollection');
const manageReportsRouter = require('./routes/manageReports');
const usersRouter = require('./routes//users/users');
const natRouter = require('./routes/users/nat');
const vetoesRouter = require('./routes/vetoes');
const testSubmissionRouter = require('./routes/testSubmission');
const manageTestRouter = require('./routes/manageTest');
const testResultsRouter = require('./routes/testResults');
const discussionVoteRouter = require('./routes/discussionVote');
const qualityAssuranceRouter = require('./routes/qualityAssurance');
const logsRouter = require('./routes/logs');
const interOpRouter = require('./routes/interOp');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
// middlewares and such
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
//natdb
mongoose.connect(config.connection, {
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'db connection error:'));
db.once('open', function() {
console.log('natdb connected');
});
app.use(
session({
secret: config.session,
store: new MongoStore({ mongooseConnection: db }),
resave: false,
saveUninitialized: false,
})
);
app.use('/', indexRouter);
app.use('/bnApps', bnAppRouter);
app.use('/reports', reportsRouter);
app.use('/appEval', appEvalRouter);
app.use('/bnEval', bnEvalRouter);
app.use('/dataCollection', dataCollectionRouter);
app.use('/evalArchive', evalArchiveRouter);
app.use('/manageReports', manageReportsRouter);
app.use('/users', usersRouter);
app.use('/users/nat', natRouter);
app.use('/vetoes', vetoesRouter);
app.use('/testSubmission', testSubmissionRouter);
app.use('/managetest', manageTestRouter);
app.use('/testresults', testResultsRouter);
app.use('/discussionVote', discussionVoteRouter);
app.use('/qualityassurance', qualityAssuranceRouter);
app.use('/logs', logsRouter);
app.use('/interOp', interOpRouter);
// catch 404
app.use(function(req, res) {
res.render('index',{
layout: false,
loggedIn: req.session.mongoId,
});
});
// error handler
// eslint-disable-next-line no-unused-vars
app.use(function(err, req, res, next) {
if (err.name == 'DocumentNotFoundError') err.message = 'Not found';
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page or return error obj for json
if (req.accepts(['html', 'json']) === 'json') {
res.json({ error: err.message || 'Something went wrong!' });
} else {
res.status(err.status || 500);
res.render('error');
}
});
// server start
const port = process.env.PORT || '3001';
app.set('port', port);
const server = http.createServer(app);
server.listen(port);
server.on('error', (error) => {
if (error.syscall !== 'listen') {
throw error;
}
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(port + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(port + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
});
server.on('listening', () => {
console.log('Listening on ' + port);
notifications.notifyDeadlines.start();
notifications.notifyBeatmapReports.start();
notifications.lowActivityTask.start();
notifications.notifyQualityAssurance.start();
});
module.exports = app;