-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
54 lines (41 loc) · 1.62 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
require('dotenv').config();
const process = require('process');
const { connect } = require('@unyxos/working-rcon');
const validator = require('express-joi-validation').createValidator({});
const express = require('express');
const app = express();
const { metricsParamsSchema } = require('./utils/joi-schema');
const logger = require('./utils/logging');
const games = {
csgo: require('./games/csgo'),
gmod: require('./games/gmod'),
css: require('./games/css'),
hl2: require('./games/hl2'),
tf2: require('./games/tf2'),
l4d2: require('./games/l4d2'),
};
app.get('/', (req, res) => {
res.sendFile(__dirname + '/utils/homepage.html');
});
app.get('/metrics', validator.query(metricsParamsSchema), async (req, res) => {
const { ip, port, password, game } = req.query;
try {
const client = await connect(ip, port, password, 5 * 1000);
const status = await client.command('status');
const stats = await client.command('stats');
await client.disconnect();
const response = games[game].setMetrics({ stats, status }, { ip, port, game });
res.end(response);
} catch (err) {
logger.error({ step: 'FETCH_METRICS', err: err.message }, 'error while fetching metrics from server');
const response = games[game].setNoMetrics({ ip, port, game });
res.end(response);
}
});
const port = process.env.HTTP_PORT || 9591;
app.listen(port, () => {
logger.info(`Metrics server listening on port ${port}`);
});
process.on('uncaughtException', (err) => {
logger.error({ step: 'UNCAUGHT_EXCEPTION', err: err.message }, 'uncaught exception');
});