27 lines
677 B
TypeScript
27 lines
677 B
TypeScript
|
|
import Fastify, { FastifyInstance } from 'fastify'
|
||
|
|
import Database from 'better-sqlite3'
|
||
|
|
import type { Config } from './config.js'
|
||
|
|
|
||
|
|
declare module 'fastify' {
|
||
|
|
interface FastifyInstance {
|
||
|
|
db: Database.Database
|
||
|
|
config: Config
|
||
|
|
fetchImpl: typeof fetch
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
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)
|
||
|
|
app.get('/api/health', async () => ({ ok: true }))
|
||
|
|
return app
|
||
|
|
}
|