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

How to proxy request to another host and pass response back to original caller #1350

Closed
zwhitchcox opened this issue Jun 18, 2020 · 8 comments
Labels
question A question (converts to discussion)

Comments

@zwhitchcox
Copy link

zwhitchcox commented Jun 18, 2020

TL;DR use this if you're integrating rocket with create-react-app

version: 0.4.5

steps taken: looked at docs, google, source code, issues, etc.

Hi, I was wondering if there was any middleware for a proxy server or what would be the best way to go about creating a proxy server so I can forward requests to my create-react-app server instance.

If you are unfamiliar, during development, the create-react-app server listens on port 3000 and supplies the front end for development.

In the past I have used express-http-proxy for my typescript code to forward development traffic to that port.

I know what I'm asking is super basic and is probably fairly easy to create, but I guess I was wondering if there was already a prepared solution or recommended approach to save myself and possibly others some time. And if not, we could create something that benefits the community as a whole.

Is there a project in place already? What would be a good place to start? I have not seen a place to create middleware or a list of middlewares that already exist, is there one yet?

@zwhitchcox
Copy link
Author

Ok, so it looks like this is impossible until #1065 is implemented?

@zwhitchcox
Copy link
Author

Closing as people don't really seem to answer questions here

@jebrosen
Copy link
Collaborator

Hi, I was wondering if there was any middleware for a proxy server or what would be the best way to go about creating a proxy server so I can forward requests to my create-react-app server instance.

I'm not aware of any middleware (in Rocket terms, it would probably be either a Fairing or Responder) that is made for proxying requests. Depending on how much of the create-react-app devserver's functionality you are using, proxying requests may end up being more complex to get working than replacing the devserver entirely with StaticFiles + a tool such as watchexec to rebuild the js when files have changed. Alternatively, a program with dedicated proxy support such as nginx can make two backend servers appear as a single one. That said, assuming you do want to proxy from inside of Rocket:

Ok, so it looks like this is impossible until #1065 is implemented?

That shouldn't be a requirement; the main limitation of 0.4 that might apply is "one in-flight request per worker thread", but this should not be a problem for static file serving in particular.

I know what I'm asking is super basic and is probably fairly easy to create, but I guess I was wondering if there was already a prepared solution or recommended approach to save myself and possibly others some time. And if not, we could create something that benefits the community as a whole.

An HTTP proxy is unfortunately less basic than it seems. The biggest issues are with headers - some of which need to be forwarded, and some of which don't - and with the forwarding of different transfer encodings of bodies. For the specific case of a static file server like create-react-app's, you should be able to avoid most of the "hard" problems though.

Is there a project in place already? What would be a good place to start? I have not seen a place to create middleware or a list of middlewares that already exist, is there one yet?

rocket_contrib is a set of addons for rocket that are officially-sanctioned, i.e. they are maintained and updated in sync with rocket itself. We don't currently maintain a list of other middleware or utilities that work with rocket, but many of them have been published to crates.io.

As for "a good place to start": I would start by getting something working in a route directly, and when I was comfortable with the implementation I would adapt it to either a Fairing or a custom Responder. For the case of proxying, a Responder seems somewhat more appropriate.

@jebrosen jebrosen added the question A question (converts to discussion) label Jun 19, 2020
@zwhitchcox
Copy link
Author

@jebrosen Hey, thanks for your detailed response, I know my question was long, so I appreciate that! That gives me more confidence in using rocket.rs moving forward, because I was afraid there wouldn't be enough community support if I get stuck, and this is my first project with Rust as well.

But I actually found that you can use create-react-app's proxy functionality and just forward requests from the create-react-app instance, which is much easier than trying to create the proxy in rust. So for anyone else stumbling upon this in the future, that is what I would recommend, and then just adding a static file server when you are ready to deploy to production.

Thanks for all you do, rocket.rs team!

@jebrosen
Copy link
Collaborator

But I actually found that you can use create-react-app's proxy functionality and just forward requests from the create-react-app instance

That is probably the best solution; it's similar to my nginx suggestion above but much more convenient. In hindsight, I knew enough to check for that first! I frequently use a similar feature in Angular's ng serve.

@zwhitchcox
Copy link
Author

In hindsight, I knew enough to check for that first!

I definitely did too...although, the last time I used React, this was not an option....thankfully there is a really good example of an integration here: https://github.com/ghotiphud/rust-web-starter

@zwhitchcox
Copy link
Author

On a semi-related note, rocket also rejects the proxied requests, even though using them without a proxy works fine...full disclosure I just started using warp, because of this and also lack of support for async/await, and I figured I'd just let someone else figure all this out and come back to it xD, but I would like to know out of curiosity if you happen to know does rocket just reject all proxied requests by default?

@jebrosen
Copy link
Collaborator

On a semi-related note, rocket also rejects the proxied requests,
if you happen to know does rocket just reject all proxied requests by default?

That doesn't sound right. The first thing that comes to mind is that the request URI doesn't match. If you request http://localhost:xyz/api/endpoint to create-react-app, does it pass it through directly as /api/endpoint or does it rewrite it to /endpoint? Alternatively, if you have some of Rocket's log output from one of these rejected requests that may help troubleshoot.

I just started using warp, because of this and also lack of support for async/await

Sorry to hear that. The async branch (#1065) is nearly ready to merge into master and there is a bit more work after that to release Rocket 0.5. When that happens, I hope you're interested in giving Rocket another try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question A question (converts to discussion)
Projects
None yet
Development

No branches or pull requests

2 participants