EvalueTonSavoir/nginx/njs/main.js
MathieuSevignyLavallee 5c21b6a15f optimize and cleanup
2024-11-15 19:35:41 -05:00

54 lines
No EOL
1.6 KiB
JavaScript

async function fetchRoomInfo(r) {
try {
// Make request to API to get room info
let res = await r.subrequest('/api/room/' + r.variables.room_id, {
method: 'GET'
});
if (res.status !== 200) {
r.error(`Failed to fetch room info: ${res.status}`);
return null;
}
let room = JSON.parse(res.responseText);
r.error(`Debug: Room info: ${JSON.stringify(room)}`); // Debug log
return room;
} catch (error) {
r.error(`Error fetching room info: ${error}`);
return null;
}
}
async function routeWebSocket(r) {
try {
const roomInfo = await fetchRoomInfo(r);
if (!roomInfo || !roomInfo.host) {
r.error(`Debug: Invalid room info: ${JSON.stringify(roomInfo)}`);
r.return(404, 'Room not found or invalid');
return;
}
// Make sure the host includes protocol if not already present
let proxyUrl = roomInfo.host;
if (!proxyUrl.startsWith('http://') && !proxyUrl.startsWith('https://')) {
proxyUrl = 'http://' + proxyUrl;
}
r.error(`Debug: Original URL: ${r.uri}`);
r.error(`Debug: Setting proxy target to: ${proxyUrl}`);
r.error(`Debug: Headers: ${JSON.stringify(r.headersIn)}`);
// Set the proxy target variable
r.variables.proxy_target = proxyUrl;
// Redirect to the websocket proxy
r.internalRedirect('@websocket_proxy');
} catch (error) {
r.error(`WebSocket routing error: ${error}`);
r.return(500, 'Internal routing error');
}
}
export default { routeWebSocket };