Question

I have a production Rails app into which I'd like to integrate, as seamlessly as possible, a non-Rails, non-Ruby server process. Namely, I'd like certain routes to be handled by a Haskell Snap web application running on the same server as the Rails app. These routes will all return JSON payloads, so there are no templating complications. But I'd like to avoid the overhead of setting up CORS, so I'd like these JSON responses to appear as if they are coming from the same source as the Rails-generated webpages.

What's the best integration strategy in this situation? Should I set up Rails controllers that make web requests to the Haskell web app and then forward the response to the client, or is there a more efficient way to do this using Rack middleware or an Nginx configuration?

Was it helpful?

Solution

The simplest way is to use nginx's Reverse Proxy on a subdirectory. For example (based on this question):

location /snap {
    rewrite /snap/(.*) /$1 break;
    proxy_pass http://localhost:8000;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Change localhost:8000 to wherever the Snap application is running.

You can also change the directory name from snap to anything you want. Just make sure you change it in both places where it appears, and that it doesn't conflict with any of the routes in your RoR application.


However, if you want to use Authentication/Authorization via the Ruby application in order to access the Haskell application's data, you may find it easier to implement your first suggestion of making your own proxy controller because that will allow you to use the Rails app's login logic before you call the Haskell app.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top