-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (50 loc) · 1.14 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
if (
process.env.DD_SITE &&
process.env.DD_API_KEY) {
const tracer = require('dd-trace').init({
logInjection: true
})
exports.tracer = tracer
}
const fastify = require('fastify')({
logger: true
})
const redirectChain = require('redirect-chain')({
maxRedirects: 5
})
fastify.get('/', async (request, reply) => {
return { hello: 'world' }
})
fastify.get('/redirect-chain', {
schema: {
querystring: {
type: 'object',
properties: {
get: {
type: 'string',
format: 'uri'
},
},
required: ['get']
}
}
}, async (request, reply) => {
const { get } = request.query
// This library generates HEAD requests
// https://github.com/tractr/redirect-chain/blob/master/index.js
const finalUrl = await redirectChain.destination(get)
return { finalUrl }
})
/**
* Run the server!
*/
const start = async () => {
try {
console.log(`starting server on ${process.env.PORT}`)
await fastify.listen({ port: process.env.PORT || 3000, host: '0.0.0.0' })
} catch (err) {
fastify.log.error(err)
process.exit(1)
}
}
start()