-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
Copy pathapp.ts
52 lines (43 loc) · 1.5 KB
/
app.ts
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
import cors from '@fastify/cors'
import swagger from '@fastify/swagger'
import { fastify, FastifyInstance, FastifyServerOptions } from 'fastify'
import { PG_META_REQ_HEADER } from './constants.js'
import routes from './routes/index.js'
import { extractRequestForLogging } from './utils.js'
// Pseudo package declared only for this module
import pkg from '#package.json' with { type: 'json' }
export const build = (opts: FastifyServerOptions = {}): FastifyInstance => {
const app = fastify({ disableRequestLogging: true, requestIdHeader: PG_META_REQ_HEADER, ...opts })
app.setErrorHandler((error, request, reply) => {
app.log.error({ error: error.toString(), request: extractRequestForLogging(request) })
reply.code(500).send({ error: error.message })
})
app.setNotFoundHandler((request, reply) => {
app.log.error({ error: 'Not found', request: extractRequestForLogging(request) })
reply.code(404).send({ error: 'Not found' })
})
app.register(swagger, {
openapi: {
servers: [],
info: {
title: 'postgres-meta',
description: 'A REST API to manage your Postgres database',
version: pkg.version,
},
},
})
app.register(cors)
app.get('/', async (_request, _reply) => {
return {
status: 200,
name: pkg.name,
version: pkg.version,
documentation: 'https://github.com/supabase/postgres-meta',
}
})
app.get('/health', async (_request, _reply) => {
return { date: new Date() }
})
app.register(routes)
return app
}