import "dotenv/config"; import Fastify, { type FastifyRequest, type FastifyReply } from "fastify"; import cors from "@fastify/cors"; import jwt from "@fastify/jwt"; import rateLimit from "@fastify/rate-limit"; import { healthRoutes } from "./routes/health.js"; import { authRoutes } from "./routes/auth.js"; const app = Fastify({ logger: true }); await app.register(cors, { origin: true }); await app.register(rateLimit, { max: 100, timeWindow: "1 minute" }); await app.register(jwt, { secret: process.env.JWT_SECRET! }); app.decorate("authenticate", async (request: FastifyRequest, reply: FastifyReply) => { try { await request.jwtVerify(); } catch { reply.code(401).send({ error: "Unauthorized" }); } }); await app.register(healthRoutes); await app.register(authRoutes); const port = Number(process.env.PORT ?? 4000); app.listen({ port, host: "0.0.0.0" }).catch((err) => { app.log.error(err); process.exit(1); });