|
|
|
|
|
import { Request, Response } from 'express'; |
|
|
import logService from '../services/logService.js'; |
|
|
|
|
|
|
|
|
export const getAllLogs = (req: Request, res: Response): void => { |
|
|
try { |
|
|
const logs = logService.getLogs(); |
|
|
res.json({ success: true, data: logs }); |
|
|
} catch (error) { |
|
|
console.error('Error getting logs:', error); |
|
|
res.status(500).json({ success: false, error: 'Error getting logs' }); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
export const clearLogs = (req: Request, res: Response): void => { |
|
|
try { |
|
|
logService.clearLogs(); |
|
|
res.json({ success: true, message: 'Logs cleared successfully' }); |
|
|
} catch (error) { |
|
|
console.error('Error clearing logs:', error); |
|
|
res.status(500).json({ success: false, error: 'Error clearing logs' }); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
export const streamLogs = (req: Request, res: Response): void => { |
|
|
try { |
|
|
|
|
|
res.writeHead(200, { |
|
|
'Content-Type': 'text/event-stream', |
|
|
'Cache-Control': 'no-cache', |
|
|
'Connection': 'keep-alive' |
|
|
}); |
|
|
|
|
|
|
|
|
const logs = logService.getLogs(); |
|
|
res.write(`data: ${JSON.stringify({ type: 'initial', logs })}\n\n`); |
|
|
|
|
|
|
|
|
const unsubscribe = logService.subscribe((log) => { |
|
|
res.write(`data: ${JSON.stringify({ type: 'log', log })}\n\n`); |
|
|
}); |
|
|
|
|
|
|
|
|
req.on('close', () => { |
|
|
unsubscribe(); |
|
|
console.log('Client disconnected from log stream'); |
|
|
}); |
|
|
} catch (error) { |
|
|
console.error('Error streaming logs:', error); |
|
|
res.status(500).json({ success: false, error: 'Error streaming logs' }); |
|
|
} |
|
|
}; |