pub trait MiddlewareLayer:
Send
+ Sync
+ 'static {
// Required methods
fn call(
&self,
req: Request,
next: BoxedNext,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'static>>;
fn clone_box(&self) -> Box<dyn MiddlewareLayer>;
}Expand description
Trait for middleware that can be applied to RustAPI
This trait allows both Tower layers and custom middleware to be used
with the .layer() method.
§Example: Implementing a custom simple logger middleware
use rustapi_core::middleware::{MiddlewareLayer, BoxedNext};
use rustapi_core::{Request, Response};
use std::pin::Pin;
use std::future::Future;
#[derive(Clone)]
struct SimpleLogger;
impl MiddlewareLayer for SimpleLogger {
fn call(
&self,
req: Request,
next: BoxedNext,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'static>> {
Box::pin(async move {
println!("Incoming request: {} {}", req.method(), req.uri());
let response = next(req).await;
println!("Response status: {}", response.status());
response
})
}
fn clone_box(&self) -> Box<dyn MiddlewareLayer> {
Box::new(self.clone())
}
}Required Methods§
Sourcefn call(
&self,
req: Request,
next: BoxedNext,
) -> Pin<Box<dyn Future<Output = Response> + Send + 'static>>
fn call( &self, req: Request, next: BoxedNext, ) -> Pin<Box<dyn Future<Output = Response> + Send + 'static>>
Apply this middleware to a request, calling next to continue the chain
Sourcefn clone_box(&self) -> Box<dyn MiddlewareLayer>
fn clone_box(&self) -> Box<dyn MiddlewareLayer>
Clone this middleware into a boxed trait object
Trait Implementations§
Source§impl Clone for Box<dyn MiddlewareLayer>
impl Clone for Box<dyn MiddlewareLayer>
Source§impl Extend<Box<dyn MiddlewareLayer>> for LayerStack
impl Extend<Box<dyn MiddlewareLayer>> for LayerStack
Source§fn extend<T: IntoIterator<Item = Box<dyn MiddlewareLayer>>>(&mut self, iter: T)
fn extend<T: IntoIterator<Item = Box<dyn MiddlewareLayer>>>(&mut self, iter: T)
Extends a collection with the contents of an iterator. Read more
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
🔬This is a nightly-only experimental API. (
extend_one)Extends a collection with exactly one element.
Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
🔬This is a nightly-only experimental API. (
extend_one)Reserves capacity in a collection for the given number of additional elements. Read more
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".