Skip to content

Conversation

silvioprog
Copy link

I found this example useful in understanding how a tide-based server can handle reverse proxy.

I found this example useful in understanding how a tide-based server can handle reverse proxy.
@silvioprog silvioprog changed the title Add simple reverse proxy example Add simple reverse proxy example (#783) Jan 24, 2021
@silvioprog silvioprog changed the title Add simple reverse proxy example (#783) Add simple reverse proxy example (Issue #783) Jan 24, 2021
@silvioprog silvioprog changed the title Add simple reverse proxy example (Issue #783) Add simple reverse proxy example Jan 24, 2021
@darkdarkfruit
Copy link

Good one

@jbr
Copy link
Member

jbr commented Feb 5, 2021

I think before merging anything like this, the example should have a large and clear warning not to use this in production. There is more to a correct reverse proxy implementation than this example, and it should be a standalone crate. This is a security-sensitive feature

@silvioprog
Copy link
Author

I think before merging anything like this, the example should have a large and clear warning not to use this in production.

Done!

@DavidBM
Copy link

DavidBM commented Mar 2, 2021

I was able to make it work with:

fn bypass_to<State: Send + 'static>(
    method: tide::http::Method,
    url: tide::http::Url,
) -> impl Fn(tide::Request<State>) -> Pin<Box<dyn Future<Output = tide::Result> + Send + 'static>> {
    move |request: tide::Request<State>| {
        let url = url.clone();
        Box::pin(async move {
            let request: &tide::http::Request = request.as_ref();

            let mut request = request.clone();

            *request.url_mut() = url;
            request.set_method(method);

            let request = surf::Request::from(request);

            let response = surf::Client::default().send(request).await?;

            Ok(tide::Response::from_res(response))
        })
    }
}

and then you can just

use tide::http::{Method, Url};

let my_proxied_url = Url::parse("...").unwrap();

let mut app = tide::new();

app.at("/your route")
        .get(bypass_to(Method::Get, my_proxied_url));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants