Embedding
Running an Ulo application inside axum, actix-web, poem, rocket or salvo, and what each host declares it cannot do.
An embedding runs the app inside another framework's server. The host binds, accepts and parses. The app answers under a path the host mounts it at, or as the host's fallback, and the host keeps its own routes, middleware and server settings.
Routing, extraction, pre-dispatch, dispatch and error rendering run inside the app either way, so controllers move between a server crate and a host without change.
| Host | Crate | Mounted as |
|---|---|---|
| axum | ulo-http-axum | a tower::Service: nest_service or fallback_service |
| actix-web | ulo-http-actix | an HttpServiceFactory: App::service(scope(..)) |
| poem | ulo-http-poem | a poem::Endpoint: Route::nest |
| rocket | ulo-http-rocket | catch-all routes, one per method: rocket.mount(..) |
| salvo | ulo-http-salvo | a salvo::Handler, nested or as the catch-all goal |
Example
examples/21-http-in-axum answers /api/* from an Ulo app and /health from axum, on one socket.
use axum::Router;
use axum::routing::get;
use ulo_http_axum::{Embedded, Handle};
/// The embedding server the app binds to: it binds no socket, the host owns them.
pub fn embedded() -> Embedded {
Embedded::new().nested_at("/api")
}
/// The host's router: its own route beside the app, mounted at `/api`.
pub fn router(app: &Handle) -> Router {
Router::new().route("/health", get(|| async { "axum is up" })).nest_service("/api", app.service())
}#[tokio::main]
async fn main() -> Result<(), ulo::BoxError> {
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".into());
let server = ex_21_http_in_axum::embedded();
let embedded = server.handle();
let app = ex_21_http_in_axum::builder().runtime(Tokio::current()).wire()?.connect().await?.bind(server).listen().await?;
let listener = tokio::net::TcpListener::bind(format!("127.0.0.1:{port}")).await?;
println!("listening on http://{}", listener.local_addr()?);
let host = axum::serve(listener, ex_21_http_in_axum::router(&embedded));
ulo_http_axum::run(app, &embedded, host, shutdown_signal()).await?;
Ok(())
}AppModule in the host pages' mounting snippets is the application's root module. Routes in the controller are written as in any Ulo app. The host strips /api before the app sees the path, and nested_at tells the app which prefix that was.
The order is the same as for a server: Embedded::new() is the server given to bind, and server.handle() is taken before bind consumes it. listen still prepares the server: it builds the route table, checks CORS and path parameters, and checks the app against the host's declared limits. Nothing is bound, so app.addresses() is empty. Port 0, inherited sockets and TLS are set on the host.
One owner of shutdown
The app's serve(signal) owns shutdown. Each adapter exports a run function that takes the host's server future, attaches the host's graceful stop to the app's drain, and awaits app.serve(signal). A host future that ends before shutdown began is an error, and the app shuts down naming the transport. Do not give the host a second shutdown signal.
The host's graceful stop starts when the app begins to drain. Connections the host still holds afterwards are closed by the app's close, under the core's close bound.
Settings on Embedded
Embedded carries the app-level settings of a server and three the adapter reads:
| Method | Effect |
|---|---|
max_body(bytes) | As on a server: 2 MiB unset. |
max_inflight(Count) | As on a server: 1,024 unset. |
shed_retry_after(Duration) | As on a server. |
challenge(..) | As on a server. |
timeout_grace(Bound) | As on a server. |
nested_at(prefix) | The prefix the host mounts the app at. Normalised to a leading / and no trailing one. A prefix carrying a parameter is refused in prepare. |
peer_addr(bool) | Says the host supplies the peer address, for a host whose adapter does not declare it. |
on_miss(Miss) | Miss::Final (unset) answers 404 itself. Miss::Forward hands an unmatched request back to the host where the adapter can. |
forward::<T>(copy) | Copies a value from the host's request into the app's request extensions, for hosts whose request store does not reach the app. |
The host's own settings (endpoint, TLS, h2c, max_concurrent_streams, header_timeout, handshake_timeout) have no method on Embedded: writing one is a compile error and is never silently ignored. A zero max_inflight or timeout_grace is refused in prepare, as on a server.
What each host declares
Each adapter declares what the host cannot do, and listen checks the app against it. A handler that needs something the host lacks fails startup as Configure, naming the limit.
| axum | actix-web | poem | rocket | salvo | |
|---|---|---|---|---|---|
| Host strips the mount prefix | yes | no, the app strips it | yes | no, the app strips it | no, the app strips it |
| WebSocket gateways on the HTTP port | yes | no | yes | yes (HTTP/1.1 assumed) | yes |
| Peer address | only with .peer_addr(true) | yes | yes | yes | yes |
Miss::Forward | no | no | no | yes | no |
| Host extensions reach the app | yes | no, use forward | yes | no, use forward | yes |
TLS info in ConnInfo | no | no | no | no | no |
| Request body | streamed | streamed | streamed | buffered up to max_body | streamed |
| Call dropped when the peer closes or resets before the response head | yes | no, it runs to its answer | HTTP/2 reset only | no, it runs to its answer | yes |
| Abandoned response noticed | at close | at the next failed write | at the next failed write | at the next failed write | at close |
Where a host cannot supply the peer address, a handler reading Dep<ClientAddr> without Option is refused at startup, unless .peer_addr(true) says the host provides it. Where host extensions do not reach the app, a handler reading Host<T> is refused unless a forward copy supplies T.
Drain behaviour also differs:
| axum | actix-web | poem | rocket | salvo | |
|---|---|---|---|---|---|
| Request whose head is still arriving at the drain | answered 503 | connection closed, no answer | answered 503 | answered 503 | answered 503 |
| HTTP/2 connection at the drain | GOAWAY | no GOAWAY, reset at the stop deadline | GOAWAY | GOAWAY | GOAWAY |
| Client abandons a response during the drain | released when the host drops it | released when the host drops it | released when the host drops it | holds the whole drain window | released when the host drops it |
The drain rows are the adapters' declared DrainPending, DrainHttp2 and DrainAbandoned values. The per-host pages explain each.
Routing in the response
The app writes Routing into the response's extensions so host middleware can read which route matched. Poem and salvo keep it in their response extensions, actix copies only Routing into its own store, and rocket keeps it in the request's local cache. The per-host pages give the lookup.
Conformance
ulo-http-conformance runs one scenario list against the hyper server and against every adapter, nested and as fallback. It asserts byte-identical responses wherever a scenario depends on no declared limit, and checks each declared limit in both directions: a host that passes a scenario it declares unsupported fails.