amoeba
A lightweight, single-threaded HTTP API framework for Rust. Attach handlers to routes and carry state through a typed context.
Philosophy
- Single-threaded — no
Arc, noMutex, no surprise contention. Requests are handled sequentially. - Typed context — your application state flows through every handler as
&mut C. - No external dependencies — built entirely on
std. - Offload heavy work — a slow handler stalls every subsequent request. Hand CPU-intensive or blocking work off to a separate thread or service.
Quick start
use *;
;
Routing
Routes are created with Route::new(method, path, handler). The method is any HTTP method string ("GET", "POST", "PUT", "DELETE", etc.). All routes use exact path matching.
Handler signature:
The server returns 404 when no route matches the path and 405 when the path matches but the method does not.
Request
Header keys are normalized to lowercase. The body is populated only when a Content-Length header is present; if absent, body is None. Access body bytes via body.data and its content type via body.content_type.
Response
Response::new() defaults to status 204 No Content. Use builder methods to set a status and optional body:
new
.body
.status
new
.status
.header
.body
Responses with a body automatically include Content-Type and Content-Length headers.
Errors
HttpError::new(message) is the standard error type. It defaults to status 500; use .status(code) to override:
new // 500
new.status // 404
new.status // 400
The server automatically converts an HttpError returned from a handler into a plain-text HTTP response with the given status.
Middleware
Middleware intercepts a request before it reaches the handler. It receives &mut C and Request and either returns a (possibly modified) Request to continue the chain, or an Err(HttpError) to short-circuit.
Register with a specific method and path, or "*" for either dimension to match all:
// Applies only to POST /echo
.middleware
// Applies to every request
.middleware
Middleware runs in registration order.
Examples
examples/counter/— stateful counter (increment, decrement, read)examples/echo/— middleware-validated echo endpointexamples/users/— request body parsing and mutable collection state
cargo run --example counter
cargo run --example echo
cargo run --example users