import type { FastifyPluginAsync } from 'fastify'; import fs from 'node:fs'; import { config } from '../config.js'; import { getDatabase } from '../db/connection.js'; function queryOne(sql: string, params: any[] = []): any | undefined { const db = getDatabase(); const stmt = db.prepare(sql); if (params.length) stmt.bind(params); let result: any; if (stmt.step()) { result = stmt.getAsObject(); } stmt.free(); return result; } export const healthRoutes: FastifyPluginAsync = async (app) => { app.get('/health', async () => { const storeCount = queryOne('SELECT COUNT(*) as count FROM stores')?.count ?? 0; const enabledCount = queryOne('SELECT COUNT(*) as count FROM stores WHERE enabled = 1')?.count ?? 0; let dbSizeBytes = 0; try { const stats = fs.statSync(config.databasePath); dbSizeBytes = stats.size; } catch { // DB file may not exist yet } return { status: 'ok', stores: { total: storeCount, enabled: enabledCount }, database: { sizeBytes: dbSizeBytes, sizeMB: Math.round(dbSizeBytes / 1024 / 1024 * 100) / 100 }, }; }); };