map_response

Function map_response 

Source
pub fn map_response<F, T, R1, R2>(f: F) -> MapResponseLayer<F, T, R1, R2>
Available on crate feature server only.
Expand description

Create a middleware for mapping a response from an async function

The async function can be:

  • async fn func(resp: Response) -> impl IntoResponse
  • async fn func(cx: &mut ServerContext, resp: Response) -> impl IntoResponse

ยงExamples

Append some headers:

use volo_http::{
    response::Response,
    server::{
        middleware::map_response,
        route::{Router, get},
    },
};

async fn handler() -> &'static str {
    "Hello, World"
}

async fn append_header(resp: Response) -> ((&'static str, &'static str), Response) {
    (("Server", "nginx"), resp)
}

let router: Router = Router::new()
    .route("/", get(handler))
    .layer(map_response(append_header));