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