问题
I have a Rack server where I run multiple websites.
use Rack::Session::Cookie
app = lambda do |env|
case
# Kek Mobile
when env['HTTP_HOST'] =~ /mobi.kek.com/
require ::File.expand_path(::File.join(::File.dirname(__FILE__),'code','kek_mobile','main.rb'))
selectedApp = KekMobile.new
# Kek Facebook App
when env['HTTP_HOST'] =~ /fb.kek.com/
require ::File.expand_path(::File.join(::File.dirname(__FILE__),'code','facebook','main.rb'))
selectedApp = Facebook.new
else #we launch the corp website
require ::File.expand_path(::File.join(::File.dirname(__FILE__),'code','corp','main.rb'))
selectedApp = Corp.new
end
selectedApp.call env
end
run app
I am trying to use some Rack Middleware but I don't want to use them for all the websites. For example I would like to use an OAuth Middleware for only the facebook app website. I tried to use the Middleware in the when statement or in the website main.rb file but it's not working. Is it possible to launch website specific Middleware?
Thank you in advance.
Thomy
回答1:
I believe the URLMap middleware will solve your problem, or at least put you on the right track.
- Examples: http://blog.ninjahideout.com/posts/rack-urlmap-and-kicking-ass
- Official docs: http://rack.rubyforge.org/doc/classes/Rack/URLMap.html
As you can see, URLMap lets you supply a different middleware pipeline for each app:
use Rack::Lint
map "/" do
use Rack::CommonLogger
run our_test_app
end
map "/lobster" do
use Rack::ShowExceptions
run Rack::Lobster.new
end
From your example, it's clear that this will not work directly, since you are mapping based on the HTTP host. However, the official docs mention, "Support for HTTP/1.1 host names exists if the URLs start with http:// or https://." So, perhaps you can call map "http://mobi.kek.com"
and map "http://fb.kek.com"
.
Good luck!
来源:https://stackoverflow.com/questions/4598264/use-some-middleware-only-for-specific-rack-website