Crate warp_reverse_proxy

Source
Expand description

Fully composable warp filter that can be used as a reverse proxy. It forwards the request to the desired address and replies back the remote address response.

use warp::{hyper::Body, Filter, Rejection, Reply, http::Response};
use warp_reverse_proxy::reverse_proxy_filter;

async fn log_response(response: Response<Body>) -> Result<impl Reply, Rejection> {
    println!("{:?}", response);
    Ok(response)
}

#[tokio::main]
async fn main() {
    let hello = warp::path!("hello" / String).map(|name| format!("Hello, {}!", name));

    // // spawn base server
    tokio::spawn(warp::serve(hello).run(([0, 0, 0, 0], 8080)));

    // Forward request to localhost in other port
    let app = warp::path!("hello" / ..).and(
        reverse_proxy_filter("".to_string(), "http://127.0.0.1:8080/".to_string())
            .and_then(log_response),
    );

    // spawn proxy server
    warp::serve(app).run(([0, 0, 0, 0], 3030)).await;
}

Modules§

errors

Statics§

CLIENT
Reverse proxy internal client

Functions§

extract_request_data_filter
Warp filter that extracts the relative request path, method, headers map and body of a request.
proxy_to_and_forward_response
Build a request and send to the requested address.
query_params_filter
Warp filter that extracts query parameters from the request, if they exist.
reverse_proxy_filter
Reverse proxy filter

Type Aliases§

Headers
Alias of warp HeaderMap
Method
Alias of warp Method
QueryParameters
Alias of query parameters.
Request
Wrapper around a request data tuple.
Uri
Alias of warp FullPath