diff --git a/nginx/conf.d/default.conf b/nginx/conf.d/default.conf index 509a0a4..fd1bc38 100644 --- a/nginx/conf.d/default.conf +++ b/nginx/conf.d/default.conf @@ -1,4 +1,6 @@ +js_shared_dict_zone zone=cache:10m; js_import njs/main.js; +js_set $cache_dict main.get_cache_dict; map $http_upgrade $connection_upgrade { default upgrade; diff --git a/nginx/njs/main.js b/nginx/njs/main.js index d824272..b5f7b4d 100644 --- a/nginx/njs/main.js +++ b/nginx/njs/main.js @@ -1,6 +1,50 @@ -async function fetchRoomInfo(r) { +function get_cache_dict(r) { + return ''; +} + +function getCachedData(r, key) { try { - // Make request to API to get room info + const cached = ngx.shared.cache.get(key); + if (cached) { + const data = JSON.parse(cached); + const now = Date.now(); + // 2 minutes cache - let game rooms rotate + if (now - data.timestamp < 120000) { + r.error(`Debug: Cache hit for ${key}, age: ${(now - data.timestamp)/1000}s`); + return data.value; + } + r.error(`Debug: Cache expired for ${key}, age: ${(now - data.timestamp)/1000}s`); + } + return null; + } catch (error) { + r.error(`Cache read error: ${error}`); + return null; + } +} + +function setCachedData(r, key, value) { + try { + const data = { + timestamp: Date.now(), + value: value + }; + ngx.shared.cache.set(key, JSON.stringify(data)); + r.error(`Debug: Cached ${key}`); + } catch (error) { + r.error(`Cache write error: ${error}`); + } +} + +async function fetchRoomInfo(r) { + const cacheKey = `room:${r.variables.room_id}`; + + try { + const cachedRoom = getCachedData(r, cacheKey); + if (cachedRoom) { + r.error(`Debug: Room info from cache: ${JSON.stringify(cachedRoom)}`); + return cachedRoom; + } + let res = await r.subrequest('/api/room/' + r.variables.room_id, { method: 'GET' }); @@ -11,44 +55,43 @@ async function fetchRoomInfo(r) { } let room = JSON.parse(res.responseText); - r.error(`Debug: Room info: ${JSON.stringify(room)}`); // Debug log + setCachedData(r, cacheKey, room); + r.error(`Debug: Room info fetched and cached: ${JSON.stringify(room)}`); return room; + } catch (error) { - r.error(`Error fetching room info: ${error}`); + r.error(`Error fetching/caching room info: ${error}`); return null; } } -async function routeWebSocket(r) { - try { - const roomInfo = await fetchRoomInfo(r); +export default { + get_cache_dict, + routeWebSocket: async function(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; + if (!roomInfo || !roomInfo.host) { + r.error(`Debug: Invalid room info: ${JSON.stringify(roomInfo)}`); + r.return(404, 'Room not found or invalid'); + return; + } + + 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)}`); + + r.variables.proxy_target = proxyUrl; + r.internalRedirect('@websocket_proxy'); + + } catch (error) { + r.error(`WebSocket routing error: ${error}`); + r.return(500, 'Internal routing error'); } - - // 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 }; \ No newline at end of file +}; \ No newline at end of file