пва
This commit is contained in:
+40
-1
@@ -19,13 +19,14 @@ import { prisma } from './lib/prisma.js'
|
||||
import { getMaxUploadBodyBytes, getProductImageMaxFileBytes } from './lib/upload-limits.js'
|
||||
import { registerAuth } from './plugins/auth.js'
|
||||
import { registerIpGate } from './plugins/ip-gate.js'
|
||||
import { registerSecurityHeaders } from './plugins/security-headers.js'
|
||||
import { registerApiRoutes } from './routes/api.js'
|
||||
import { registerOAuthSocialRoutes } from './routes/oauth-social.js'
|
||||
import { registerSseRoutes } from './routes/sse.js'
|
||||
import { registerUploadsResized } from './routes/uploads-resized.js'
|
||||
import { registerUserNotificationRoutes } from './routes/user/notifications.js'
|
||||
import { registerUserAddressRoutes } from './routes/user-addresses.js'
|
||||
import { registerUserCartRoutes } from './routes/user-cart.js'
|
||||
import { registerSseRoutes } from './routes/sse.js'
|
||||
import { registerUserMessageRoutes } from './routes/user-messages.js'
|
||||
import { registerUserOrderRoutes } from './routes/user-orders.js'
|
||||
import { registerUserPaymentRoutes } from './routes/user-payments.js'
|
||||
@@ -48,6 +49,44 @@ await fastify.register(cors, {
|
||||
credentials: true,
|
||||
})
|
||||
|
||||
await registerSecurityHeaders(fastify)
|
||||
|
||||
fastify.get('/health', async () => {
|
||||
try {
|
||||
await prisma.$queryRaw`SELECT 1`
|
||||
return { status: 'ok', database: 'connected', uptime: process.uptime() }
|
||||
} catch {
|
||||
return { status: 'degraded', database: 'disconnected', uptime: process.uptime() }
|
||||
}
|
||||
})
|
||||
|
||||
fastify.setErrorHandler(function errorHandler(error, request, reply) {
|
||||
const isProd = process.env.NODE_ENV === 'production'
|
||||
|
||||
if (error.validation) {
|
||||
return reply.code(400).send({
|
||||
error: 'Ошибка валидации',
|
||||
details: isProd ? undefined : error.validation,
|
||||
})
|
||||
}
|
||||
|
||||
if (error.code === 'FST_ERR_VALIDATION') {
|
||||
return reply.code(400).send({ error: 'Неверный формат запроса' })
|
||||
}
|
||||
|
||||
if (error.statusCode) {
|
||||
return reply.code(error.statusCode).send({
|
||||
error: error.message || 'Произошла ошибка',
|
||||
})
|
||||
}
|
||||
|
||||
request.log.error(error)
|
||||
|
||||
return reply.code(500).send({
|
||||
error: isProd ? 'Внутренняя ошибка сервера' : error.message,
|
||||
})
|
||||
})
|
||||
|
||||
await fastify.register(jwt, {
|
||||
secret: process.env.JWT_SECRET || 'dev-jwt-secret-change-me',
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user