mirror of
https://github.com/koala73/worldmonitor.git
synced 2026-04-25 17:14:57 +02:00
* refactor: dedupe edge api json response assembly * refactor: expand jsonResponse helper to all edge functions Roll out jsonResponse() from _json-response.js to 16 files (14 handlers + 2 shared helpers), eliminating 55 instances of the new Response(JSON.stringify(...)) boilerplate. Only exception: health.js uses JSON.stringify(body, null, indent) for pretty-print mode, which is incompatible with the helper signature. Replaced local jsonResponse/json() definitions in contact.js, register-interest.js, and cache-purge.js with the shared import.
69 lines
1.6 KiB
JavaScript
69 lines
1.6 KiB
JavaScript
import { getCorsHeaders, isDisallowedOrigin } from './_cors.js';
|
|
import { jsonResponse } from './_json-response.js';
|
|
import { readJsonFromUpstash } from './_upstash-json.js';
|
|
|
|
export const config = { runtime: 'edge' };
|
|
|
|
const REDIS_KEY = 'military:flights:v1';
|
|
const STALE_KEY = 'military:flights:stale:v1';
|
|
|
|
let cached = null;
|
|
let cachedAt = 0;
|
|
const CACHE_TTL = 120_000;
|
|
|
|
let negUntil = 0;
|
|
const NEG_TTL = 30_000;
|
|
|
|
async function fetchMilitaryFlightsData() {
|
|
const now = Date.now();
|
|
if (cached && now - cachedAt < CACHE_TTL) return cached;
|
|
if (now < negUntil) return null;
|
|
|
|
let data;
|
|
try { data = await readJsonFromUpstash(REDIS_KEY); } catch { data = null; }
|
|
|
|
if (!data) {
|
|
try { data = await readJsonFromUpstash(STALE_KEY); } catch { data = null; }
|
|
}
|
|
|
|
if (!data) {
|
|
negUntil = now + NEG_TTL;
|
|
return null;
|
|
}
|
|
|
|
cached = data;
|
|
cachedAt = now;
|
|
return data;
|
|
}
|
|
|
|
export default async function handler(req) {
|
|
const corsHeaders = getCorsHeaders(req, 'GET, OPTIONS');
|
|
|
|
if (req.method === 'OPTIONS') {
|
|
return new Response(null, { status: 204, headers: corsHeaders });
|
|
}
|
|
|
|
if (isDisallowedOrigin(req)) {
|
|
return jsonResponse({ error: 'Origin not allowed' }, 403, corsHeaders);
|
|
}
|
|
|
|
const data = await fetchMilitaryFlightsData();
|
|
|
|
if (!data) {
|
|
return jsonResponse(
|
|
{ error: 'Military flight data temporarily unavailable' },
|
|
503,
|
|
{ 'Cache-Control': 'no-cache, no-store', ...corsHeaders },
|
|
);
|
|
}
|
|
|
|
return jsonResponse(
|
|
data,
|
|
200,
|
|
{
|
|
'Cache-Control': 's-maxage=120, stale-while-revalidate=60, stale-if-error=300',
|
|
...corsHeaders,
|
|
},
|
|
);
|
|
}
|