Recipe

Hyper (Rust) Primer

Hyper is the low-level HTTP library that powers most of the Rust web ecosystem. This primer walks you through the core concepts you need to ship a Meridian-compatible service in Rust without reaching for a heavyweight framework.

1. Service and MakeService

Hyper is built around two traits: Service turns a request into a response future, and MakeService constructs a fresh Service per connection. Most apps use service_fn to skip the boilerplate.

2. Minimal Server

Hyper 1.x decoupled the server runtime from Tokio. You wire it up with hyper_util and a Tokio listener. Here is the canonical shape for a Meridian gateway proxy:

use hyper::{Request, Response, body::Incoming};
use hyper_util::rt::TokioIo;
use hyper_util::server::conn::auto;
use tokio::net::TcpListener;

async fn handle(_req: Request<Incoming>)
    -> Result<Response<String>, hyper::Error>
{
    Ok(Response::new("hello meridian".into()))
}

#[tokio::main]
async fn main() {
    let listener = TcpListener::bind("0.0.0.0:8080")
        .await.unwrap();
    loop {
        let (stream, _) = listener.accept().await.unwrap();
        let io = TokioIo::new(stream);
        tokio::spawn(async move {
            auto::Builder::new(hyper_util::rt::TokioExecutor::new())
                .serve_connection(io, hyper::service::service_fn(handle))
                .await.ok();
        });
    }
}

3. Streaming Bodies

Meridian endpoints stream tokens as they arrive. Hyper's body type implements http_body::Body, so you wrap a channel or an async stream and return it directly. Use http_body_util::StreamBody for the easy path, or implement Body by hand for backpressure control.