[dancer-users] Dancer2::Handler:: issues

Steven Humphrey catchdancerusers at 33k.co.uk
Tue Mar 26 13:30:13 GMT 2013

Hello all,

Apologies if this has been raised before.
I reckon there is an issue with the way Dancer2::Handler:: objects get 
incorporated into apps.

The current process is:
Dancer2::Core::App object is instantiated and its BUILD method gets 
called. This calls init_route_handlers.
init_route_handlers loops through the config route_handlers section and 
creates an appropriate route handler for each app.
This means, for example, the Dancer2::Handler::File object that handles 
requests to files in the public folder gets added to every app.

The effect of this for a non-existent url request
1) Loop through apps (pm files)
2) Find megasplat route for file handler
3) Call before hooks defined in that app!
4) Call Dancer2::Handler::File route
5) Dancer2::Handler::File doesn’t find file, so it ‘passes’ on to the 
next route.
6) Go to next app.

Whilst this is certainly inefficient, my primary concern is that the 
before hooks gets called for every single app when you expect before 
hooks to only apply to the routes that you defined in the same app/perl 
module as the before hook. Having before hooks apply to routes that you 
did not define is very confusing.

I’d suggest that the Dancer2 Handlers either get added to their own 
private Dancer2::Core::App object or to a single ‘handler app’.
Dancer2::Runner would therefore loop through the (default) handlers and 
add them to specific app objects.
I can’t currently see a use case where you’d want the same route 
handler to appear in every app, although potentially the route handler 
could define the route based off something in the config, which in 
theory could be different for each app/pm file.

2-3 questions then arise:
1) How does Dancer2::Runner handle these. A separate app for each 
2) If you do want a before hook to apply, how do you apply one.
3) If for some reason that I can’t see, you do want a handler to create 
a route in each app, how do you declare that? (some sort of 
app_route_handler config key?)

I am happy to implement these changes and raise a github issue/pull 
request if the core developers agree with me.

You can of course avoid before hooks firing by turning off the default 
route_handlers with a “route_handler: {}” line in config.yml, but that 
is super confusing and causes other issues.

- shumphrey

More information about the dancer-users mailing list