Skip to main content

Module http

Module http 

Source
Expand description

HTTP JSON-RPC 2.0 transport for tcode serve --http (#2053, #2054).

Why: matches how trusty-memory (web::router + run_http_on) and trusty-search (service::server::build_router) stand up their axum daemons: a pure axum::Router builder — testable via oneshot, no real socket — wrapped in the shared guarded middleware stack (trusty_common::server::with_guarded_middleware_same_origin_cors, #6003), plus a bind+serve entry point that installs the shared trusty_common::shutdown_signal() graceful-shutdown watcher (SIGTERM + SIGINT), exactly like trusty-memory’s run_http_on. POST /rpc and GET /health dispatch through the SAME crate::jsonrpc::Router the STDIO transport (crate::serve::transport) uses via Router::dispatch_json — there is exactly one implementation of each method (ping, health, session.*, …), not one per transport.

#2054 adds GET /sessions/{id}/events: an SSE route that is HTTP’s real live-streaming mechanism for session.attach. HTTP POST /rpc is fundamentally one request/one response, so session.attach over HTTP acknowledges immediately (replay burst + this endpoint’s URL in stream_url) rather than holding the connection open — a browser or CLI client then opens this endpoint to receive the ring-buffer replay followed by live events, and simply closing that connection IS “detach” for HTTP (there is nothing further to call). See crate::jsonrpc::ConnectionContext’s docs and session::protocol::attach for the full STDIO-vs-HTTP rationale. Multi-client attach falls out for free here: every GET gets its own crate::events::subscribe() receiver.

What: build_axum_router (pure, unit-testable) and run_http (binds a TcpListener — port 0 yields an OS-assigned ephemeral port — logs the bound address to stderr, never stdout, and serves until graceful shutdown).

#5439 puts every route behind a credential. trusty_common::daemon_token mints and stores it at 0600; trusty_common::server::bearer_auth is the router-wide guard, applied here to the FULLY-MERGED router so a route added later cannot slip past it. PUBLIC_HEALTH_PATH is the single exemption and discloses only liveness to an anonymous caller (#6472); SSE_TICKET_PATH is how a browser EventSource, which cannot send a header, carries an existing right onto a URL.

Deferred: batch JSON-RPC requests (a JSON array body) are not supported — POST /rpc accepts a single request object per call, matching what the ticket asked for (“single-request is fine for now”).

Also deferred, and larger: this whole listener is scheduled for replacement by a Unix socket fronted by trusty-console (ADR-0032, the #5439 owner ruling of 2026-08-19), which authenticates by peer credential rather than by a shared secret. This layer is the hardening that stands until then, not the end state.

Test: http::tests::* drive build_axum_router via tower::util::ServiceExt::oneshot (no real socket): POST /rpc ping success, POST /rpc malformed JSON -> -32700, GET /health, session.create + GET /sessions/{id}/events SSE replay + live event.

Constants§

PUBLIC_HEALTH_PATH
The one route that answers an unauthenticated caller (#6472).
SSE_TICKET_PATH
POST here (authenticated) to mint a single-use SSE ticket.
TOKEN_APP_NAME
The app name whose data directory holds this daemon’s credential file.

Functions§

build_axum_router
Build the pure axum router (no listener bound) exposing POST /rpc, GET /health, GET /sessions/{id}/events, and (#2983 Slice 2 + Slice 3) the full /sessions* REST resource group.
run_http
Bind a TcpListener and serve POST /rpc + GET /health + GET /sessions/{id}/events until SIGTERM/SIGINT (graceful) or an internal axum error.