The planner is the BRouter client in this architecture — route compute, rate limiting, and the eventual move of BRouter off-box all live there. The journal had two places that called BRouter directly (demo-bot and /api/v1/routes/compute) and both were broken on prod because the journal service has no BROUTER_URL. Fix: - Extend the planner's /api/route with an optional `format: "gpx"` that returns BRouter's raw GPX (for server-to-server callers that don't need the way-tag enriched GeoJSON). - Point the journal demo-bot at `PLANNER_URL/api/route` with `format: "gpx"` instead of calling BRouter directly. - Point /api/v1/routes/compute at `PLANNER_URL/api/route` instead of BRouter directly. Journal no longer needs BROUTER_URL at all. When BRouter moves to a dedicated host later, only the planner changes. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
65 lines
2.3 KiB
TypeScript
65 lines
2.3 KiB
TypeScript
import { data } from "react-router";
|
|
import type { Route } from "./+types/api.route";
|
|
import { computeRoute, computeSegmentGpx, BRouterError } from "~/lib/brouter";
|
|
import { checkRateLimit } from "~/lib/rate-limit";
|
|
|
|
export async function action({ request }: Route.ActionArgs) {
|
|
if (request.method !== "POST") {
|
|
return data({ error: "Method not allowed" }, { status: 405 });
|
|
}
|
|
|
|
const body = await request.json();
|
|
const { waypoints, profile, sessionId, noGoAreas, format } = body as {
|
|
waypoints: Array<{ lat: number; lon: number }>;
|
|
profile?: string;
|
|
sessionId?: string;
|
|
noGoAreas?: Array<{ points: Array<{ lat: number; lon: number }> }>;
|
|
format?: "geojson" | "gpx";
|
|
};
|
|
|
|
if (!waypoints || waypoints.length < 2) {
|
|
return data({ error: "At least 2 waypoints are required" }, { status: 400 });
|
|
}
|
|
|
|
// Rate limit by session ID or IP
|
|
const rateLimitKey = sessionId ?? request.headers.get("x-forwarded-for") ?? "unknown";
|
|
const limit = checkRateLimit(`route:${rateLimitKey}`);
|
|
|
|
if (!limit.allowed) {
|
|
return data(
|
|
{ error: "Rate limit exceeded" },
|
|
{
|
|
status: 429,
|
|
headers: { "Retry-After": String(limit.retryAfterSeconds) },
|
|
},
|
|
);
|
|
}
|
|
|
|
try {
|
|
// `format: "gpx"` returns BRouter's raw GPX without the way-tag
|
|
// enrichment the interactive planner needs. Used by server-to-server
|
|
// callers (e.g. the Journal's demo-bot) that just want the track.
|
|
if (format === "gpx") {
|
|
const gpx = await computeSegmentGpx({ waypoints, profile, noGoAreas });
|
|
return new Response(gpx, {
|
|
status: 200,
|
|
headers: {
|
|
"content-type": "application/gpx+xml; charset=utf-8",
|
|
"x-ratelimit-remaining": String(limit.remaining),
|
|
},
|
|
});
|
|
}
|
|
|
|
const route = await computeRoute({ waypoints, profile, noGoAreas });
|
|
return data(route, {
|
|
headers: { "X-RateLimit-Remaining": String(limit.remaining) },
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof BRouterError && e.statusCode >= 400 && e.statusCode < 500) {
|
|
// BRouter client error: unroutable waypoints, missing tiles, etc.
|
|
return data({ error: e.message, code: "no_route" }, { status: 422 });
|
|
}
|
|
const message = e instanceof Error ? e.message : "Route computation failed";
|
|
return data({ error: message, code: "server_error" }, { status: 502 });
|
|
}
|
|
}
|