Get ordered list of middleware in a generic rack application?
-
25-09-2019 - |
문제
The functionality I am looking for is similar to the rake middleware
command in Rails, except for a generic rack application.
해결책
This will return a list of all the rack applications (including middleware):
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)
다른 팁
$ 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/
this is long overdue, I know
here's a nice thread, the definitive answer is the last post
UPDATED as per Marc-Andre's comment:
The following is the last post from this link
Rack does not have a mechanism for that and since not all middleware is added via @middleware, Sinatra can't tell you what middleware is used. Neither can rails, btw, it can just tell you what is possible. Middleware does not have to be a linear list (i.e. when using a Rack router or something).