ulo
ulo
Documentation

Start here

Coming from NestJSNestJS topics, mapped

The model

Protocols

HTTP Serversulo-http-hyperTLSRuntimesEmbeddingaxumactix-websalvopoemrocketWriting a server crate

Integrations

Operating

Reference

HTTP Servers

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.

HostCrateMounted as
axumulo-http-axuma tower::Service: nest_service or fallback_service
actix-webulo-http-actixan HttpServiceFactory: App::service(scope(..))
poemulo-http-poema poem::Endpoint: Route::nest
rocketulo-http-rocketcatch-all routes, one per method: rocket.mount(..)
salvoulo-http-salvoa 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:

MethodEffect
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.

axumactix-webpoemrocketsalvo
Host strips the mount prefixyesno, the app strips ityesno, the app strips itno, the app strips it
WebSocket gateways on the HTTP portyesnoyesyes (HTTP/1.1 assumed)yes
Peer addressonly with .peer_addr(true)yesyesyesyes
Miss::Forwardnononoyesno
Host extensions reach the appyesno, use forwardyesno, use forwardyes
TLS info in ConnInfononononono
Request bodystreamedstreamedstreamedbuffered up to max_bodystreamed
Call dropped when the peer closes or resets before the response headyesno, it runs to its answerHTTP/2 reset onlyno, it runs to its answeryes
Abandoned response noticedat closeat the next failed writeat the next failed writeat the next failed writeat 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:

axumactix-webpoemrocketsalvo
Request whose head is still arriving at the drainanswered 503connection closed, no answeranswered 503answered 503answered 503
HTTP/2 connection at the drainGOAWAYno GOAWAY, reset at the stop deadlineGOAWAYGOAWAYGOAWAY
Client abandons a response during the drainreleased when the host drops itreleased when the host drops itreleased when the host drops itholds the whole drain windowreleased 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.

Runtimes

The runtime and listener crates a server is built from, with tokio and smol side by side.

axum

Running an Ulo application inside an axum server with ulo-http-axum.

On this page

ExampleOne owner of shutdownSettings on EmbeddedWhat each host declaresRouting in the responseConformance