-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
72 lines (61 loc) · 2.08 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
require('dotenv').config();
const express = require('express');
const { createServer } = require('http');
const readline = require('readline');
const path = require('path');
const { handleAoe4Match, handleAoe4Rank, handleAoe4WinRate } = require('./aoe4/handlers');
const {
PORT = 5000,
} = process.env;
function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}
function wrapError(handler) {
var hdl = handler;
return (req, res) => {
hdl(req, res).catch(err => {
console.log(err.stack);
res.status(500).send('Internal error');
});
}
}
function logRequest(req, res, next)
{
if (req.url != '/') {
const nightbotUser = new URLSearchParams(req.headers['nightbot-user'] || '').get('displayName');
const nightbotChannel = new URLSearchParams(req.headers['nightbot-channel'] || '').get('displayName');
const by = nightbotChannel ? ` [by ${nightbotUser}@${nightbotChannel}]` : '';
const startTime = performance.now();
console.log(`Api Req: ${req.url}${by}`);
res.once('finish', () => {
const endTime = performance.now();
const elapsed = endTime - startTime;
console.log(`Api Res: ${req.url} (${res.statusCode}, ${elapsed.toFixed(0)} msec)`);
});
}
next();
}
async function run() {
const app = express()
.use(express.static(path.join(__dirname, 'public')))
.use(express.json())
.use(logRequest)
.set('views', path.join(__dirname, 'views'))
.set('view engine', 'ejs')
.get('/api/aoe4/match', wrapError(handleAoe4Match))
.get('/api/aoe4/rank', wrapError(handleAoe4Rank))
.get('/api/aoe4/winrate', wrapError(handleAoe4WinRate))
.get('/', (req, res) => res.render('pages/index'));
const serverExpress = createServer(app);
await new Promise((resolve, reject) => { serverExpress.listen(PORT, () => resolve()); });
console.log(`Listening on ${ PORT }`);
await askQuestion('Press any key to close\n');
}
run().then(process.exit);