import type { FastifyInstance } from 'fastify'; import { getSystemMetrics, getDockerStats, dockerCleanup } from './repository.js'; import { DockerCleanupParamsSchema } from './types.js'; import { requireAdmin } from '../../lib/auth.js'; export async function systemRoutes(fastify: FastifyInstance) { // Get system metrics (admin only) fastify.get('/system/metrics', { preHandler: async (req) => requireAdmin(req), }, async (req, reply) => { try { const metrics = await getSystemMetrics(); return reply.send(metrics); } catch (error) { fastify.log.error('Failed to get system metrics:', error); return reply.code(500).send({ error: 'Failed to get system metrics' }); } }); // Get Docker stats (admin only) fastify.get('/docker/stats', { preHandler: async (req) => requireAdmin(req), }, async (req, reply) => { try { const stats = await getDockerStats(); return reply.send(stats); } catch (error) { fastify.log.error('Failed to get Docker stats:', error); return reply.code(500).send({ error: 'Failed to get Docker stats' }); } }); // Docker cleanup (admin only) fastify.post('/docker/cleanup', { preHandler: async (req) => requireAdmin(req), }, async (req, reply) => { try { const params = DockerCleanupParamsSchema.parse(req.body); const result = await dockerCleanup(params.type, params.force); return reply.send(result); } catch (error: any) { fastify.log.error('Docker cleanup failed:', error); return reply.code(500).send({ error: error.message || 'Docker cleanup failed' }); } }); }