简体   繁体   中英

How do I set up rails apps as rack apps within another rails app?

(I believe this is doable, but just googling around, I haven't found any examples of it.)

Perhaps a more concrete example of what I'm asking. Suppose I have rails-3 apps already written: Foo, Bar, Baz, Qux. I have another application written: Master. Within Master I want to match routes and run the other apps in it. Since they are all rack-compatible, I imagine the routing in master would be something like this:

match "/foo" => Foo::Application
match "/bar" => Bar::Application
match "/baz" => Baz::Application
match "/qux" => Qux::Application

but I haven't figured out how to do it and where to actually put the code for the apps relative to the master app.

You probably want to catch things earlier than routing in the master application.

You can do this by going lower-level in to Rack; the Rack configuration file in the root of your Rails app called config.ru:

# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment', __FILE__)

run Rack::URLMap.new \
  "/"    => Master::Application,
  "/foo" => Foo::Application,
  "/bar" => Bar::Application

  # ... etc.

You can read more about Rack::URLMap here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM