[−][src]Trait hreq::server::StateMiddleware
Trait for middleware that use a state.
Typically this trait is not used directly since there is a blanket implementation for any function that matches this signature:
ⓘThis example is not tested
async fn my_middleware( state: MyState, req: Request<Body>, next: Next ) -> impl Into<Reply> { ... }
Reply
is not a type you would use in your own type signatures. impl Into<Reply>
represents a whole range of (concrete) possible return types. See Reply
for more details.
Examples
use hreq::Error; use hreq::prelude::*; use hreq::server::Next; use std::sync::{Arc, Mutex}; #[derive(Clone)] struct MyState(Arc<Mutex<String>>); fn main() { let state = MyState(Arc::new(Mutex::new("Hello".to_string()))); let mut server = Server::with_state(state); server.at("/path") .with_state() .middleware(my_middle) .get(|state, req| async { "Hello" }); } async fn my_middle( state: MyState, req: Request<Body>, next: Next, ) -> Result<Response<Body>, Error> { // Do things with request here. // Continue the request chain. let res = next.run(req).await?; // Do things with the response here. Ok(res) }
Required methods
fn call<'a>(
&'a self,
state: State,
req: Request<Body>,
next: Next
) -> Pin<Box<dyn Future<Output = Reply> + Send + 'a>>
&'a self,
state: State,
req: Request<Body>,
next: Next
) -> Pin<Box<dyn Future<Output = Reply> + Send + 'a>>
Call the middleware.
Implementors
impl<State, F: Send + Sync + 'static, Fut, Ret> StateMiddleware<State> for F where
F: Fn(State, Request<Body>, Next) -> Fut,
Fut: Future<Output = Ret> + Send + 'static,
Ret: Into<Reply>,
[src]
F: Fn(State, Request<Body>, Next) -> Fut,
Fut: Future<Output = Ret> + Send + 'static,
Ret: Into<Reply>,