|
| 1 | +import type { FastifyInstance } from 'fastify' |
| 2 | +import { PostgresMeta } from '../../../lib/index.js' |
| 3 | +import { DEFAULT_POOL_CONFIG } from '../../constants.js' |
| 4 | +import { extractRequestForLogging } from '../../utils.js' |
| 5 | +import { apply as applyPyTemplate } from '../../templates/python.js' |
| 6 | +import { getGeneratorMetadata } from '../../../lib/generators.js' |
| 7 | + |
| 8 | +export default async (fastify: FastifyInstance) => { |
| 9 | + fastify.get<{ |
| 10 | + Headers: { pg: string } |
| 11 | + Querystring: { |
| 12 | + excluded_schemas?: string |
| 13 | + included_schemas?: string |
| 14 | + } |
| 15 | + }>('/', async (request, reply) => { |
| 16 | + const connectionString = request.headers.pg |
| 17 | + const excludedSchemas = |
| 18 | + request.query.excluded_schemas?.split(',').map((schema) => schema.trim()) ?? [] |
| 19 | + const includedSchemas = |
| 20 | + request.query.included_schemas?.split(',').map((schema) => schema.trim()) ?? [] |
| 21 | + |
| 22 | + const pgMeta: PostgresMeta = new PostgresMeta({ ...DEFAULT_POOL_CONFIG, connectionString }) |
| 23 | + const { data: generatorMeta, error: generatorMetaError } = await getGeneratorMetadata(pgMeta, { |
| 24 | + includedSchemas, |
| 25 | + excludedSchemas, |
| 26 | + }) |
| 27 | + if (generatorMetaError) { |
| 28 | + request.log.error({ error: generatorMetaError, request: extractRequestForLogging(request) }) |
| 29 | + reply.code(500) |
| 30 | + return { error: generatorMetaError.message } |
| 31 | + } |
| 32 | + |
| 33 | + return applyPyTemplate(generatorMeta) |
| 34 | + }) |
| 35 | +} |
0 commit comments