-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (36 loc) · 1.16 KB
/
server.js
File metadata and controls
42 lines (36 loc) · 1.16 KB
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
const path = require('path')
const fastify = require('fastify')({
logger: true
})
if (process.env.NODE_ENV !== 'production') {
fastify.register(require('fastify-cors'))
}
const fastifyStatic = require('fastify-static')
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'build'),
prefixAvoidTrailingSlash: true
})
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'photos'),
list: true,
decorateReply: false,
prefix: '/private/',
})
fastify.addHook('preHandler', (request, reply, done) => {
console.log('/' + request.headers['authorization'] + '/')
console.log('/' + process.env.PRIVATE_KEY + '/')
if (
request.url.includes('private') &&
!request.url.endsWith('.png') && !request.url.endsWith('.jpg') &&
request.headers['authorization'] !== process.env.PRIVATE_KEY
) {
reply.code(401).send({ code: '401', error: 'Unauthorized', message: 'You must authenticate' })
return reply
} else {
done()
}
})
fastify.listen(process.env.PORT || 5000, '0.0.0.0', (err, address) => {
if (err) throw err
fastify.log.info(`Server listening on ${address}`)
})