(roomManager: IRoomManager, logger: Logger)
| 58 | * @returns HTTP request handler function |
| 59 | */ |
| 60 | export function createHttpHandler(roomManager: IRoomManager, logger: Logger) { |
| 61 | return async (req: IncomingMessage, res: ServerResponse) => { |
| 62 | // Health check doesn't require auth |
| 63 | if (req.method === 'GET' && req.url === '/health') { |
| 64 | try { |
| 65 | const connections = await roomManager.getTotalActiveConnections() |
| 66 | res.writeHead(200, { 'Content-Type': 'application/json' }) |
| 67 | res.end( |
| 68 | JSON.stringify({ |
| 69 | status: 'ok', |
| 70 | timestamp: new Date().toISOString(), |
| 71 | connections, |
| 72 | }) |
| 73 | ) |
| 74 | } catch (error) { |
| 75 | logger.error('Error in health check:', error) |
| 76 | res.writeHead(503, { 'Content-Type': 'application/json' }) |
| 77 | res.end(JSON.stringify({ status: 'error', message: 'Health check failed' })) |
| 78 | } |
| 79 | return |
| 80 | } |
| 81 | |
| 82 | // All POST endpoints require internal API key authentication |
| 83 | if (req.method === 'POST') { |
| 84 | const authResult = checkInternalApiKey(req) |
| 85 | if (!authResult.success) { |
| 86 | res.writeHead(401, { 'Content-Type': 'application/json' }) |
| 87 | res.end(JSON.stringify({ error: authResult.error })) |
| 88 | return |
| 89 | } |
| 90 | |
| 91 | if (!roomManager.isReady()) { |
| 92 | sendError(res, 'Room manager unavailable', 503) |
| 93 | return |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // Handle workflow deletion notifications from the main API |
| 98 | if (req.method === 'POST' && req.url === '/api/workflow-deleted') { |
| 99 | try { |
| 100 | const body = await readRequestBody(req) |
| 101 | const { workflowId } = JSON.parse(body) |
| 102 | await roomManager.handleWorkflowDeletion(workflowId) |
| 103 | sendSuccess(res) |
| 104 | } catch (error) { |
| 105 | logger.error('Error handling workflow deletion notification:', error) |
| 106 | sendError(res, 'Failed to process deletion notification') |
| 107 | } |
| 108 | return |
| 109 | } |
| 110 | |
| 111 | // Handle workflow update notifications from the main API |
| 112 | if (req.method === 'POST' && req.url === '/api/workflow-updated') { |
| 113 | try { |
| 114 | const body = await readRequestBody(req) |
| 115 | const { workflowId } = JSON.parse(body) |
| 116 | await roomManager.handleWorkflowUpdate(workflowId) |
| 117 | sendSuccess(res) |
no test coverage detected