HTTP Servers
The two ways to serve an Ulo application over HTTP, a server crate that owns the sockets or a host framework that does, and how to choose between them.
ulo-http implements the routing, extraction, pre-dispatch, error rendering and the HTTP wire behaviour. It owns no socket. Something else accepts connections, and that choice is made in main.
The two shapes
| Server crate | Embedding | |
|---|---|---|
| Who owns the sockets | the app | the host framework |
| Crates | ulo-http-hyper | ulo-http-axum, ulo-http-actix, ulo-http-poem, ulo-http-rocket, ulo-http-salvo |
| Bound with | app.bind(Server::new("0.0.0.0:8080")) | app.bind(Embedded::new()), then the host mounts embedded.service() |
| TLS, h2c, endpoints, header timeouts | set on the server | the host's |
| Routes of its own beside the app | no | yes |
Pick the server crate unless an existing host framework must keep its sockets, its middleware or its other routes. ulo-http-hyper is the reference: one conformance suite (ulo-http-conformance) checks every other crate in the table against the behaviour of the reference, and the differences that remain are declared by each adapter and listed on the pages below.
A server crate needs two further choices, a runtime and a listener, and both follow from one decision: tokio or smol. Runtimes covers the pairing. TLS applies to server crates only.
Starting a server
Every transport starts the same way. The server passed to bind is the line that differs between shapes (examples/01-hello):
use ulo_http_hyper::Server;
use ulo_runtime_tokio::{Tokio, shutdown_signal};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let port = std::env::var("PORT").unwrap_or_else(|_| "3000".into());
let app = ex_01_hello::builder()
.runtime(Tokio::current())
.wire()?
.connect()
.await?
.bind(Server::new(format!("127.0.0.1:{port}")))
.listen()
.await?;
for bound in app.addresses() {
println!("listening on http://{}", bound.addr);
}
app.serve(shutdown_signal()).await?;
Ok(())
}wire, connect, bind and listen are the application phases, and ex_01_hello is the example crate whose builder() returns the configured AppBuilder. listen prepares every server first and reports all configuration failures together, before any port is bound. A bad address, an unreadable certificate or a zero limit is a StartupError::Configure, never a port conflict.
Pages
ulo-http-hyper: the reference server, its settings and limits.- TLS: PEM files, a custom rustls configuration, ALPN.
- Runtimes: tokio and smol, and what changes between them.
- Embedding: how an embedding works, and what each host declares it cannot do.
- axum, actix-web, salvo, poem, rocket: one page per host.
- Writing a server crate: the
BackendandListenertraits.
Handlers, extractors and enhancers do not depend on the choice: see Request lifecycle and Extractors.