Prevent a middleware from loading twice when inheriting another the parent controller

33 views
Skip to first unread message

scalp42

unread,
Mar 9, 2014, 8:26:07 PM3/9/14
to scor...@googlegroups.com
Hi Tom,

I'm running into a situation where I have a root controller with a middleware.

If I map a sub controller in it and have the sub controller inherit the parent one, I end up with the controller running twice.

Assuming that I'm trying to use Rack::Session::Cookie:

# base.rb
class Base < Scorched::Controller
 middleware
<< proc do
 
use Rack::Session::Cookie, secret: 'mysecretcookie'
 
end
 
 map pattern
: '/sub', conditions: { method: 'GET' }, target: Sub
 
 
# If set after the mapping, it won't work for some reason
 
# middleware << proc do
 
# use Rack::Session::Cookie, secret: 'mysecretcookie'
 
# end
end


# sub.rb
class Sub < ApiV1
 
get '/hello' do
 
'world'
 
end
end


If I try to curl /sub/hello, I will end up with two headers for the cookie (middleware is run twice).

I tried to move it after the mappings, but it didn't run at all.

I created a gist if it helps: https://gist.github.com/scalp42/fc82207c5cf546e5ea24

Thanks a lot in advance for your help,


Anthony

scalp42

unread,
Mar 9, 2014, 8:46:02 PM3/9/14
to scor...@googlegroups.com
Looks like switching map by controller gets me the desired behavior:

map pattern: '/sub', conditions: { method: 'GET' }, target: Sub

to

controller '/sub', Sub

Only problem is that I lose the method condition on the sub controller.

scalp42

unread,
Mar 9, 2014, 8:59:12 PM3/9/14
to scor...@googlegroups.com
I can also get the desired behavior this way:

class Base < Scorched::Controller
  middleware << proc do
    use Rack::Session::Cookie, secret: 'mysecretcookie'
    map '/sub' do
      use Rack::Session::Cookie, secret: 'mysecretcookie'
    end
  end
 
  map pattern: '/sub', conditions: { method: 'GET' }, target: Sub
end

In this case, I will have as wanted two different cookies for '/' and '/sub/'.

Hopefully it helps someone,

Cheers.

scalp42

unread,
Mar 9, 2014, 9:00:17 PM3/9/14
to scor...@googlegroups.com
Please note that you still lose the possibility of having a different cookie per the method for example.

Tom Wardrop

unread,
Mar 9, 2014, 10:05:06 PM3/9/14
to scor...@googlegroups.com
I'm confused with your example, plus it doesn't seem to be complete (APIv1 is missing for example). It's also not clear what you're trying to do. Here's a simple example that uses Rack::Session::Cookie which is defined in a base controller that the `Root` and `Sub` controller inherit from: https://gist.github.com/Wardrop/9458253

Note, you need to actually define something on the session for Rack::Session::Cookie to actually set a cookie, and it needs to be different for each request if you want the session cookie to be reset on every request (i.e. appear in the response headers).

Tom


On 10 March 2014 11:00, scalp42 <scal...@gmail.com> wrote:
Please note that you still lose the possibility of having a different cookie per the method for example.

--
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/1daf032f-7b34-4daf-b399-36e5895171ea%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages