pub struct ServerMechanism { /* private fields */ }Expand description
Entry point for building an HTTP route.
Pairs an HTTP method with a URL path and acts as the root of a fluent builder chain.
Optionally attach shared state, a JSON body expectation, or URL query parameter
deserialisation — then finalise with onconnect (async)
or onconnect_sync (sync) to produce a
SocketType ready to be mounted on a Server.
§Example
// Plain GET — no body, no state
let health = ServerMechanism::get("/health")
.onconnect(|| async { reply!() });
// POST — JSON body deserialised into `CreateItem`
let create = ServerMechanism::post("/items")
.json::<CreateItem>()
.onconnect(|body| async move {
let item = Item { id: 1, name: body.name };
reply!(json => item, status => Status::Created)
});
// GET — shared counter state injected on every request
let counter: Arc<Mutex<u64>> = Arc::new(Mutex::new(0));
let count_route = ServerMechanism::get("/count")
.state(counter.clone())
.onconnect(|state| async move {
let n = *state.lock().unwrap();
reply!(json => n)
});Implementations§
Source§impl ServerMechanism
impl ServerMechanism
Sourcepub fn get(path: impl Into<String>) -> Self
pub fn get(path: impl Into<String>) -> Self
Creates a route matching HTTP GET requests at path.
Sourcepub fn post(path: impl Into<String>) -> Self
pub fn post(path: impl Into<String>) -> Self
Creates a route matching HTTP POST requests at path.
Sourcepub fn put(path: impl Into<String>) -> Self
pub fn put(path: impl Into<String>) -> Self
Creates a route matching HTTP PUT requests at path.
Sourcepub fn delete(path: impl Into<String>) -> Self
pub fn delete(path: impl Into<String>) -> Self
Creates a route matching HTTP DELETE requests at path.
Sourcepub fn patch(path: impl Into<String>) -> Self
pub fn patch(path: impl Into<String>) -> Self
Creates a route matching HTTP PATCH requests at path.
Sourcepub fn head(path: impl Into<String>) -> Self
pub fn head(path: impl Into<String>) -> Self
Creates a route matching HTTP HEAD requests at path.
Sourcepub fn options(path: impl Into<String>) -> Self
pub fn options(path: impl Into<String>) -> Self
Creates a route matching HTTP OPTIONS requests at path.
Sourcepub fn state<S: Clone + Send + Sync + 'static>(
self,
state: S,
) -> StatefulSocketBuilder<S>
pub fn state<S: Clone + Send + Sync + 'static>( self, state: S, ) -> StatefulSocketBuilder<S>
Attaches shared state S to this route, transitioning to StatefulSocketBuilder.
A fresh clone of S is injected into the handler on every request.
Sourcepub fn json<T: DeserializeOwned + Send>(self) -> JsonSocketBuilder<T>
pub fn json<T: DeserializeOwned + Send>(self) -> JsonSocketBuilder<T>
Declares that this route expects a JSON-encoded request body, transitioning to
JsonSocketBuilder.
Sourcepub fn query<T: DeserializeOwned + Send>(self) -> QuerySocketBuilder<T>
pub fn query<T: DeserializeOwned + Send>(self) -> QuerySocketBuilder<T>
Declares that this route extracts its input from URL query parameters, transitioning
to QuerySocketBuilder.
Sourcepub fn encryption<T>(self, key: SerializationKey) -> EncryptedBodyBuilder<T>
pub fn encryption<T>(self, key: SerializationKey) -> EncryptedBodyBuilder<T>
Declares that this route expects an authenticated-encrypted request body
(ChaCha20-Poly1305), transitioning to EncryptedBodyBuilder.
Sourcepub fn encrypted_query<T>(
self,
key: SerializationKey,
) -> EncryptedQueryBuilder<T>
pub fn encrypted_query<T>( self, key: SerializationKey, ) -> EncryptedQueryBuilder<T>
Declares that this route expects authenticated-encrypted URL query parameters
(ChaCha20-Poly1305), transitioning to EncryptedQueryBuilder.
Sourcepub fn onconnect<F, Fut, Re>(self, handler: F) -> SocketType
pub fn onconnect<F, Fut, Re>(self, handler: F) -> SocketType
Finalises this route with an async handler that receives no arguments.
Returns a SocketType ready to be passed to Server::mechanism.
§Example
let route = ServerMechanism::get("/ping")
.onconnect(|| async {
reply!(json => Pong { ok: true })
});Sourcepub unsafe fn onconnect_sync<F, Re>(self, handler: F) -> SocketType
pub unsafe fn onconnect_sync<F, Re>(self, handler: F) -> SocketType
Finalises this route with a synchronous handler that receives no arguments.
§Safety
Every incoming request spawns an independent task on Tokio’s blocking thread pool. The pool caps the number of live OS threads (default 512), but the queue of waiting tasks is unbounded — under a traffic surge, tasks accumulate without limit, consuming unbounded memory and causing severe latency spikes or OOM crashes. Additionally, any panic inside the handler is silently converted into a 500 response, masking runtime errors. Callers must ensure the handler completes quickly and that adequate backpressure or rate limiting is applied externally.