danielSanchezQ / warp-reverse-proxy

Fully composable warp filter that can be used as a reverse proxy.
https://github.com/danielSanchezQ/warp-reverse-proxy
MIT License
48 stars 18 forks source link

Add method to proxy a request passing around a context object #36

Closed juliotpaez closed 3 years ago

juliotpaez commented 3 years ago

Hi Daniel,

I leave here the changes I mentioned in the issue #35. Also I give you another approach to resolve these problems:

warp::any()
    .and(with_context(&context))
    .and(extract_request_data_filter())
    .map(|context, uri, params, method, headers, body| {
        (context, ProxyData {
            proxy_address: "".to_string(),
            base_path: "".to_string(),
            uri,
            params,
            method,
            headers,
            body,
        })
    })
    .untuple_one()
    .and_then(proxy_request)

Where proxy_request is the following method:

async fn proxy_request(
    context: PipelineContext,
    proxy_data: ProxyData,
) -> Result<(PipelineContext, http::Response<Bytes>), Rejection> {
    match proxy_to_and_forward_response(
        proxy_data.proxy_address,
        proxy_data.base_path,
        proxy_data.uri,
        proxy_data.params,
        proxy_data.method,
        proxy_data.headers,
        proxy_data.body,
    )
    .await
    {
        Ok(response) => Ok((context, response)),
        Err(e) => Err(e)
    }
}

As you can see, another thing that I noticed is that the order of the arguments are really unnecessary, therefore I recommend to change the proxy_to_and_forward_response signatura to receive only a single object with all that info or even better using trait to be able to mix the context object together with ProxyData.

danielSanchezQ commented 3 years ago

I'll discard this for now. It is easy enough to create a custom wrapper around proxy_to_and_forward_response for anyone that requires the use of a context and composes the filter. But I'll take into account the grouping of the proxy data for further development. Thank you a lot @juliotpaez !