Files
WeatherStar4000/proxy/handlers.mjs
Eddy G 7a07c67e84 Replace CORS proxy with complete server-side cache
- Replace cors/ directory and cors.mjs utility with comprehensive
    HTTP caching proxy in proxy/ directory
- Implement RFC-compliant caching with cache-control headers,
    conditional requests, and in-flight deduplication
- Centralized error handling with "safe" fetch utilities
- Add unified proxy handlers for weather.gov, SPC, radar, and mesonet APIs
- Include cache management endpoint and extensive diagnostic logging
- Migrate client-side URL rewriting from cors.mjs to url-rewrite.mjs
2025-06-24 20:45:43 -04:00

45 lines
1.5 KiB
JavaScript

// Consolidated proxy handlers for all external API requests with caching
import cache from './cache.mjs';
import OVERRIDES from '../src/overrides.mjs';
// Weather.gov API proxy (catch-all for any Weather.gov API endpoint)
export const weatherProxy = async (req, res) => {
await cache.handleRequest(req, res, 'https://api.weather.gov', {
serviceName: 'Weather.gov',
skipParams: ['u'],
});
};
// Radar proxy for weather radar images
export const radarProxy = async (req, res) => {
await cache.handleRequest(req, res, 'https://radar.weather.gov', {
serviceName: 'Radar',
skipParams: ['u'],
encoding: 'binary', // Radar images are binary data
});
};
// SPC (Storm Prediction Center) outlook proxy
export const outlookProxy = async (req, res) => {
await cache.handleRequest(req, res, 'https://www.spc.noaa.gov', {
serviceName: 'SPC Outlook',
skipParams: ['u'],
});
};
// Iowa State Mesonet proxy with configurable host
export const mesonetProxy = async (req, res) => {
// Determine if this is a binary file (images)
const isBinary = req.path.match(/\.(png|jpg|jpeg|gif|webp|ico)$/i);
// Use override radar host if provided, otherwise default to mesonet
const radarHost = OVERRIDES.RADAR_HOST || 'mesonet.agron.iastate.edu';
await cache.handleRequest(req, res, `https://${radarHost}`, {
serviceName: `Iowa State Mesonet (${radarHost})`,
skipParams: [], // No parameters to skip for Mesonet
encoding: isBinary ? 'binary' : 'utf8', // Use binary encoding for images
});
};