Axum host (id_effect_axum)
Workspace crate id_effect_axum runs Effect<A, E, R> programs inside
Axum handlers on the same Tokio runtime that serves the request.
Mental model
- Axum stays
async fnat the wire edge; your domain stays inEffect. - The router state
Sis the handler's environment: a handler isFn(&mut S) -> Effect<A, E, S>.Scan be plain state, a typed capability list (HCons<Db, HNil>, read withneed::<Db>()), or a runtime-checkedDynEnv. - Effects are
Send, so each request's effect runs as one fiber on the serving runtime (anEffectExecutorover the ambient Tokio handle). Any runtime flavour works; there is noblock_in_placebridge.
Outcomes
| Effect outcome | HTTP response |
|---|---|
success A | A::into_response() |
typed failure E | E::into_response() |
| defect (a panic in the build closure or the effect) | 500, with a HandlerDefect response extension (the message is never sent) |
| interrupt | 503 |
When the client disconnects, the request fiber is interrupted (DropPolicy::Interrupt, the
default), so its finalizers (ensuring, acquire_release, scoped layers) run.
DropPolicy::Detach lets it run to completion. EffectExecutor::builder() also sets
max_in_flight(n) with SaturationPolicy::{Wait, Reject} (503).
API surface
routing::get/post/put/patch/delete/on— method routers forFn(&mut S) -> Effect<A, E, S>handlers;*_with_metrics(Counter, Histogram, f)variants.execute(State(s), build)— inside your ownasynchandler: run one effect and turn itsExitinto aResponse.run_with_env(s, build)/run_with_caps(State<DynEnv>, build)— run one effect and get theExit<A, E>back (into_result()forResult<A, Cause<E>>).RequestCaps+CapOverrideLayer— per-request capabilities over aDynEnv(execute_with_request_caps).route— typed routes:Route::get(..).public().path::<P>().query::<Q>().body::<B>().handler(f)decodes every request part through aSchemabeforefruns, and answers invalid input with an RFC 9457Problem(422,application/problem+json) listing every issue with a JSON Pointer. Every route declares its access —.public()or.guard(g)— orroute::Router::routedoes not compile it.route::Routerkeeps a registry of the routes;ValidPath/ValidQuery/ValidHeaders/ValidJson/ValidFormdo the same decoding in plain axum handlers. See Typed routes and validation.json::decode_json— decode a JSON body with aSchema, failing with the sameProblem(it replacesdecode_json_schema, removed in 0.11).body::{stream_body, body_stream}— response bodies from aStream<Bytes, E>(dropping the body interrupts the producer), request bodies as a stream.server— the server as a scoped layer:server_layer(app),serve(app),serve_until_shutdown(app)(SIGINT/SIGTERM, graceful drain),HostConfigfromid_effect_config.
Typed capabilities as router state
From examples/020_capability_run_with.rs:
#![allow(unused)] fn main() { use axum::Router; use id_effect::{Effect, HCons, HNil, hlist, need}; use id_effect_axum::routing; use std::convert::Infallible; #[derive(Clone, Copy, Debug)] struct Counter(u32); type AppEnv = HCons<Counter, HNil>; fn handler(_env: &mut AppEnv) -> Effect<String, Infallible, AppEnv> { need::<Counter>().map(|c| format!("count={}", c.0)) } let app = Router::new() .route("/", routing::get(handler)) .with_state(hlist![Counter(7)]); let _: Router = app; }
Serving with a layer
The server's lifetime is a scoped layer, so it composes with the rest of the application's layers and shuts down gracefully when the program ends:
#![allow(unused)] fn main() { use id_effect_axum::{EffectExecutor, HostConfig, HostError, provide_host_config_env, serve_until_shutdown}; async fn run(app: axum::Router) { let config = provide_host_config_env::<HostError>().provide_to(HostConfig::layer()); let program = serve_until_shutdown(app).provide_layer(config); let exit = EffectExecutor::global().run_effect(program).await; } }
examples/http-showcase is a complete service built this way (config, Postgres, metrics,
OpenTelemetry, graceful shutdown).
Further reading
- Examples:
cargo run -p id_effect_axum --example 010_routing_hello(also020–071), and080_typed_routesfor typed routes - Typed routes and validation — schemas per request part, Problem+JSON errors, the route registry
- RPC boundaries —
id_effect_rpcenvelopes, correlation ids, tracing - Tower service for generic
Servicecomposition without Axum cargo doc --open -p id_effect_axum