Expand description
Typed, fluent HTTP server construction.
This module provides a builder-oriented API for declaring HTTP routes and
serving them with a built-in hyper-based HTTP engine. The entry point for every
route is ServerMechanism, which pairs an HTTP method with a URL path and
supports incremental enrichment — attaching a JSON body expectation, URL
query parameter deserialisation, or shared state — before being finalised
into a SocketType route handle via onconnect.
Completed routes are registered on a Server and served via one of the
serve* methods, each of which returns a ServerFuture. A ServerFuture
can be .await’d to run the server inline or called .background() on to
spawn it as a background Tokio task and get a tokio::task::JoinHandle back.
Graceful shutdown is available via
Server::serve_with_graceful_shutdown and Server::serve_from_listener.
For runtime address migration, use Server::serve_managed: it starts
the server immediately and returns a BackgroundServer handle that supports
BackgroundServer::rebind (graceful shutdown + restart on a new address,
all existing routes preserved) and BackgroundServer::stop.
§Hot-reloading routes
BackgroundServer::mechanism pushes a new route into the running
server’s shared route table without any restart or port gap. Because
routes are stored in an Arc<RwLock<Vec<SocketType>>> shared between the
caller and the server loop, the new route becomes visible to the next
incoming request immediately.
§Builder chains at a glance
| Chain | Handler receives |
|---|---|
ServerMechanism::method(path).onconnect(f) | nothing |
.json::<T>().onconnect(f) | T: DeserializeOwned |
.query::<T>().onconnect(f) | T: DeserializeOwned |
.encryption::<T>(key).onconnect(f) | T: bincode::Decode<()> (decrypted body) |
.encrypted_query::<T>(key).onconnect(f) | T: bincode::Decode<()> (decrypted query) |
.state(s).onconnect(f) | S: Clone + Send + Sync |
.state(s).json::<T>().onconnect(f) | (S, T) |
.state(s).query::<T>().onconnect(f) | (S, T) |
.state(s).encryption::<T>(key).onconnect(f) | (S, T) — decrypted body |
.state(s).encrypted_query::<T>(key).onconnect(f) | (S, T) — decrypted query |
For blocking handlers (not recommended in production) every finaliser also
has an unsafe onconnect_sync counterpart.
§#[mechanism] attribute macro
As an alternative to spelling out the builder chain by hand, the
mechanism attribute macro collapses the entire
server.mechanism(ServerMechanism::method(path) … .onconnect(handler)) call
into a single decorated async fn.
§Response helpers
Use the [reply!] macro as the most concise way to build a response:
// 200 OK, empty body
// reply!()
// 200 OK, JSON body
// reply!(json => item)
// 201 Created, JSON body
// reply!(json => item, status => Status::Created)Re-exports§
pub use super::SerializationKey;
Structs§
- Background
Server - A managed, background HTTP server returned by
Server::serve_managed. - Empty
Reply - An empty
200 OKreply — returned byreply!(). - Encrypted
Body Builder - Route builder that expects an authenticated-encrypted request body of type
T(ChaCha20-Poly1305). - Encrypted
Query Builder - Route builder that expects authenticated-encrypted URL query parameters of type
T(ChaCha20-Poly1305). - Html
Reply - An HTML body reply — returned by
html_reply. - Json
Socket Builder - Route builder that expects and deserialises a JSON request body of type
T. - Query
Socket Builder - Route builder that expects and deserialises URL query parameters of type
T. - Rejection
- An HTTP-level error value returned from route handlers.
- Server
- The HTTP server that owns and dispatches a collection of
SocketTyperoutes. - Server
Future - Opaque future returned by
Server::serve,Server::serve_with_graceful_shutdown, andServer::serve_from_listener. - Server
Mechanism - Entry point for building an HTTP route.
- Socket
Type - A fully assembled, type-erased HTTP route ready to be registered on a
Server. - Stateful
Encrypted Body Builder - Route builder carrying shared state
Sand an authenticated-encrypted request body of typeT(ChaCha20-Poly1305). - Stateful
Encrypted Query Builder - Route builder carrying shared state
Sand authenticated-encrypted query parameters of typeT(ChaCha20-Poly1305). - Stateful
Json Socket Builder - Route builder that carries shared state
Sand expects a JSON body of typeT. - Stateful
Query Socket Builder - Route builder that carries shared state
Sand expects URL query parameters of typeT. - Stateful
Socket Builder - Route builder that carries shared state
Swith no body or query expectation.
Enums§
- Status
- A collection of common HTTP status codes used with the reply helpers.
Traits§
- Reply
- A value that can be converted into a fully-formed HTTP response.
Functions§
- forbidden
- Returns a
403 Forbiddenrejection. - html_
reply - Wraps
contentinto a200 OKHTML response. - reply
- Returns an empty
200 OKreply result. - reply_
sealed - Seals
valuewithkeyand returns it as anapplication/octet-streamresponse (200 OK). - reply_
sealed_ with_ status - Seals
valuewithkey, attaches the given HTTPstatus, and returns it as a result. - reply_
with_ json - Serialises
jsonas a JSON body and returns it as a200 OKreply result. - reply_
with_ status - Wraps
replywith the given HTTPstatuscode and returns it as a result. - reply_
with_ status_ and_ json - Serialises
jsonas a JSON body, attaches the given HTTPstatus, and returns a result.
Attribute Macros§
- mechanism
- Declare a server-side route.