axum
Running an Ulo application inside an axum server with ulo-http-axum.
ulo-http-axum is the one tower embedding. The host hands the app a tower::Service, and the app is mounted with nest_service or fallback_service. It builds against axum 0.8 with the tokio, http1, http2 and original-uri features, and takes the workspace MSRV, 1.88.
examples/21-http-in-axum is the complete program; see Embedding.
Mounting
use tower::Layer;
use ulo::App;
let server = ulo_http_axum::Embedded::new().nested_at("/api").peer_addr(true);
let embedded = server.handle();
let app = App::builder(AppModule).runtime(ulo_runtime_tokio::Tokio::current()).wire()?.connect().await?.bind(server).listen().await?;
let router = axum::Router::new()
.nest_service("/api", ulo_http_axum::HostLayer.layer(embedded.service()))
.into_make_service_with_connect_info::<SocketAddr>();
ulo_http_axum::run(app, &embedded, axum::serve(listener, router), ulo_runtime_tokio::shutdown_signal()).await?;(From the crate documentation of ulo-http-axum.)
nest_service strips the prefix before the app sees the path. HostLayer, wrapped around the app's service, puts what axum keeps elsewhere into the request's extensions: a ConnInfo from ConnectInfo<SocketAddr> and the OriginalPath from OriginalUri. Handle::service() without the layer, as in example 21, still serves, but the app then sees no peer address.
ulo_http_axum::run takes axum::serve::Serve rather than a version with graceful shutdown already attached, so the app's drain is the only shutdown owner. axum's drain has no bound of its own, and the app's close bound limits it.
What it declares
- The peer address only through
ConnectInfo.peer_addrisfalseunless.peer_addr(true)is set and the router is served withinto_make_service_with_connect_info::<SocketAddr>(). - WebSocket upgrades through hyper's
OnUpgrade, so gateways on the HTTP port work. - No miss forwarding, no TLS information, streamed request and response bodies.
- A dropped response body is observed at the disconnect.
- GOAWAY on HTTP/2 at the drain, and a request still arriving is answered 503.