How to print the middleware list?

2,924 views
Skip to first unread message

LI Daobing

unread,
Mar 9, 2011, 7:55:44 AM3/9/11
to sina...@googlegroups.com
Hello,

when I use rails, I can print the middleware list with "rake middleware" or "Rails.configuration.middleware", but I can't find a way to print this in sinatra.

I have tried Sinatra::Application.middleware, but it always return a empty Array

thanks

Ben Lovell

unread,
Mar 9, 2011, 8:01:54 AM3/9/11
to sina...@googlegroups.com
Sinatra::Application.instance_variable_get("@middleware")

Cheers,
Ben 

Konstantin Haase

unread,
Mar 9, 2011, 8:04:52 AM3/9/11
to sina...@googlegroups.com
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).

Konstantin

> --
> You received this message because you are subscribed to the Google Groups "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to sinatrarb+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sinatrarb?hl=en.

LI Daobing

unread,
Mar 9, 2011, 8:57:28 AM3/9/11
to sina...@googlegroups.com

this does not work, it only prints following information

[[Rack::Runtime, [], nil]]


and finally I found a way for this, throw exception and print
stackstrace, example as follows

require 'rubygems'
gem 'sinatra'
require 'sinatra'

enable :sessions
use Rack::Runtime

get '/' do
begin
raise 'hello'
rescue => e
content_type 'text/plain'
res = ''
for x in e.backtrace
res << x << "\n"
end
res
end
end


it will print following information:

hi.rb:10:in `GET /'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1063:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1063:in
`compile!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:649:in
`instance_eval'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:649:in
`route_eval'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:633:in
`route!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:681:in
`process_route'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:678:in
`catch'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:678:in
`process_route'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:632:in
`route!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:631:in
`each'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:631:in
`route!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:766:in
`dispatch!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:559:in
`call!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:731:in
`instance_eval'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:731:in
`invoke'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:731:in
`catch'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:731:in
`invoke'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:559:in
`call!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:544:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/runtime.rb:17:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/showexceptions.rb:24:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/methodoverride.rb:24:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/commonlogger.rb:18:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/session/cookie.rb:37:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1173:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1199:in
`synchronize'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1173:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/content_length.rb:13:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/chunked.rb:15:in
`call'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/connection.rb:84:in
`pre_process'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/connection.rb:82:in
`catch'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/connection.rb:82:in
`pre_process'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/connection.rb:57:in
`process'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/connection.rb:42:in
`receive_data'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in
`run_machine'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/eventmachine-0.12.10/lib/eventmachine.rb:256:in
`run'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/backends/base.rb:61:in
`start'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/thin-1.2.8/lib/thin/server.rb:159:in
`start'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/rack-1.2.1/lib/rack/handler/thin.rb:14:in
`run'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/base.rb:1144:in
`run!'
/home/lidb/.rvm/gems/ruby-1.8.7-p330/gems/sinatra-1.1.3/lib/sinatra/main.rb:25
hi.rb:21

> Cheers,
> Ben


>
> --
> You received this message because you are subscribed to the Google Groups
> "sinatrarb" group.
> To post to this group, send email to sina...@googlegroups.com.
> To unsubscribe from this group, send email to
> sinatrarb+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sinatrarb?hl=en.
>

--
Best Regards
LI Daobing

Ben Lovell

unread,
Mar 9, 2011, 9:01:30 AM3/9/11
to sina...@googlegroups.com
On 9 March 2011 13:57, LI Daobing <lida...@gmail.com> wrote:
On Wed, Mar 9, 2011 at 21:01, Ben Lovell <benjami...@gmail.com> wrote:
> On 9 March 2011 12:55, LI Daobing <lida...@gmail.com> wrote:
>>
>> Hello,
>> when I use rails, I can print the middleware list with "rake middleware"
>> or "Rails.configuration.middleware", but I can't find a way to print this in
>> sinatra.
>> I have tried Sinatra::Application.middleware, but it always return a empty
>> Array
>>
>
> Sinatra::Application.instance_variable_get("@middleware")

this does not work, it only prints following information

[[Rack::Runtime, [], nil]]
...

It will show any middleware that has been "use"d i.e. inserted into the pipeline via your app.

LI Daobing

unread,
Mar 9, 2011, 9:03:49 AM3/9/11
to sina...@googlegroups.com
On Wed, Mar 9, 2011 at 22:01, Ben Lovell <benjami...@gmail.com> wrote:
> On 9 March 2011 13:57, LI Daobing <lida...@gmail.com> wrote:
>>
>> On Wed, Mar 9, 2011 at 21:01, Ben Lovell <benjami...@gmail.com>
>> wrote:
>> > On 9 March 2011 12:55, LI Daobing <lida...@gmail.com> wrote:
>> >>
>> >> Hello,
>> >> when I use rails, I can print the middleware list with "rake
>> >> middleware"
>> >> or "Rails.configuration.middleware", but I can't find a way to print
>> >> this in
>> >> sinatra.
>> >> I have tried Sinatra::Application.middleware, but it always return a
>> >> empty
>> >> Array
>> >>
>> >
>> > Sinatra::Application.instance_variable_get("@middleware")
>>
>> this does not work, it only prints following information
>>
>> [[Rack::Runtime, [], nil]]
>> ...
>
> It will show any middleware that has been "use"d i.e. inserted into the
> pipeline via your app.
>

got it thanks

Reply all
Reply to author
Forward
0 new messages