Add simple reverse proxy example#784
Open
silvioprog wants to merge 2 commits intohttp-rs:mainfrom
silvioprog:main
Open
Add simple reverse proxy example#784silvioprog wants to merge 2 commits intohttp-rs:mainfrom silvioprog:main
silvioprog wants to merge 2 commits intohttp-rs:mainfrom
silvioprog:main
Conversation
I found this example useful in understanding how a tide-based server can handle reverse proxy.
|
Good one |
Member
|
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 |
Author
Done! |
|
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
I found this example useful in understanding how a tide-based server can handle reverse proxy.