import Fastify from "fastify"; import fastifySwagger from "@fastify/swagger"; import { config } from "./config.js"; import { client } from "./db/index.js"; import { errorHandler } from "./plugins/error-handler.js"; import { authenticate } from "./plugins/authenticate.js"; import { accountContext } from "./plugins/account-context.js"; import scalarDocs from "./plugins/scalar-docs.js"; import { registerRoutes } from "./routes/index.js"; export function buildApp() { const app = Fastify({ logger: true, }); app.addHook("onClose", async () => { await client.end(); }); app.register(fastifySwagger, { openapi: { openapi: "3.0.0", info: { title: "Eyrun API", description: "Authentication and account management API", version: "1.0.0", }, servers: config.API_URL ? [{ url: config.API_URL, description: "Production server" }] : [{ url: "http://localhost:3000", description: "Development server" }], components: { securitySchemes: { bearerAuth: { type: "http", scheme: "bearer", bearerFormat: "JWT", }, }, }, }, }); app.register(scalarDocs); app.register(errorHandler); app.register(authenticate); app.register(accountContext); app.register(registerRoutes); return app; }