Files
jason5ng32andClaude Opus 5.5 c29e48687b Refactor(layout): split backend-only code out of common/, group frontend utils
common/ had become the backend's library: 30 of its 41 modules were
imported by nothing in the frontend, so the name no longer said what
belonged there. It now holds only what the frontend or the build imports
too; backend-only code moved to server/, flat except for datasets/ (the
dataset engine and its readers). The *-db/ dataset directories stay in
common/: self-hosting docs and Docker volumes mount those paths.

frontend/utils/ keeps its app-wide modules at the root and gains ip/
(IP / ASN shaping and validation), report/ (the report pipeline) and
features/ (the pure layer of a single feature). Paths only — no logic
changes. The Dockerfile copies server/, and the AGENTS.md files say where
a new module goes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-05 13:10:02 +08:00

44 lines
1.7 KiB
JavaScript

// /api/cfradar — single dispatch route for all Cloudflare Radar data.
// `?view=` selects an entry in RADAR_VIEWS (server/cf-radar.js): the view's
// guards validate and normalize its params, its fetch builds the payload,
// and the route's cache middleware in backend-server.js reads the view's
// per-view TTL. Guards run before the API-key check so param errors stay
// 400s on keyless deployments (and tests never reach an upstream call).
import { RADAR_VIEWS, hasRadarApiKey } from '../server/cf-radar.js';
import logger from '../server/logger.js';
// Run a view's guard middlewares outside an Express chain. Each guard either
// calls next() or writes its own 4xx response — so "didn't pass" means the
// response has already been sent and the dispatcher must stop.
const runGuards = (guards, req, res) => guards.every((guard) => {
let passed = false;
guard(req, res, () => { passed = true; });
return passed;
});
export default async (req, res) => {
if (req.method !== 'GET') {
return res.status(405).json({ error: 'Method Not Allowed' });
}
const viewName = req.query.view;
if (!viewName) {
return res.status(400).json({ error: 'No view provided' });
}
const view = RADAR_VIEWS[viewName];
if (!view) {
return res.status(400).json({ error: 'Invalid view' });
}
if (!runGuards(view.guards, req, res)) return;
if (!hasRadarApiKey()) {
return res.status(500).json({ error: 'API key is missing' });
}
try {
res.json(await view.fetch(req.query));
} catch (error) {
logger.error({ err: error, view: viewName }, 'cf-radar view failed');
res.status(500).json({ error: 'Internal server error' });
}
};