Files
discordeno/docker-apps/rest-passthrough/src/fastify.ts
Fleny 919474069d chore: Migrate ESLint and prettier to Biome (#3634)
* Migrate eslint and prettier to biomejs

This does NOT include examples/bigbot as it has its own formatter

* Update to biome 1.8.0

* Readd dotenv dev dependency to rest

During a merge it got lost
2024-07-13 13:05:02 -05:00

53 lines
1.2 KiB
TypeScript

import fastifyEnv from '@fastify/env'
import fastifyHelmet from '@fastify/helmet'
import fastifyMultipart from '@fastify/multipart'
import fastify, { type FastifyInstance } from 'fastify'
export const buildFastifyApp = async (): Promise<FastifyInstance> => {
const app = await fastify()
await app.register(fastifyEnv, {
schema: {
type: 'object',
properties: {
HOST: {
type: 'string',
default: 'localhost',
},
DISCORD_TOKEN: {
type: 'string',
minLength: 1,
},
AUTHORIZATION_TOKEN: {
type: 'string',
minLength: 1,
},
},
required: ['DISCORD_TOKEN', 'AUTHORIZATION_TOKEN'],
},
})
await app.register(fastifyHelmet)
app.register(fastifyMultipart, { attachFieldsToBody: true })
app.addHook('onRequest', async (request, reply) => {
if (request.headers.authorization !== request.server.config.AUTHORIZATION_TOKEN) {
reply.status(401).send({
message: 'Credentials not valid.',
})
}
})
return app
}
declare module 'fastify' {
interface FastifyInstance {
config: {
HOST: string
DISCORD_TOKEN: string
AUTHORIZATION_TOKEN: string
}
}
}