ian
ian

Reputation: 12251

Mount multiple Rack apps without adding a prefix to the url

How do I mount/run multiple rack apps without using map or Rack::UrlMap? Using these will dispatch the apps fine, but will also prefix the route used for dispatch to the beginning of the matcher, so:

class API < Sinatra::Base
  get "/api" do
    # blah
  end
end

map( "/api" ) { run API }

The route above is accessed at "/api/api" which is not what I want, just "/api" is what I want. I don't want to dig into the request object with a filter and remove prefixes if there's a better way.

I've tried:

use API.app  # the app is wrapped in a `def self.app` for convenience.
run Web.app

but use causes problems if the app itself has used use within it too. Doing this:

run API.app
run Web.app

will only serve routes from the last app given to run.

I'm about to try Rack::Cascade but I've never used that before and don't know if it's a good answer to this problem.

Upvotes: 2

Views: 765

Answers (1)

ian
ian

Reputation: 12251

The answer is indeed Rack::Cascade:

run Rack::Cascade.new( [API, Web] )

Upvotes: 3

Related Questions