вложения, канальчики, бим-бим + бам-бам
This commit is contained in:
@@ -1,19 +1,80 @@
|
||||
import type { TypeBoxTypeProvider } from '@fastify/type-provider-typebox'
|
||||
import { dirname, join } from 'node:path'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import FastifyAutoLoad from '@fastify/autoload'
|
||||
import FastifyCookie from '@fastify/cookie'
|
||||
import FastifyCors from '@fastify/cors'
|
||||
import FastifyMultipart from '@fastify/multipart'
|
||||
import FastifySensible from '@fastify/sensible'
|
||||
import FastifySwagger from '@fastify/swagger'
|
||||
import FastifyApiReference from '@scalar/fastify-api-reference'
|
||||
import Fastify from 'fastify'
|
||||
import prisma from './prisma/client.ts'
|
||||
|
||||
console.log(process.env.DATABASE_URL)
|
||||
import { Prisma } from './prisma/generated-client/client.ts'
|
||||
import { ErrorReplySchema } from './schemas/common.ts'
|
||||
import 'dotenv/config'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = dirname(__filename)
|
||||
|
||||
const fastify = Fastify({
|
||||
logger: true,
|
||||
}).withTypeProvider<TypeBoxTypeProvider>()
|
||||
|
||||
fastify.register(FastifySensible)
|
||||
|
||||
fastify.setErrorHandler((error, request, reply) => {
|
||||
if (error instanceof Prisma.PrismaClientValidationError) {
|
||||
reply.notAcceptable()
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (error.statusCode) {
|
||||
reply.getHttpError(error.statusCode, error.message)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
reply.badRequest(error.message)
|
||||
})
|
||||
|
||||
fastify.register(FastifySwagger, {
|
||||
openapi: {
|
||||
info: {
|
||||
version: '1.0.0',
|
||||
title: 'Chad API',
|
||||
},
|
||||
},
|
||||
transform: ({ schema, url }) => {
|
||||
if (!url.startsWith('/chad'))
|
||||
return { schema, url }
|
||||
|
||||
const transformedSchema: typeof schema = schema ?? {}
|
||||
const responseSchema: any = transformedSchema.response ?? {}
|
||||
|
||||
responseSchema['4xx'] ??= ErrorReplySchema
|
||||
responseSchema['5xx'] ??= ErrorReplySchema
|
||||
|
||||
transformedSchema.response = responseSchema
|
||||
|
||||
return { schema: transformedSchema, url }
|
||||
},
|
||||
})
|
||||
|
||||
fastify.register(FastifyApiReference, {
|
||||
routePrefix: '/reference',
|
||||
configuration: {
|
||||
showOperationId: true,
|
||||
showDeveloperTools: 'never',
|
||||
pageTitle: 'Chad API',
|
||||
customCss: `
|
||||
.scalar-mcp-layer,
|
||||
.agent-button-container,
|
||||
.t-doc__sidebar > div > button:last-child {
|
||||
display: none !important;
|
||||
}
|
||||
`,
|
||||
},
|
||||
})
|
||||
|
||||
fastify.register(FastifyCors, {
|
||||
@@ -44,9 +105,6 @@ fastify.register(FastifyAutoLoad, {
|
||||
|
||||
try {
|
||||
await fastify.listen({ port, host: '0.0.0.0' })
|
||||
|
||||
await prisma.$connect()
|
||||
fastify.log.info('Testing DB Connection. OK')
|
||||
}
|
||||
catch (err) {
|
||||
fastify.log.error(err)
|
||||
|
||||
Reference in New Issue
Block a user