-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
98 lines (80 loc) · 2.77 KB
/
index.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
const bodyParser = require('body-parser');
const cors = require('cors');
const dgram = require('dgram');
const express = require('express');
const mongoose = require('mongoose');
const morgan = require('morgan');
const path = require('path');
// XML Parsing Support for body-parser
require('body-parser-xml')(bodyParser);
const buildAssets = require('./controllers/common/build_assets');
const errorLogger = require('./controllers/common/error_logger');
const app = express();
const udpApp = dgram.createSocket('udp4');
const mongoOptions = {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
};
// View Rendering Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '50mb' }));
app.use(bodyParser.xml({
verify: (req, res, buf, encoding) => {
if (buf && buf.length) req.rawBody = buf.toString(encoding || 'utf8');
},
}));
// Route Access Details
if (process.env.NODE_ENV === 'development') app.use(morgan('dev'));
// Cross Origin Policy
app.use(cors());
// Actual Routes
const engineController = require('./controllers/engine');
app.use('/', engineController); // For In-Game Server Access
app.use('//', engineController); // For Launcher Access
// Testing Routes Only
app.use('/runner', require('./controllers/runner'));
// Error Handler (To-Do)
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
// Log the errors to a file
const {
method,
originalUrl,
ip,
body,
headers,
} = req;
// eslint-disable-next-line max-len
errorLogger.error(`${new Date().toISOString()} | ${method} - ${originalUrl} | IP: ${ip} | Error: ${err.message} | Body: ${JSON.stringify(body)} | Headers: ${JSON.stringify(headers)}`);
// Send a response
if (!res.headersSent) {
res.status(500).send('');
}
});
// UDP Server Settings
udpApp.on('error', (err) => {
console.log(`server error:\n${err.stack}`);
udpApp.close();
});
udpApp.on('message', (msg, rinfo) => {
console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
udpApp.on('listening', () => {
const address = udpApp.address();
console.log(new Date(), `UDP server running on ${address.port}`);
});
mongoose.connect(process.env.MONGO_URI, mongoOptions, (err) => {
if (err) console.log(new Date(), 'DB Connection Error', err.message);
else {
console.log(new Date(), 'Database connection successful');
buildAssets();
}
});
if (process.env.ENABLE_UDP) udpApp.bind(process.env.PORT);
app.listen(process.env.PORT, () => {
console.log(new Date(), `API Server running on ${process.env.PORT}`);
});
module.exports = app;