Can't figure how to use Rack middlewares

24 views
Skip to first unread message

scalp42

unread,
Mar 4, 2014, 1:33:45 PM3/4/14
to scor...@googlegroups.com
Hi Tom,

I'm not sure if it's directly related to Scorched but I figured I should still ask.

I'm trying to figure how to use Rack middlewares and the doc is not entirely clear to me, in my case rack-attack (https://github.com/kickstarter/rack-attack) for example.

I pretty much want to try throttling requests from localhost, as a test.

I updated my initial gist to represent how I'm trying to use it: https://gist.github.com/scalp42/f3eafe67116062ef8239

In my example, I'm trying to add the middleware using the test.ru file.

In the docs, you mention using it on a controller level (which in my case would be in Test or ApiV1, not even sure), but I could not exactly understand this part:

middleware << proc do
  use Rack::Session::Cookie, secret: 'blah'
  # Stolen from Rack's own documentation...
  map "/lobster" do
    use Rack::Lint
    run Rack::Lobster.new
  end
end
Any chance you could explain this part more if possible ?

If none of my questions are really related to Scorched, feel free to close the discussion. I just haven't been able to get any middleware going.

Thanks in advance!

Tom Wardrop

unread,
Mar 4, 2014, 5:23:47 PM3/4/14
to scor...@googlegroups.com
Hey, no worries, I don't mind helping you out.

So basically, Scorched allows you to load middleware (and other Rack stuff) like you would in a normal rackup file (e.g. config.ru) within a controller. You do this by defining a `proc` and appending to the `middleware` collection on the controller. At runtime, these proc's are evaluated in the context of a Rack::Builder instance, which is more-or-less exactly how rackup files work.

So in the example you quoted from the documentation, you can simply pretend that the contents of that `proc` is the contents of a rackup file. One advantage over simply adding this to your rackup file is that you can apply such middleware only for certain sub-controllers, as opposed to applying to your whole application.
use Rack::Session::Cookie, secret: 'blah'
# Stolen from Rack's own documentation...
map "/lobster" do
  use Rack::Lint
  run Rack::Lobster.new
end

--
You received this message because you are subscribed to the Google Groups "Scorched" group.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scorched/b87ccc60-4911-4a04-9ac1-4b7ec177e6bc%40googlegroups.com.

Tom Wardrop

unread,
Mar 4, 2014, 5:25:43 PM3/4/14
to scor...@googlegroups.com
Whoops, I submitted that before I was actually finished. Anyway, hopefully that clarifies things a little. I'm not sure how much familiar you are with Rack middleware and rackup files, and it's not clear which parts are confusing you the most, but hopefully that helps? Feel free to ask any more questions.
Reply all
Reply to author
Forward
0 new messages