我正在寻找的功能类似于rake middleware
Rails 中的命令,但通用机架应用程序除外。
问问题
17276 次
5 回答
42
$ 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/
于 2013-12-21T00:23:02.930 回答
20
这将返回机架应用程序的所有中间件的列表:
def middleware_classes(app)
app.instance_variable_get(:@use).map{|middleware| middleware.call.class.name }
end
app = Rack::Builder.parse_file('config.ru').first; nil # trailing nil to avoid paging in an interactive console
p middleware_classes(app)
于 2012-07-31T15:04:23.143 回答
1
如果您使用的是扩展的 Sinatra 应用程序Sinatra::Base
,我必须使用 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]
可能有一种更清洁的方法来做到这一点。
于 2020-02-28T12:56:47.850 回答
0
试试 Konstantin Haase 的rack-graph gem。
出于某种原因,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
给定以下示例机架应用程序:
# 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)
于 2020-10-11T01:48:09.787 回答