Route journal BRouter calls through the planner

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>
This commit is contained in:
Ullrich Schäfer 2026-04-19 11:11:10 +02:00
parent 017a098fca
commit 42f4c98014
No known key found for this signature in database
GPG key ID: A32FF691A0F752D9
4 changed files with 90 additions and 20 deletions

View file

@ -284,3 +284,38 @@ function noGoAreasToParam(areas: NoGoArea[]): string {
export function getBRouterUrl(): string {
return BROUTER_URL;
}
/**
* Single-segment GPX fetch. Used by callers (notably the Journal's
* demo-bot) that only need the raw GPX track for two endpoints no
* multi-waypoint merge, no way-tag enrichment. Throws BRouterError on
* non-2xx so the action handler can map it to an HTTP status.
*/
export async function computeSegmentGpx(request: {
waypoints: Array<{ lat: number; lon: number }>;
profile?: string;
noGoAreas?: NoGoArea[];
}): Promise<string> {
if (request.waypoints.length < 2) {
throw new Error("At least 2 waypoints are required");
}
const profile = request.profile ?? "trekking";
const lonlats = request.waypoints.map((w) => `${w.lon},${w.lat}`).join("|");
const params = new URLSearchParams({
lonlats,
profile,
alternativeidx: "0",
format: "gpx",
});
const nogoParam = request.noGoAreas?.length ? noGoAreasToParam(request.noGoAreas) : undefined;
if (nogoParam) params.set("polygons", nogoParam);
const resp = await fetch(`${BROUTER_URL}/brouter?${params}`);
if (!resp.ok) {
const body = await resp.text();
throw new BRouterError(body.trim(), resp.status);
}
const gpx = await resp.text();
if (!gpx.includes("<trkpt")) throw new BRouterError("no route found", 422);
return gpx;
}

View file

@ -1,6 +1,6 @@
import { data } from "react-router";
import type { Route } from "./+types/api.route";
import { computeRoute, BRouterError } from "~/lib/brouter";
import { computeRoute, computeSegmentGpx, BRouterError } from "~/lib/brouter";
import { checkRateLimit } from "~/lib/rate-limit";
export async function action({ request }: Route.ActionArgs) {
@ -9,11 +9,12 @@ export async function action({ request }: Route.ActionArgs) {
}
const body = await request.json();
const { waypoints, profile, sessionId, noGoAreas } = body as {
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) {
@ -35,6 +36,20 @@ export async function action({ request }: Route.ActionArgs) {
}
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) },