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 from the Content-Length header; if absent, body is None.
Response
Shorthand constructors:
ok // 200
created // 201
no_content // 204
bad_request // 400
unauthorized // 401
forbidden // 403
not_found // 404
internal_server_error // 500
Builder-style for custom status codes or headers:
new
.status_code
.header
.body
The body argument can be a String, &str, Html(...), or Json(...).
JSON
Implement IntoJson to serialize a type as a response body:
use ;
JsonValue variants: JsonNull, JsonBool(bool), JsonChar(char), JsonUint(u64), JsonInt(i64), JsonFloat(f64), JsonString(String), JsonList(Vec<JsonValue>), JsonObject(Vec<(String, JsonValue)>).
HTML
use Html;
Errors
HttpError::new(status_code, detail) is the standard error type. Any handler can return it; the server automatically converts it to a JSON response:
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.
use ;
Register with "*" to apply globally or an exact path to apply only to that route:
new
.middleware
.route
.run;
Middleware runs in registration order.
Examples
examples/simple/— minimal single-route serverexamples/counter/— stateful counter with an HTML UI and JSON responsesexamples/json/— nested struct serialization viaIntoJsonexamples/middleware/— global API-key auth middlewareexamples/poll/— multi-option poll with live vote totals and an HTML UI
cargo run --example simple
cargo run --example counter
cargo run --example json
cargo run --example middleware
cargo run --example poll