2010-10-24 15 views

回答

17

這將返回所有的機架應用程序(包括中間件)的列表:

require 'rack' 

def middleware_classes(app)                                    
    r = [app] 

    while ((next_app = r.last.instance_variable_get(:@app)) != nil) 
    r << next_app 
    end 

    r.map{|e| e.instance_variable_defined?(:@app) ? e.class : e } 
end 

app = Rack::Builder.parse_file('config.ru').first 

p middleware_classes(app) 
+0

+1像一個魅力工作。我把所有這些放到'test-rack.rb'腳本中,並通過'ruby test-rack.rb'運行。 (編輯問題添加缺少的機架需求;) – 2014-10-18 02:50:31

-3

這是姍姍來遲,我知道

here's a nice thread,明確的答案是最後一篇

更新每馬克 - 安德烈的評論:

以下是這link

最後發表的文章

Rack沒有這個機制,因爲並不是所有的中間件都是通過@middleware添加的,Sinatra不能告訴你使用了什麼中間件。順便說一句,它可以告訴你什麼是可能的。中間件不一定是線性列表(即使用Rack路由器或其他設備時)。

+5

-1。 SO的全部內容不必通過發佈列表。複製並改進正確答案並將該鏈接添加爲源。 – 2013-01-15 21:30:21

19
> rake middleware 

use ActionDispatch::Static 
use Rack::Lock 
use #<ActiveSupport::Cache::Strategy::LocalCache::Middleware:0x007ffd148f9468> 
use Rack::Runtime 
use Rack::MethodOverride 
use ActionDispatch::RequestId 
use Rails::Rack::Logger 
use ActionDispatch::ShowExceptions 
use ActionDispatch::DebugExceptions 
use ActionDispatch::RemoteIp 
use ActionDispatch::Reloader 
use ActionDispatch::Callbacks 
use ActiveRecord::Migration::CheckPending 
use ActiveRecord::ConnectionAdapters::ConnectionManagement 
use ActiveRecord::QueryCache 
use ActionDispatch::Cookies 
use ActionDispatch::Session::CookieStore 
use ActionDispatch::Flash 
use ActionDispatch::ParamsParser 
use Rack::Head 
use Rack::ConditionalGet 
use Rack::ETag 
run RackTest::Application.routes 

http://pothibo.com/2013/11/ruby-on-rails-inside-actiondispatch-and-rack/

+13

從描述中,很清楚他知道'rake中間件'。他希望這是一個非Rails應用程序。 – 2014-10-09 22:14:14

相關問題