-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
93 lines (81 loc) · 2.97 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
// Imports
const express = require('express'),
app = express(),
path = require('path'),
session = require('express-session'),
MongoConnection = require('./db/index.js'),
MongoStore = require('connect-mongodb-session')(session),
Config = require('./config/config.json'),
flash = require('connect-flash'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
helpers = require('./helpers/helpers.js'),
User = require('./db/models/user.js');
/* Middlewares that enable us to:
- Serve static pages
- Use Pug to render
- Receive better formatted POST requests
- Have Sessions
- Have Flashes (redirect messages)
- Have Routes
- Parse JSON body
- Serve Errors */
app.use(express.static(path.join(__dirname, 'public')));
app.set('view engine', 'pug');
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
// Configure Mongo & Express Session Store
const store = new MongoStore({
uri: process.env.MONGODB_URI || Config.session.connection,
collection: Config.session.collection
});
app.use(session({
store: store,
resave: false,
saveUninitialized: true,
secret: Config.session.secret
}));
// Pass username to Pug templates when possible
app.use(async (req, res, next) => {
res.locals.username = req.session.username;
res.locals.versionCode = Config.versionCode;
res.locals.version = Config.version;
next();
})
// Configure Flashes and Cookies
app.use(cookieParser());
app.use(flash());
// Routing requests to the correct routers
const routes = require('./routes/routes.js');
app.use('/', routes.index);
app.use('/vuln', routes.vuln);
app.use('/activity', routes.activity);
app.use('/user', routes.user);
app.use('/files', routes.files);
app.use('/settings', routes.settings);
// LetsEncrypt
if (process.env.ACME_URI && process.env.ACME_SERVE) {
app.get(`/.well-known/acme-challenge/${process.env.ACME_URI}`, (req, res) => {
res.send(process.env.ACME_SERVE);
});
}
app.all('/*', (req, res, next) => {
return helpers.sendError(res, 400);
})
// Host the app on the port specified so it is accessible with a browser
const server = app.listen(process.env.PORT || Config.port, () => {
console.log(`VulnerabiliTea started on port ${process.env.PORT || Config.port}`);
});
// Graceful Shutdown
process.on('SIGINT' || 'SIGTERM', async () => {
console.info('Shutdown signal received.\nVulnerabiliTea is shutting down...');
console.log('Shutting down HTTP server.');
await server.close();
console.log('HTTP server closed.');
console.log('Disconnecting from MongoDB server...');
await MongoConnection.close(false);
console.log('MongoDB connection closed.');
console.log('Everything finished, killing process with exit code 0.');
process.exit(0);
});