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 trusty_common::server::with_standard_middleware (CORS/trace/gzip), 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).

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”).

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.

Functions§

build_axum_router
Build the pure axum router (no listener bound) exposing POST /rpc, GET /health, and GET /sessions/{id}/events.
run_http
Bind a TcpListener and serve POST /rpc + GET /health + GET /sessions/{id}/events until SIGTERM/SIGINT (graceful) or an internal axum error.