3

I was wondering which could be a better way of mounting different apps for ruby . I have 2 sinatra apps and one rails app .

One way is to use rails as base and mount the sinatra apps using the routes.rb (within rails)

RailsApp::Application.routes.draw do
  mount SinatraApp1, :at => "/url1"
  mount SinatraApp2, :at => "/url2"
  # rest of the rail routes
end

This way both the sinatra apps are in rails.

Other way is to use rackup to mount all three using config.ru (all three apps in parallel)

map "/" do
  run RailsApp::Application
end

map "/url1" do
  run SinatraApp1
end

map "/url2" do
  run SinatraApp2
end

I am not able to find the advantages of one over the other or which method is better for what reason.

Gaurav Shah
  • 5,223
  • 7
  • 43
  • 71
  • This article offers some helpful explanations too: https://robots.thoughtbot.com/how-to-share-a-session-between-sinatra-and-rails – user1515295 Nov 08 '15 at 14:59

1 Answers1

3

Interesting there is any significant difference apart from the fact that in the latter part(application mounted using rackup)

any requests to

/url1

/url2

would be server directly from the mounted rack application without actually passing the request in the middleware stack of rails and then detect valid rack application for the given mounted path .

Which would happen in first part where your sinatra application is mounted inside Rails defined in routes.rb of your file

So I your are trying to mount your sinatra application in routes.rb instead of config.ru then consider the fact that your request would be passed all along rails middleware stack where routing middleware detect the appropriate path as passed the request to desired sinatra application

A Simple check You can do for this is try hitting your path i.e /url1 or /url2 in both the technique and you would see sinatra application routes.rb would log the request in your rails application whereas the other one would not

Hope this help

Viren
  • 5,812
  • 6
  • 45
  • 98
  • So I see no reason to mount it via rails . mounting it with `config.ru` would be better always ? but everywhere when I google the mounting approach I see the rails one . any advantages of mounting it via rails ? – Gaurav Shah Jun 23 '12 at 13:59
  • @GauravShah I see 2 reasons for it `Reason 1:-` The Reason only known to the people who use the approach of mount rack application in rails routes :) , `Reason-2:-` Which I guess is major reason why people adopt this is that prior to `rails3`, `rails2` does embraces the rack so beautiful I mean unlike rails3, in rails2 does not ship with a default `config.ru` implicitly although you can create define one but `not` ship a default `config.ru` could be the reason which would have adopted that practice which we can see is still evident in `rails3` application too . – Viren Jun 23 '12 at 17:11
  • so config.ru method is better ? – Gaurav Shah Jun 26 '12 at 11:49