获取通用机架应用程序中的有序中间件列表?

时间:2010-10-24 11:36:26

标签: ruby rake rack middleware

我正在寻找的功能类似于Rails中的rake middleware命令,但通用机架应用程序除外。

5 个答案:

答案 0 :(得分:27)

$ 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/

答案 1 :(得分: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)

答案 2 :(得分:1)

如果您使用的扩展了Sinatra::Base的Sinatra应用程序,我必须使用迈克尔·黑尔(Michael Hale)答案的稍作修改的版本:

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
​
sinatra_app = Rack::Builder.parse_file('config.ru').first
sinatra_rack_builder = sinatra_app.build(sinatra_app)
sinatra_extended_app = sinatra_rack_builder.to_app
rack_app = sinatra_extended_app.app

pp middleware_classes(rack_app)

将其放入诸如dump_middleware.rb之类的文件中之后,我能够按预期看到中间件:

$ bundle exec ruby ./dump_middleware.rb
[Rack::Head,
 Rack::NullLogger,
 Rack::Session::Cookie,
 Rack::Protection::FrameOptions,
 Rack::Protection::HttpOrigin,
 Rack::Protection::IPSpoofing,
 Rack::Protection::JsonCsrf,
 Rack::Protection::PathTraversal,
 Rack::Protection::RemoteToken,
 Rack::Protection::SessionHijacking,
 Rack::Protection::XSSHeader,
 Warden::Manager,
 SinatraApp]

可能会有一种更清洁的方法。

答案 3 :(得分:0)

尝试Konstantin Haase的rack-graph宝石。

出于某种原因,康斯坦丁(Konstantin)认为不适合在rubygems上发布此gem,因此您要么需要使用git将其添加到Gemfile中,要么在本地安装并引用它。

# Gemfile
gem 'rack-graph', github: 'rkh/rack-graph'

$ bundle exec rackup -s Graph
# Locally (without bundler/Gemfile):
$ git clone https://github.com/rkh/rack-graph.git
$ ruby -I/path/to/rack-graph/lib $(which rackup) -s Graph

给出以下示例Rack应用程序:

# config.ru
Foo = proc { [200, {}, ['Foo']] }
App = proc { [200, {}, ['Ok']] }

map '/foo' do
  use Rack::Runtime
  use Rack::MethodOverride
  run Foo
end

run App

这是输出:

# Output:
Rack::ContentLength
 |- Rack::CommonLogger(stderr)
    |- Rack::ShowExceptions
       |- Rack::Lint
          |- Rack::TempfileReaper
             |- Rack::URLMap
                |- "/foo"
                |  |- Rack::Runtime
                |     |- Rack::MethodOverride
                |        |- Proc(0x00007fd93a97c2d0 /Users/steve/ruby/config.ru:1)
                |
                |- ""
                   |- Proc(0x00007fd93a97c2a8 /Users/steve/ruby/config.ru:2)

答案 4 :(得分:-4)

这是姗姗来迟,我知道

here's a nice thread,最后的答案是最后一篇

根据Marc-Andre的评论更新:

以下是此link

的最后一篇文章

Rack没有这样的机制,因为并非所有中间件都是通过@middleware添加的,Sinatra无法告诉你使用了什么中间件。铁路,顺便说一句,它只能告诉你什么是可能的。中间件不必是线性列表(即使用Rack路由器或其他东西时)。