forked from JulienPlanchetCoineo/frostybot-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
74 lines (51 loc) · 1.62 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
var express = require('express');
var bodyParser = require('body-parser');
const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
var context = require('express-http-context');
// Set App Title
process.title = "frostybot-js";
// Load Modules
const loader = require('./core/core.loader');
loader.load_all()
// API Router
var apiRouter = require('./routes/routes.api');
// Load Express
var app = express();
// Get Listen Port
const portfile = __dirname + '/.port';
var port = 80
try {
var port = fs.readFileSync(portfile, {encoding:'utf8', flag:'r'})
} catch {
var port = (process.env.FROSTYBOT_PORT || 80);
}
app.set('port', port);
fs.writeFileSync(portfile, port)
// Trust reverse proxy if used
// Only use this if you are configuring Frostybot behind a reverse proxy
// Currently disabled to prevent source address spoofing using X-Forward-For headers
// app.set('trust proxy', true);
// Save raw buffer for command parsing
function rawBufferSaver (req, res, buf, encoding) {
if (buf && buf.length) {
req.rawBody = buf.toString(encoding || 'utf8')
}
}
// Body parsers
app.use(bodyParser.raw({ type: 'text/plain', verify: rawBufferSaver }));
app.use(bodyParser.json({ type: 'application/json' }));
app.use(bodyParser.urlencoded({ extended: true }));
// Unique Request ID (HTTP Context)
app.use(context.middleware);
app.use(function(req, res, next) {
context.set('reqId', uuidv4());
var reqId = context.get('reqId');
next();
});
// Map to Main API router
app.use('/', apiRouter);
// Map to UI
app.use('/ui', express.static('ui'))
// Export app
module.exports = app;