In Sinatra, is there a way to forward a request to

2019-09-09 13:18发布

问题:

I'm trying to do something like this:

class Dispatcher < Sinatra::Base
  def initialize
    @foo = Foo.new
  end

  get '/foo/*' do 
    @foo.call(params[:splat])
  end
end

So that URL /foo/abc/def?xxx=yyy would be like calling the Foo app with /abc/def?xxx=yyy.

This seems like it should be easy, but I haven't seen any example of it.

回答1:

I ended up doing this in a Rack config.ru file:

map "/abc" do
  run Foo.new('abc')
end

map "/def" do
  run Foo.new('def')
end

Not exactly what I wanted, but saves me from modifying the underlying app.



回答2:

I'm not sure why you would use Sinatra for that. If I understand you right, and you use Apache with Proxy-Rewrite rules you just do:

The .htaccess file

RewriteEngine On
RewriteRule ^foo/(.*) http://localhost:61000/$1 [P]

So all your domain.tdl/foo get redirected to your local running app athttp://localhost:61000/ with all Post and Get Parameters.



标签: sinatra rack