January 2019
Intermediate to advanced
520 pages
14h 32m
English
The actix-web crate supports middleware that can be attached to an App instance to process every request and response. Middleware helps to log requests, transform them, or even control access to a group of paths using regular expressions. Consider middleware as handlers for all incoming requests and outgoing responses. To create the middleware, we first have to implement the Middleware trait for it. Look at the following code:
pub struct Counter;impl Middleware<State> for Counter { fn start(&self, req: &HttpRequest<State>) -> Result<Started> { let value = *req.state().0.borrow(); *req.state().0.borrow_mut() = value + 1; Ok(Started::Done) } fn response(&self, _req: &HttpRequest<State>, resp: HttpResponse) -> Result<Response> {Read now
Unlock full access