forked from EdenServer/eden-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
72 lines (62 loc) · 2.2 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
64
65
66
67
68
69
70
71
72
import 'lightenv';
import bodyParser from 'body-parser';
import express from 'express';
import mysql from 'mysql2';
import path from 'path';
import Cache from './api/v1/utils/cache';
import Pattern from './api/v1/utils/pattern';
import preparedStatement from './api/v1/utils/db';
import { loadItems, refreshOwnersCache } from './api/v1/utils/items';
import { refreshTitleCache } from './api/v1/utils/chars';
import api from './api';
import rateLimit from 'express-rate-limit';
const port = process.env.PORT || 8081;
const app = express();
const limiter = rateLimit({
windowMs: 10000, // 10 seconds
max: 100, // Limit each IP to 100 requests per `window`
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
});
app.use(limiter);
app.set('trust proxy', 1);
app.get('/ip', (request, response) => response.send(request.ip));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/api', api);
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
app.locals.cache = new Cache({ interval: 120000 }); // 2 minutes
app.locals.pattern = new Pattern();
app.locals.db = mysql.createPool({
host: process.env.MYSQLHOST,
user: process.env.MYSQLUSER,
password: process.env.MYSQLPASS,
database: process.env.MYSQLDB,
port: process.env.MYSQLPORT ? parseInt(process.env.MYSQLPORT) : 3306,
waitForConnections: true,
connectionLimit: 10,
});
app.locals.query = preparedStatement(app.locals.db);
(async () => {
try {
app.locals.items = await loadItems(app.locals.query);
// Setup cached results and refreshing of them
await refreshOwnersCache(app.locals.query);
setInterval(
async () => await refreshOwnersCache(app.locals.query),
86400000 // Once every day
);
await refreshTitleCache(app.locals.query);
setInterval(
async () => await refreshTitleCache(app.locals.query),
14400000 // Once every 4 hours
);
// eslint-disable-next-line no-console
app.listen(port, () => console.log(`Listening on port ${port}...`));
} catch (error) {
// eslint-disable-next-line no-console
console.error(error);
}
})();