Trait poem::middleware::Middleware
source · [−]pub trait Middleware<E: Endpoint> {
type Output: Endpoint;
fn transform(&self, ep: E) -> Self::Output;
}
Expand description
Represents a middleware trait.
Create you own middleware
use poem::{handler, web::Data, Endpoint, EndpointExt, Middleware, Request, Result};
/// A middleware that extract token from HTTP headers.
struct TokenMiddleware;
impl<E: Endpoint> Middleware<E> for TokenMiddleware {
type Output = TokenMiddlewareImpl<E>;
fn transform(&self, ep: E) -> Self::Output {
TokenMiddlewareImpl { ep }
}
}
/// The new endpoint type generated by the TokenMiddleware.
struct TokenMiddlewareImpl<E> {
ep: E,
}
const TOKEN_HEADER: &str = "X-Token";
/// Token data
struct Token(String);
#[poem::async_trait]
impl<E: Endpoint> Endpoint for TokenMiddlewareImpl<E> {
type Output = E::Output;
async fn call(&self, mut req: Request) -> Result<Self::Output> {
if let Some(value) = req
.headers()
.get(TOKEN_HEADER)
.and_then(|value| value.to_str().ok())
{
// Insert token data to extensions of request.
let token = value.to_string();
req.extensions_mut().insert(Token(token));
}
// call the next endpoint.
self.ep.call(req).await
}
}
#[handler]
async fn index(Data(token): Data<&Token>) -> String {
token.0.clone()
}
// Use the `TokenMiddleware` middleware to convert the `index` endpoint.
let ep = index.with(TokenMiddleware);
let mut resp = ep
.call(Request::builder().header(TOKEN_HEADER, "abc").finish())
.await
.unwrap();
assert_eq!(resp.take_body().into_string().await.unwrap(), "abc");
Create middleware with functions
use std::sync::Arc;
use poem::{handler, web::Data, Endpoint, EndpointExt, IntoResponse, Request, Result};
const TOKEN_HEADER: &str = "X-Token";
#[handler]
async fn index(Data(token): Data<&Token>) -> String {
token.0.clone()
}
/// Token data
struct Token(String);
async fn token_middleware<E: Endpoint>(next: E, mut req: Request) -> Result<E::Output> {
if let Some(value) = req
.headers()
.get(TOKEN_HEADER)
.and_then(|value| value.to_str().ok())
{
// Insert token data to extensions of request.
let token = value.to_string();
req.extensions_mut().insert(Token(token));
}
// call the next endpoint.
next.call(req).await
}
let ep = index.around(token_middleware);
let mut resp = ep
.call(Request::builder().header(TOKEN_HEADER, "abc").finish())
.await
.unwrap();
assert_eq!(resp.take_body().into_string().await.unwrap(), "abc");
Associated Types
New endpoint type.
If you don’t know what type to use, then you can use
BoxEndpoint
, which will bring some
performance loss, but it is insignificant.
Required methods
Implementations on Foreign Types
sourceimpl<E, T1, T2> Middleware<E> for (T1, T2) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
impl<E, T1, T2> Middleware<E> for (T1, T2) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
sourceimpl<E, T1, T2, T3> Middleware<E> for (T1, T2, T3) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
impl<E, T1, T2, T3> Middleware<E> for (T1, T2, T3) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
sourceimpl<E, T1, T2, T3, T4> Middleware<E> for (T1, T2, T3, T4) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
impl<E, T1, T2, T3, T4> Middleware<E> for (T1, T2, T3, T4) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
sourceimpl<E, T1, T2, T3, T4, T5> Middleware<E> for (T1, T2, T3, T4, T5) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
impl<E, T1, T2, T3, T4, T5> Middleware<E> for (T1, T2, T3, T4, T5) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6> Middleware<E> for (T1, T2, T3, T4, T5, T6) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
impl<E, T1, T2, T3, T4, T5, T6> Middleware<E> for (T1, T2, T3, T4, T5, T6) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
T15: Middleware<T14::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
T15: Middleware<T14::Output>,
sourceimpl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
T15: Middleware<T14::Output>,
T16: Middleware<T15::Output>,
impl<E, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16> Middleware<E> for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, T15, T16) where
E: Endpoint,
T1: Middleware<E>,
T2: Middleware<T1::Output>,
T3: Middleware<T2::Output>,
T4: Middleware<T3::Output>,
T5: Middleware<T4::Output>,
T6: Middleware<T5::Output>,
T7: Middleware<T6::Output>,
T8: Middleware<T7::Output>,
T9: Middleware<T8::Output>,
T10: Middleware<T9::Output>,
T11: Middleware<T10::Output>,
T12: Middleware<T11::Output>,
T13: Middleware<T12::Output>,
T14: Middleware<T13::Output>,
T15: Middleware<T14::Output>,
T16: Middleware<T15::Output>,
Implementors
sourceimpl<E> Middleware<E> for CookieJarManager where
E: Endpoint,
impl<E> Middleware<E> for CookieJarManager where
E: Endpoint,
type Output = CookieJarManagerEndpoint<E>
sourceimpl<E> Middleware<E> for ForceHttps where
E: Endpoint,
impl<E> Middleware<E> for ForceHttps where
E: Endpoint,
type Output = ForceHttpsEndpoint<E>
sourceimpl<E, T> Middleware<E> for AddData<T> where
E: Endpoint,
T: Clone + Send + Sync + 'static,
impl<E, T> Middleware<E> for AddData<T> where
E: Endpoint,
T: Clone + Send + Sync + 'static,
type Output = AddDataEndpoint<E, T>
sourceimpl<E: Endpoint> Middleware<E> for CookieSession
This is supported on crate feature session
only.
impl<E: Endpoint> Middleware<E> for CookieSession
This is supported on crate feature
session
only.type Output = CookieJarManagerEndpoint<CookieSessionEndpoint<E>>
sourceimpl<E: Endpoint> Middleware<E> for Compression
impl<E: Endpoint> Middleware<E> for Compression
type Output = CompressionEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for Cors
impl<E: Endpoint> Middleware<E> for Cors
type Output = CorsEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for Csrf
impl<E: Endpoint> Middleware<E> for Csrf
type Output = CookieJarManagerEndpoint<CsrfEndpoint<E>>
sourceimpl<E: Endpoint> Middleware<E> for NormalizePath
impl<E: Endpoint> Middleware<E> for NormalizePath
type Output = NormalizePathEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for OpenTelemetryMetrics
impl<E: Endpoint> Middleware<E> for OpenTelemetryMetrics
type Output = OpenTelemetryMetricsEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for PropagateHeader
impl<E: Endpoint> Middleware<E> for PropagateHeader
type Output = PropagateHeaderEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for SensitiveHeader
impl<E: Endpoint> Middleware<E> for SensitiveHeader
type Output = SensitiveHeaderEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for SetHeader
impl<E: Endpoint> Middleware<E> for SetHeader
type Output = SetHeaderEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for SizeLimit
impl<E: Endpoint> Middleware<E> for SizeLimit
type Output = SizeLimitEndpoint<E>
sourceimpl<E: Endpoint> Middleware<E> for Tracing
impl<E: Endpoint> Middleware<E> for Tracing
type Output = TracingEndpoint<E>
sourceimpl<T, E> Middleware<E> for OpenTelemetryTracing<T> where
T: Tracer + Send + Sync,
E: Endpoint,
impl<T, E> Middleware<E> for OpenTelemetryTracing<T> where
T: Tracer + Send + Sync,
E: Endpoint,
type Output = OpenTelemetryTracingEndpoint<T, E>
sourceimpl<T, E, E2> Middleware<E> for FnMiddleware<T> where
T: Fn(E) -> E2,
E: Endpoint,
E2: Endpoint,
impl<T, E, E2> Middleware<E> for FnMiddleware<T> where
T: Fn(E) -> E2,
E: Endpoint,
E2: Endpoint,
type Output = E2
sourceimpl<T: SessionStorage, E: Endpoint> Middleware<E> for ServerSession<T>
This is supported on crate feature session
only.
impl<T: SessionStorage, E: Endpoint> Middleware<E> for ServerSession<T>
This is supported on crate feature
session
only.