-
Notifications
You must be signed in to change notification settings - Fork 321
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
base: main
Are you sure you want to change the base?
Conversation
I found this example useful in understanding how a tide-based server can handle reverse proxy.
Good one |
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 |
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)); |
I found this example useful in understanding how a tide-based server can handle reverse proxy.