Rack apps mounted in different subdomains

330 views
Skip to first unread message

Lukas Alexandre

unread,
Oct 25, 2013, 3:12:02 AM10/25/13
to ruby-...@googlegroups.com

I'm building a Grape API alongside Sinatra. So far I've been mounting them in separate routes like this:

run Rack::URLMap.new("/" => Frontend::Server.new,
                     "/api" => API::Server.new)

Where the "/api" is served by a Grape app and "/" by a Sinatra app. But I wanted to use subdomains to separate those concerns instead of the actual "sub-URL". Any clues on how to do this?

Thanks in advance for the help.


Daniel Doubrovkine

unread,
Oct 25, 2013, 8:56:41 AM10/25/13
to ruby-...@googlegroups.com
I think this SO answers your question: http://stackoverflow.com/questions/19582880/rack-apps-mounted-in-different-subdomains

You don't need an URL map, use a middleware to split the two.


--
You received this message because you are subscribed to the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ruby-grape+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--

dB. | Moscow - Geneva - Seattle - New York
code.dblock.org - @dblockdotorg - artsy.net - github/dblock

Lukas Alexandre

unread,
Oct 25, 2013, 9:20:06 AM10/25/13
to ruby-...@googlegroups.com
Thanks!!


Lukas Alexandre
Co-Founder & Developer
CodeLogic.Me

You received this message because you are subscribed to a topic in the Google Groups "Grape Framework Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/ruby-grape/SIIDKJiIYDg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to ruby-grape+...@googlegroups.com.

Lukas Alexandre

unread,
Oct 25, 2013, 9:42:51 AM10/25/13
to ruby-...@googlegroups.com
BTW, something just came up. Any AJAX request from the “/“ view to “API” would be considered CORS right? If so is there any know way to get around it?


Lukas Alexandre
Co-Founder & Developer
CodeLogic.Me

Daniel Doubrovkine

unread,
Oct 25, 2013, 9:56:14 AM10/25/13
to ruby-...@googlegroups.com
You mean circumvent CORS? It's implemented in the browser, you cannot work around that :)

Rob Hunter

unread,
Oct 25, 2013, 11:03:59 AM10/25/13
to ruby-...@googlegroups.com

Just to clarify some terms here - it sounds like you might be talking past each other on the CORS front.

When a page "http://example.com/" tries to access a resource "http://subdomain.example.com/", the browser considers that a violation of the famous Same Origin policy.

CORS (Cross-Origin Resource Sharing) is a way around the Same Origin Policy in that it allows servers to allow specific exemptions to the Same Origin Policy.

Grape itself doesn't enable CORS itself, but it's easy to enable CORS with a middleware gem - see the CORS section in Grape's README:
https://github.com/intridea/grape/blob/master/README.md#cors

General information on using CORS to work around the Same Origin Policy:
http://enable-cors.org/

Daniel Doubrovkine

unread,
Oct 26, 2013, 4:17:01 PM10/26/13
to ruby-...@googlegroups.com
Lukas, let us know if that answers your questions. 

cheers
dB.

Lukas Alexandre

unread,
Oct 26, 2013, 5:25:20 PM10/26/13
to ruby-...@googlegroups.com
Hi guys,

Everything did worked. But I still had the problem that serving the API from “api.” and some pages from “www.” that needed data from the API would cause CORS. That is something that I’m trying to avoid.

I really appreciate your help on this. Thanks!


Lukas Alexandre
Co-Founder & Developer
CodeLogic.Me

Tiago Ferreira

unread,
Oct 26, 2013, 11:21:53 PM10/26/13
to ruby-...@googlegroups.com
Hey Lukas,

I already used rack-cors with grape and goliath and everything works fine (see example below):
use Rack::Cors do
    allow do
      origins '*'
      resource '*', :headers => :any, :methods => [:get, :post,
                                                   :options, :delete]
    end

    allow do
      origins '*'
      resource '/public/*', :headers => :any, :methods => :get
    end
end
In your case, I think that you should be more restrictive about the origins. BTW, did you use rack-cors and still didn't work?

Tiago Ferreira Lima

GitHub: fltiago
Skype: fltiago

Lukas Alexandre

unread,
Oct 30, 2013, 12:29:55 PM10/30/13
to ruby-...@googlegroups.com
Hi Tiago,

Using RackCORS did worked as expected. I just don’t feel too comfortable with that approach (god knows why…). But I’ll probably have to stick with it.

Thanks!

Lukas Alexandre
Co-Founder & Developer
CodeLogic.Me

Reply all
Reply to author
Forward
0 new messages