import 'dotenv/config' import Fastify from 'fastify' import cors from '@fastify/cors' import jwt from '@fastify/jwt' import multipart from '@fastify/multipart' import fastifyStatic from '@fastify/static' import path from 'node:path' import { registerAuth } from './plugins/auth.js' import { registerApiRoutes } from './routes/api.js' import { registerAuthRoutes } from './routes/auth.js' const port = Number(process.env.PORT) || 3333 const origin = (process.env.CORS_ORIGIN ?? '') .split(',') .map((s) => s.trim()) .filter(Boolean) const fastify = Fastify({ logger: true }) await fastify.register(cors, { origin: origin.length ? origin : true, credentials: true, }) await fastify.register(jwt, { secret: process.env.JWT_SECRET || 'dev-jwt-secret-change-me', }) await fastify.register(multipart, { limits: { files: 10, fileSize: 10 * 1024 * 1024, }, }) const uploadsDir = path.join(process.cwd(), 'uploads') await fastify.register(fastifyStatic, { root: uploadsDir, prefix: '/uploads/', }) fastify.decorate('authenticate', async function authenticate(request, reply) { try { await request.jwtVerify() } catch { return reply.code(401).send({ error: 'Не авторизован' }) } }) registerAuth(fastify) await registerAuthRoutes(fastify) await registerApiRoutes(fastify) fastify.get('/health', async () => ({ ok: true })) try { await fastify.listen({ port, host: '0.0.0.0' }) } catch (err) { fastify.log.error(err) process.exit(1) }