mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
- Remove PostHog analytics runtime and configuration - Add API rate limiting (api/_rate-limit.js) - Harden traffic controls across edge functions - Add runtime fallback controls and data-loader improvements - Add military base data scripts (fetch-mirta-bases, fetch-osm-bases) - Gitignore large raw data files - Settings playground prototypes
106 lines
3.5 KiB
JavaScript
106 lines
3.5 KiB
JavaScript
import { getCorsHeaders, isDisallowedOrigin } from './_cors.js';
|
|
import { validateApiKey } from './_api-key.js';
|
|
import { checkRateLimit } from './_rate-limit.js';
|
|
|
|
export const config = { runtime: 'edge' };
|
|
|
|
function getRelayBaseUrl() {
|
|
const relayUrl = process.env.WS_RELAY_URL;
|
|
if (!relayUrl) return null;
|
|
return relayUrl.replace('wss://', 'https://').replace('ws://', 'http://').replace(/\/$/, '');
|
|
}
|
|
|
|
function getRelayHeaders(baseHeaders = {}) {
|
|
const headers = { ...baseHeaders };
|
|
const relaySecret = process.env.RELAY_SHARED_SECRET || '';
|
|
if (relaySecret) {
|
|
const relayHeader = (process.env.RELAY_AUTH_HEADER || 'x-relay-key').toLowerCase();
|
|
headers[relayHeader] = relaySecret;
|
|
headers.Authorization = `Bearer ${relaySecret}`;
|
|
}
|
|
return headers;
|
|
}
|
|
|
|
async function fetchWithTimeout(url, options, timeoutMs = 15000) {
|
|
const controller = new AbortController();
|
|
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
try {
|
|
return await fetch(url, { ...options, signal: controller.signal });
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
}
|
|
}
|
|
|
|
export default async function handler(req) {
|
|
const corsHeaders = getCorsHeaders(req, 'GET, OPTIONS');
|
|
|
|
if (isDisallowedOrigin(req)) {
|
|
return new Response(JSON.stringify({ error: 'Origin not allowed' }), {
|
|
status: 403,
|
|
headers: { 'Content-Type': 'application/json', ...corsHeaders },
|
|
});
|
|
}
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, { status: 204, headers: corsHeaders });
|
|
}
|
|
if (req.method !== 'GET') {
|
|
return new Response(JSON.stringify({ error: 'Method not allowed' }), {
|
|
status: 405,
|
|
headers: { 'Content-Type': 'application/json', ...corsHeaders },
|
|
});
|
|
}
|
|
|
|
const keyCheck = validateApiKey(req);
|
|
if (keyCheck.required && !keyCheck.valid) {
|
|
return new Response(JSON.stringify({ error: keyCheck.error }), {
|
|
status: 401,
|
|
headers: { 'Content-Type': 'application/json', ...corsHeaders },
|
|
});
|
|
}
|
|
|
|
const rateLimitResponse = await checkRateLimit(req, corsHeaders);
|
|
if (rateLimitResponse) return rateLimitResponse;
|
|
|
|
const relayBaseUrl = getRelayBaseUrl();
|
|
if (!relayBaseUrl) {
|
|
return new Response(JSON.stringify({ error: 'WS_RELAY_URL is not configured' }), {
|
|
status: 503,
|
|
headers: { 'Content-Type': 'application/json', ...corsHeaders },
|
|
});
|
|
}
|
|
|
|
try {
|
|
const requestUrl = new URL(req.url);
|
|
const relayUrl = `${relayBaseUrl}/polymarket${requestUrl.search || ''}`;
|
|
const response = await fetchWithTimeout(relayUrl, {
|
|
headers: getRelayHeaders({ Accept: 'application/json' }),
|
|
}, 15000);
|
|
|
|
const body = await response.text();
|
|
const isSuccess = response.status >= 200 && response.status < 300;
|
|
const headers = {
|
|
'Content-Type': response.headers.get('content-type') || 'application/json',
|
|
'Cache-Control': isSuccess
|
|
? 'public, max-age=120, s-maxage=300, stale-while-revalidate=900, stale-if-error=1800'
|
|
: 'public, max-age=10, s-maxage=30, stale-while-revalidate=120',
|
|
...(isSuccess && { 'CDN-Cache-Control': 'public, s-maxage=300, stale-while-revalidate=900, stale-if-error=1800' }),
|
|
...corsHeaders,
|
|
};
|
|
|
|
return new Response(body, {
|
|
status: response.status,
|
|
headers,
|
|
});
|
|
} catch (error) {
|
|
const isTimeout = error?.name === 'AbortError';
|
|
return new Response(JSON.stringify({
|
|
error: isTimeout ? 'Relay timeout' : 'Relay request failed',
|
|
details: error?.message || String(error),
|
|
}), {
|
|
status: isTimeout ? 504 : 502,
|
|
headers: { 'Content-Type': 'application/json', ...corsHeaders },
|
|
});
|
|
}
|
|
}
|