Module redirect

Source
Expand description

Service that redirects all requests.

§Example

Imagine that we run example.com and want to redirect all requests using HTTP to HTTPS. That can be done like so:

use http::{Request, Uri, StatusCode};
use http_body_util::Full;
use bytes::Bytes;
use tower::{Service, ServiceExt};
use tower_http::services::Redirect;

let uri: Uri = "https://example.com/".parse().unwrap();
let mut service: Redirect<Full<Bytes>> = Redirect::permanent(uri);

let request = Request::builder()
    .uri("http://example.com")
    .body(Full::<Bytes>::default())
    .unwrap();

let response = service.oneshot(request).await?;

assert_eq!(response.status(), StatusCode::PERMANENT_REDIRECT);
assert_eq!(response.headers()["location"], "https://example.com/");

Structs§

Redirect
Service that redirects all requests.
ResponseFuture
Response future of Redirect.