-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
54 lines (44 loc) · 1.36 KB
/
index.mjs
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
import 'dotenv/config'
import express from 'express'
import log from './log.mjs'
import { determineEndpoint, determineModifier } from './web.mjs'
import { randomQueueEndpoint } from './queue.mjs'
const app = express()
// disable x-powered-by header
app.disable('x-powered-by');
// this is required so that the ip forwarded from Caddy is trusted
app.set('trust proxy', (ip) => {
if (ip === '127.0.0.1') return true // only localhost
else return false
})
// logging >w<
app.use(async (req, res, next) => {
log.warn(`${req.method}:${req.url} ${res.statusCode}`)
next()
})
app.get('/help', async (req, res) => {
res.redirect(process.env.REDIRECT_URL)
})
app.get(/^\/404(?:\/.*)?$/, async (req, res, next) => {
res.status(404).end()
})
app.get('/', async (req, res, next) => {
res.redirect(process.env.REDIRECT_URL)
})
// matches "/" followed by endpoint followed by optional modifiers
app.get(/^\/(\w+)(?:\/(\w+))?(?:\/(\w+))?(?:\/.*)?$/, async (req, res, next) => {
await determineEndpoint(req, res, next)
})
// matches "/" followed by modifers
/*
app.get(/^\/(\w+)?(?:\/(\w+))?(?:\/.*)?$/, async (req, res, next) => {
await determineModifier(req, res, next, 'random')
})
*/
app.use(async (req, res, next) => {
res.status(404).end()
})
app.listen(process.env.PORT, async () => {
log.log(`API listening on port ${process.env.PORT}!`)
randomQueueEndpoint()
})