const { listLogChannels, getLogTail } = require('../../../infrastructure/log-files'); async function handleLogsRoutes(req, res) { const urlPath = req.urlPath; const method = req.method; if (method === 'GET' && urlPath === '/api/logs') { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ channels: listLogChannels() })); return true; } const match = urlPath.match(/^\/api\/logs\/([a-zA-Z]+)$/); if (method === 'GET' && match) { const channelId = match[1]; const channels = listLogChannels(); if (!channels.some((c) => c.id === channelId)) { res.writeHead(404, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: 'Unknown log channel' })); return true; } const url = new URL(req.url, `https://${req.headers.host}`); const lines = Math.min(parseInt(url.searchParams.get('lines') || '500', 10), 5000); const tail = getLogTail(channelId, lines); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ channel: channelId, lines: tail })); return true; } return false; } module.exports = { handleLogsRoutes };