Expand description
Typed, fluent HTTP server construction.
This module provides a builder-oriented API for declaring HTTP routes and
serving them with warp under the hood. 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 with a single
.await. Graceful shutdown is available via
Server::serve_with_graceful_shutdown and Server::serve_from_listener.
§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<()> (VEIL-decrypted body) |
.encrypted_query::<T>(key).onconnect(f) | T: bincode::Decode<()> (VEIL-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) — VEIL-decrypted body |
.state(s).encrypted_query::<T>(key).onconnect(f) | (S, T) — VEIL-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:
// Fluent form:
// server.mechanism(
// ServerMechanism::post("/items")
// .json::<NewItem>()
// .onconnect(|body: NewItem| async move {
// reply!(json => Item { id: 1, name: body.name }, status => Status::Created)
// })
// );
// Equivalent attribute form:
#[mechanism(server, POST, "/items", json)]
async fn create(body: NewItem) {
reply!(json => Item { id: 1, name: body.name }, status => Status::Created)
}See the mechanism item for the full syntax reference.
§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§
- Encrypted
Body Builder - Route builder that expects a VEIL-encrypted request body of type
T. - Encrypted
Query Builder - Route builder that expects VEIL-encrypted URL query parameters of type
T. - 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. - Server
- The HTTP server that owns and dispatches a collection of
SocketTyperoutes. - Server
Mechanism - Entry point for building an HTTP route.
- Stateful
Encrypted Body Builder - Route builder carrying shared state
Sand a VEIL-encrypted request body of typeT. - Stateful
Encrypted Query Builder - Route builder carrying shared state
Sand VEIL-encrypted query parameters of typeT. - 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.
Functions§
- reply
- Returns an empty
200 OKwarp reply 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 warp result. - reply_
with_ json - Serialises
jsonas a JSON body and returns it as a200 OKwarp reply result. - reply_
with_ status - Wraps
replywith the given HTTPstatuscode and returns it as a warp result. - reply_
with_ status_ and_ json - Serialises
jsonas a JSON body, attaches the given HTTPstatus, and returns a warp result.
Type Aliases§
- Socket
Type - A fully assembled, type-erased HTTP route ready to be registered on a
Server.
Attribute Macros§
- mechanism
- Concise route declaration for
toolkit-zerosocket-server routes.