-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
63 lines (52 loc) · 1.59 KB
/
index.ts
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
const express = require('express')
const apicache = require('apicache');
const cache = apicache.middleware;
import Rcon, { RconConfig } from 'rcon-ts';
import Factorio from './factorio';
require('dotenv').config()
const dev = process.env.NODE_ENV !== 'production';
const PORT = process.env.PORT || 3000;
const DEFAULT_CACHE = "2 minutes";
const RCON_HOST = process.env.RCON_HOST || 'factorio.domain.tld';
const RCON_PORT = process.env.RCON_PORT || 27015;
const RCON_PASSWORD = process.env.RCON_PASSWORD || null;
const RCON_TIMEOUT = process.env.RCON_TIMEOUT || 5000;
// Fail early
if (!RCON_PASSWORD ) {
throw new Error('Missing RCON_PASSWORD, are you sure your env is set correctly?');
}
const config: RconConfig = {
host: RCON_HOST,
port: ~~RCON_PORT,
password: RCON_PASSWORD,
timeout: ~~RCON_TIMEOUT
}
// Factorio
const rcon = new Rcon(config);
rcon.enableConsoleLogging = true;
const factorio = new Factorio(rcon);
// Express
const app = express();
app.set('view engine', 'pug');
// Routes
app.get('/', cache(DEFAULT_CACHE), async (req: any, res: any) => {
let data : any = {};
await factorio.run();
if (factorio.rcon.errors && factorio.rcon.errors.length > 0) {
data = {
isUp: false
}
} else {
data = {
players: factorio.players,
seed: factorio.seed,
time: factorio.time,
evolution: factorio.evolution,
isUp: true
}
}
data.host = RCON_HOST
res.render('index', data);
});
app.listen(PORT, () => console.log('[+] express is running on ' + PORT));
module.exports = app