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 17:07:04 +02:00
|
|
|
import { SerialQueue } from './queue.js'
|
2026-08-29 15:20:42 +02:00
|
|
|
import { registerAuthRoutes } from './routes/authRoutes.js'
|
2026-08-29 15:51:58 +02:00
|
|
|
import { registerSettingsRoutes } from './routes/settingsRoutes.js'
|
2026-08-29 17:07:04 +02:00
|
|
|
import { registerLookupRoutes } from './routes/lookupRoutes.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 17:07:04 +02:00
|
|
|
discogsQueue: SerialQueue
|
2026-08-29 14:43:44 +02:00
|
|
|
}
|
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 17:07:04 +02:00
|
|
|
app.decorate('discogsQueue', new SerialQueue({ minIntervalMs: 1100 }))
|
2026-08-29 15:20:42 +02:00
|
|
|
|
|
|
|
|
await app.register(cookie)
|
|
|
|
|
await registerAuthRoutes(app)
|
2026-08-29 15:51:58 +02:00
|
|
|
await registerSettingsRoutes(app)
|
2026-08-29 17:07:04 +02:00
|
|
|
await registerLookupRoutes(app)
|
2026-08-29 15:20:42 +02:00
|
|
|
|
2026-08-29 14:43:44 +02:00
|
|
|
app.get('/api/health', async () => ({ ok: true }))
|
|
|
|
|
return app
|
|
|
|
|
}
|