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:
parent
017a098fca
commit
42f4c98014
4 changed files with 90 additions and 20 deletions
|
|
@ -413,9 +413,16 @@ export function pickLocale(
|
|||
return persona.locales[Math.floor(rand() * persona.locales.length)]!;
|
||||
}
|
||||
|
||||
// --- BRouter --------------------------------------------------------------
|
||||
// --- Route compute via the planner ---------------------------------------
|
||||
|
||||
const BROUTER_URL = process.env.BROUTER_URL ?? "http://localhost:17777";
|
||||
/**
|
||||
* The journal goes through the planner (not BRouter directly) because
|
||||
* the planner is the BRouter client in this architecture — route
|
||||
* compute, rate limiting, and the eventual move of BRouter off-box
|
||||
* all live there. Same `PLANNER_URL` the interactive handoff routes
|
||||
* use; falls back to the dev port in local work.
|
||||
*/
|
||||
const PLANNER_URL = process.env.PLANNER_URL ?? "http://localhost:3001";
|
||||
|
||||
export interface BrouterResult {
|
||||
gpx: string;
|
||||
|
|
@ -430,12 +437,26 @@ export async function requestBrouterGpx(
|
|||
endpoints: Endpoints,
|
||||
{ signal }: { signal?: AbortSignal } = {},
|
||||
): Promise<BrouterResult | BrouterError> {
|
||||
const lonlats = `${endpoints.start[0]},${endpoints.start[1]}|${endpoints.end[0]},${endpoints.end[1]}`;
|
||||
const url = `${BROUTER_URL}/brouter?lonlats=${lonlats}&profile=trekking&alternativeidx=0&format=gpx`;
|
||||
const url = `${PLANNER_URL}/api/route`;
|
||||
const body = {
|
||||
waypoints: [
|
||||
{ lon: endpoints.start[0], lat: endpoints.start[1] },
|
||||
{ lon: endpoints.end[0], lat: endpoints.end[1] },
|
||||
],
|
||||
profile: "trekking",
|
||||
format: "gpx" as const,
|
||||
};
|
||||
try {
|
||||
const resp = await fetch(url, { signal });
|
||||
const resp = await fetch(url, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body),
|
||||
signal,
|
||||
});
|
||||
if (!resp.ok) {
|
||||
if (resp.status === 500) return { kind: "no-route" };
|
||||
// Planner returns 422 for BRouter client errors (unroutable
|
||||
// waypoints) and 5xx for upstream/planner failures.
|
||||
if (resp.status === 422) return { kind: "no-route" };
|
||||
return { kind: "upstream-error", status: resp.status };
|
||||
}
|
||||
const gpx = await resp.text();
|
||||
|
|
|
|||
|
|
@ -2,9 +2,9 @@ import type { Route } from "./+types/api.v1.routes.compute";
|
|||
import { requireApiUser, apiError } from "~/lib/api-guard.server";
|
||||
import { ComputeRouteRequestSchema, ERROR_CODES } from "@trails-cool/api";
|
||||
|
||||
const BROUTER_URL = process.env.BROUTER_URL ?? "http://localhost:17777";
|
||||
const PLANNER_URL = process.env.PLANNER_URL ?? "http://localhost:3001";
|
||||
|
||||
/** POST /api/v1/routes/compute — proxy to BRouter */
|
||||
/** POST /api/v1/routes/compute — server-to-server proxy through the planner. */
|
||||
export async function action({ request }: Route.ActionArgs) {
|
||||
if (request.method !== "POST") return new Response(null, { status: 405 });
|
||||
await requireApiUser(request);
|
||||
|
|
@ -16,19 +16,18 @@ export async function action({ request }: Route.ActionArgs) {
|
|||
parsed.error.issues.map((i) => ({ field: i.path.join("."), message: i.message })));
|
||||
}
|
||||
|
||||
const { waypoints, profile } = parsed.data;
|
||||
const lonlats = waypoints.map((w) => `${w.lon},${w.lat}`).join("|");
|
||||
|
||||
const brouterUrl = `${BROUTER_URL}/brouter?lonlats=${lonlats}&profile=${profile}&alternativeidx=0&format=geojson`;
|
||||
|
||||
try {
|
||||
const resp = await fetch(brouterUrl);
|
||||
const resp = await fetch(`${PLANNER_URL}/api/route`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(parsed.data),
|
||||
});
|
||||
if (!resp.ok) {
|
||||
return apiError(502, ERROR_CODES.INTERNAL_ERROR, `BRouter returned ${resp.status}`);
|
||||
return apiError(resp.status === 422 ? 422 : 502, ERROR_CODES.INTERNAL_ERROR, `Planner returned ${resp.status}`);
|
||||
}
|
||||
const geojson = await resp.json();
|
||||
return Response.json(geojson);
|
||||
const payload = await resp.json();
|
||||
return Response.json(payload);
|
||||
} catch {
|
||||
return apiError(502, ERROR_CODES.INTERNAL_ERROR, "BRouter unavailable");
|
||||
return apiError(502, ERROR_CODES.INTERNAL_ERROR, "Planner unavailable");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue