仅将某些中间件用于特定的Rack网站 [英] use some Middleware only for specific Rack website
问题描述
我有一个运行多个网站的机架服务器.
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
我正在尝试使用一些Rack中间件,但是我不想在所有网站上都使用它们.例如,我只想将OAuth中间件仅用于facebook应用程序网站. 我试图在when语句或网站main.rb文件中使用中间件,但是它不起作用. 是否可以启动网站特定的中间件?
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?
谢谢.
Thomy
推荐答案
我相信URLMap中间件将解决您的问题,或者至少使您处于正确的轨道.
I believe the URLMap middleware will solve your problem, or at least put you on the right track.
- 示例: http://blog.ninjahideout.com/posts/rack-urlmap-and-kicking-ass
- 官方文档: http://rack.rubyforge.org/doc/classes /Rack/URLMap.html
- Examples: http://blog.ninjahideout.com/posts/rack-urlmap-and-kicking-ass
- Official docs: http://rack.rubyforge.org/doc/classes/Rack/URLMap.html
如您所见,URLMap允许您为每个应用程序提供不同的中间件管道:
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
在您的示例中,很明显,这将无法直接运行,因为您是基于HTTP主机进行映射的.但是,官方文档提到:如果URL以http://或https://开头,则支持HTTP/1.1主机名."因此,也许您可以调用map "http://mobi.kek.com"
和map "http://fb.kek.com"
.
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"
.
祝你好运!
这篇关于仅将某些中间件用于特定的Rack网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!