garage44.eu/jeroen/cartopolis

logclone: git clone https://garage44.eu/jeroen/cartopolis.git

.. /docs/adr/034-public-data-relay.md

raw

ADR-034: Public Data Relay (Standardised Upstream Adapters)


Metadata:


Context

Problem: ADR-033's web target needs some Public-tier upstreams fetched server-side. That is not a one-off — we keep adding upstreams, and the client already reaches eleven of them while re-implementing the same policy each time, divergently:

ConcernCurrent state
User-Agentfive different constants (TILE_USER_AGENT, NAV_USER_AGENT, OVAPI_USER_AGENT ×2, plus weather.rs / globe_weather.rs)
Cacheper-file disk paths (tile_cache_path, cell_cache_path ×2), per-file TTLs (LINES_TTL, VEHICLE_TTL, REFRESH ×2)
PolitenessOVERPASS_THROTTLE sleep in one file, ad-hoc 15u64 << attempts backoff in another, nothing in the rest
Timeoutshand-set per call site (15 s, 30 s, …)

So the requirement is "standardise how a Public-tier upstream is reached", in a way that also collapses this duplication.

CORS is not the driver — one origin is

An earlier framing of this work said we needed to emit CORS headers. That was wrong. If the wasm app and everything it calls are served from one origin, those requests are same-origin and CORS never applies. Our own services are currently on subdomains (nominatim.cartopolis.org, 3dbag.cartopolis.org), and a subdomain is a different origin — so the fix is to path-mount them under the app's origin in the existing Caddy, not to sprinkle CORS headers.

CORS survives in exactly two places: consuming a third party's headers if we ever call one directly (L0 below), and local dev (trunk serve on another port — use trunk's proxy to mirror the production paths rather than dev-only CORS headers).

The relay's real justification is therefore server-side fetch, caching, identification and transform — not CORS.

Findings from the upstream audit

Auditing all eleven upstreams surfaced three constraints that matter more than CORS did:

  1. Secret custody. ai_assistant.rs sends an ANTHROPIC_API_KEY as x-api-key. A wasm build ships to the user's machine, so it cannot hold a secret. This one is not a Public-tier problem at all — see "What the relay does not serve" below.
  2. Mixed content. OVapi departures (http://v0.ovapi.nl/tpc/…) and lines (http://v0.ovapi.nl/line/) are plain HTTP. An HTTPS page cannot fetch them at all — blocked as mixed content, independent of CORS. They must be proxied to be reachable at all.
  3. User-Agent is a forbidden header in browsers. fetch() cannot set it. Every upstream whose usage policy expects identification — OSM, Nominatim, Overpass, met.no, OVapi, i.e. the reason those five constants exist — cannot be identified from a browser. Anything carrying a real identification obligation therefore wants a relay.

Requirements:

Decision

Add a cartopolis_relay crate: a small, stateless, unauthenticated HTTP service that fronts Public-tier upstreams through a registry of upstream adapters, served from the same origin as the app.

Single-origin layout (one Caddy site block)

https://cartopolis.org/            → wasm app + assets
https://cartopolis.org/tiles/*     → self-hosted vector tiles, PMTiles (ADR-035)
https://cartopolis.org/nominatim/* → nominatim
https://cartopolis.org/overpass/*  → overpass
https://cartopolis.org/3dbag/*     → 3dbag
https://cartopolis.org/v1/*        → cartopolis_relay (L2 adapters)
wss://cartopolis.org/ws            → cartopolis_simulator (ADR-033 Phase 2)

Same Caddy that already reverse-proxies these, on one hostname instead of several.

A three-level ladder (choose the lowest that works)

LevelWhenCost
L0 — directthird-party that sends CORS and carries no identification obligationnothing. Don't proxy what you needn't (P-03). Currently no members — kept as the question to ask first
L1 — path-mountour own services; simple passthroughCaddy route only, no Rust
L2 — relay adapterneeds transform, cache, identification, or is otherwise unreachable (mixed content, no CORS)one UPSTREAMS entry

The registry (mirrors LAYER_DEFS, ADR-023)

pub struct UpstreamDef {
    /// Stable key; served at `/v1/{key}`.
    pub key: &'static str,
    /// Build the upstream URL from the request's query params.
    pub url: fn(&Params) -> Result<String, String>,
    /// Upstream bytes → response bytes. Identity for a pure passthrough.
    pub transform: fn(&[u8]) -> Result<Vec<u8>, String>,
    pub content_type: &'static str,
    /// Serve from cache within this window.
    pub ttl: Duration,
    /// Minimum spacing between upstream hits (politeness).
    pub min_interval: Duration,
}

pub const UPSTREAMS: &[UpstreamDef] = &[ /* one entry per upstream */ ];

Every adapter gets these for free — each replaces something currently duplicated or missing:

  1. TTL cache + single-flight — concurrent requests collapse to one upstream fetch.
  2. Stale-if-error — serve the last good response when the upstream fails, instead of a gap.
  3. One project User-Agent — replaces the five divergent constants, and supplies the identification a browser cannot.
  4. Politeness spacing (min_interval) — replaces the ad-hoc sleep/backoff.
  5. Timeout + size cap — uniform, not per-call-site.
  6. TLS termination — an http:// upstream becomes reachable from an HTTPS page.

Upstream inventory and approach

#SourceEndpointApproachWhy
1OSM vector tiles (MVT)(self-hosted — ADR-035)L1 /tiles/*OSM discourages proxying their CDN; we serve our own vector tiles from a PMTiles archive (ADR-035). Highest byte volume
2Nominatim searchnominatim.cartopolis.orgL1 /nominatim/*ours
3Nominatim reversesame, /reverseL1 /nominatim/*ours
4Overpassoverpass.cartopolis.org/api/interpreterL1 /overpass/*ours (client-side throttle can later move to an L2 adapter)
53DBAG LoD2.23dbag.cartopolis.org/lod22/{z}/{x}/{y}.jsonL1 /3dbag/*ours; large payloads suit plain HTTP caching
6met.no forecastapi.met.no/weatherapi/locationforecast/2.0/completeL2 met-forecastUA identification required by ToS; shared cache honours their caching guidance
7NOAA GFS (GRIB2)nomads.ncep.noaa.gov/cgi-bin/filter_gfs_1p00.plL2 gfs-tcdctransform (GRIB → 360×181 grid) + cycle clock + cache; no CORS
8OVapi departureshttp://v0.ovapi.nl/tpc/{codes}L2 ovapi-departuresmixed content — unreachable otherwise; + UA
9OVapi lineshttp://v0.ovapi.nl/line/L2 ovapi-linesmixed content; long TTL (24 h today)
10OVapi GTFS-RTgtfs.ovapi.nl/nl/vehiclePositions.pbL2 ovapi-vehicles15 s poll × N clients is antisocial; shared cache. Best future candidate for push over WS
11Anthropic APIapi.anthropic.com/v1/messagesnot the relay — see belowholds a secret; admin-only

What the relay does not serve

The relay stays Public-tier and unauthenticated. Anything needing a session or an ACL goes through cartopolis_simulator over NetMessage, per the transport rule (Public/cacheable → HTTP; session-bound/authoritative → WebSocket).

The AI assistant is the first such case: the Anthropic key is a server secret, and access is admin-only (ADR-031's Authorized(cap) tier — its first concrete use). So the AI request becomes a NetMessage handled by the sim, which checks the capability and calls Anthropic with its own key. It is not an upstream adapter, and AiConfig.api_key leaves the client.

Where it runs

cartopolis_relay is lib + thin bin, the shape ADR-015 already established for cartopolis_simulator ("exposes both a lib and a thin bin, so the server can run standalone or embedded"). cartopolis_simulator embeds it behind a default-on relay feature, so the normal deployment is still one process; splitting it out later is a flag, not a refactor.

It is a separate HTTP listener from the game protocol: no auth, no session, no sim state, no NetMessage. That separation is load-bearing — ADR-031's Public tier and ADR-033's Phase 1 must work with no sim session, and the world-authority process should not carry a public unauthenticated surface by default (P-04).

Client side

The client keeps a configurable base URL (same shape as the existing CARTO_NOMINATIM_URL env override, ADR-014) and targets /tiles/…, /nominatim/…, /overpass/…, /3dbag/… or /v1/{key}. This is URL construction, not a new abstraction — it composes with ADR-033's HttpFetch provider. Both targets use the relay, so there is one code path (the ADR-033 principle) and native gains the shared cache and politeness too.

Rationale

Primary Reasoning:

  1. It consolidates instead of adding. The relay is where five User-Agents, four cache schemes and two throttle strategies become one. The web target is the trigger; the cleanup is worth doing regardless.
  2. Browsers structurally cannot satisfy our upstream obligations. No User-Agent, no mixed content, no secrets. These aren't preferences to design around — they decide which upstreams must be server-side.
  3. A shared warm cache is better citizenship and cheaper (P-03), and keeps us inside usage policies as the viewer count grows.
  4. One registry entry per upstream matches the pattern contributors already know from LAYER_DEFS (P-05).
  5. Lib + bin + default-on embed gives the simple single-process deployment, without welding a public endpoint to the authoritative sim.
  6. Keeping auth out of the relay preserves one clean invariant (Public = anonymous, stateless, cacheable) and sends session-bound work where authority already lives.

Alternatives Considered:

AlternativeProsConsRejected Because
Emit CORS headers everywhere, keep subdomainsNo Caddy restructurePreflights, more config, still cross-originSingle origin makes CORS moot entirely
Module inside cartopolis_simulator, no crateFewest moving partsPublic tier then requires running the world sim; public unauth surface on the authority processBreaks ADR-031/033 Phase-1 independence
Caddy proxy config onlyNo Rust codeCan't transform (GRIB→grid), can't single-flight or stale-if-errorCovers L1 only; L2 needs code
Relay grows authenticated adapters (for Anthropic)One serviceMuddies the stateless/anonymous invariant; duplicates sim authzSession-bound work belongs in the sim
Proxy OSM's tile CDNNo tile infrastructureOSM discourages proxying; still can't set UAADR-035 self-hosts vector tiles instead
Bespoke endpoint per upstreamSimple each timeRebuilds cache/UA/throttle per endpointDoesn't standardise anything

Consequences

Positive:

Negative:

Unverified: whether met.no and the OVapi feeds send permissive CORS headers — the relay is warranted for them on identification/mixed-content grounds regardless, so this does not change the approach, only the reasoning recorded per row.

Related