-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
74 lines (63 loc) · 1.97 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
import dotenv from 'dotenv';
import express from 'express';
import { join } from 'path';
import rateLimiter from 'express-rate-limit';
import https from 'https';
import fs from 'fs';
import download from './src/routes/download';
import search from './src/routes/search';
import ready from './src/routes/ready';
import build from './src/routes/build';
import checkServices from './src/utils/check';
import { ERRORS, RATE_LIMIT, HTTP_STATUS } from './src/config/constants';
import errorHandler from './src/middleware/error-handler';
dotenv.config();
const port = process.env.PORT || 3000;
const limiter = rateLimiter(RATE_LIMIT);
const app = express();
app.set('views', join(__dirname, '/views'));
app.set('view engine', 'pug');
app.use((req, res, next) => {
if (process.env.NODE_ENV !== 'production') return next();
if (req.secure) {
return next();
}
res.redirect(join('https://', req.hostname, req.url));
return null;
});
app.use(limiter);
app.use('/public', express.static(join(__dirname, '/views/public')));
app.use(express.json());
app.use(ready);
app.use(build);
app.use(download);
app.use(search);
app.get('/', (req, res) => {
res.render('game-search');
});
app.use((req, res) => {
res.status(HTTP_STATUS.NOT_FOUND.code).render('error-page', HTTP_STATUS.NOT_FOUND);
});
app.use(errorHandler);
checkServices()
.then(() => {
app.listen(port, () => {
console.log(`Web server is listening at port ${port}.`);
});
})
.catch((err) => {
if (err.message === ERRORS.DATABASE_ERROR) {
process.exit(0);
}
if (err.message === ERRORS.STEAMCMD_ERROR) {
process.exit(-1);
}
});
const { key, cert } = {
key: fs.readFileSync(`${process.env.CERTPATH}/privkey.pem`),
cert: fs.readFileSync(`${process.env.CERTPATH}/fullchain.pem`),
};
if (process.env.NODE_ENV === 'production') {
const httpsServer = https.createServer({ key, cert }, app);
httpsServer.listen(443);
}