pub struct RedirectFallbackMiddleware { /* private fields */ }Expand description
Middleware that redirects 404 errors to a fallback URL
§Examples
use std::sync::Arc;
use reinhardt_middleware::{RedirectFallbackMiddleware, RedirectResponseConfig};
use reinhardt_http::{Handler, Middleware, Request, Response};
use hyper::{StatusCode, Method, Version, HeaderMap};
use bytes::Bytes;
struct NotFoundHandler;
#[async_trait::async_trait]
impl Handler for NotFoundHandler {
async fn handle(&self, _request: Request) -> reinhardt_core::exception::Result<Response> {
Ok(Response::new(StatusCode::NOT_FOUND))
}
}
let config = RedirectResponseConfig::new("/404".to_string());
let middleware = RedirectFallbackMiddleware::new(config);
let handler = Arc::new(NotFoundHandler);
let request = Request::builder()
.method(Method::GET)
.uri("/missing")
.version(Version::HTTP_11)
.headers(HeaderMap::new())
.body(Bytes::new())
.build()
.unwrap();
let response = middleware.process(request, handler).await.unwrap();
assert_eq!(response.status, StatusCode::FOUND);
assert_eq!(
response.headers.get(hyper::header::LOCATION).unwrap(),
"/404"
);Implementations§
Source§impl RedirectFallbackMiddleware
impl RedirectFallbackMiddleware
Sourcepub fn new(config: RedirectResponseConfig) -> Self
pub fn new(config: RedirectResponseConfig) -> Self
Create a new RedirectFallbackMiddleware with the given configuration
§Examples
use reinhardt_middleware::{RedirectFallbackMiddleware, RedirectResponseConfig};
let config = RedirectResponseConfig::new("/404".to_string());
let middleware = RedirectFallbackMiddleware::new(config);Trait Implementations§
Source§impl Middleware for RedirectFallbackMiddleware
impl Middleware for RedirectFallbackMiddleware
Auto Trait Implementations§
impl Freeze for RedirectFallbackMiddleware
impl RefUnwindSafe for RedirectFallbackMiddleware
impl Send for RedirectFallbackMiddleware
impl Sync for RedirectFallbackMiddleware
impl Unpin for RedirectFallbackMiddleware
impl UnsafeUnpin for RedirectFallbackMiddleware
impl UnwindSafe for RedirectFallbackMiddleware
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more