trails/apps/journal/app/routes/api.v1.routes.compute.ts
Ullrich Schäfer 42f4c98014
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>
2026-04-19 11:11:10 +02:00

33 lines
1.3 KiB
TypeScript

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 PLANNER_URL = process.env.PLANNER_URL ?? "http://localhost:3001";
/** 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);
const body = await request.json().catch(() => null);
const parsed = ComputeRouteRequestSchema.safeParse(body);
if (!parsed.success) {
return apiError(400, ERROR_CODES.VALIDATION_ERROR, "Validation failed",
parsed.error.issues.map((i) => ({ field: i.path.join("."), message: i.message })));
}
try {
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(resp.status === 422 ? 422 : 502, ERROR_CODES.INTERNAL_ERROR, `Planner returned ${resp.status}`);
}
const payload = await resp.json();
return Response.json(payload);
} catch {
return apiError(502, ERROR_CODES.INTERNAL_ERROR, "Planner unavailable");
}
}