开发者

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 开发者_Python百科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


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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜