import Fastify, { FastifyInstance } from 'fastify' import Database from 'better-sqlite3' import cookie from '@fastify/cookie' import type { Config } from './config.js' import { registerAuthRoutes } from './routes/authRoutes.js' declare module 'fastify' { interface FastifyInstance { db: Database.Database config: Config fetchImpl: typeof fetch } interface FastifyRequest { user?: import('./auth.js').UserRow } } export interface AppOptions { db: Database.Database config: Config fetchImpl?: typeof fetch } export async function buildApp(opts: AppOptions): Promise { const app = Fastify({ logger: false }) app.decorate('db', opts.db) app.decorate('config', opts.config) app.decorate('fetchImpl', opts.fetchImpl ?? fetch) await app.register(cookie) await registerAuthRoutes(app) app.get('/api/health', async () => ({ ok: true })) return app }