Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple reverse proxy example #784

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add simple reverse proxy example #784

wants to merge 2 commits into from

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.

None yet

4 participants