Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Cannot programmatically apply middlewares? #760

Closed
karenetheridge opened this Issue · 8 comments

4 participants

@karenetheridge

I'm trying to programmatically apply middlewares, as described in Dancer::Cookbook:

For example, I'm inserting this code:

use Path::Class;

my $access_log = file('access.log')->absolute;
my $logfh = $access_log->open('a') or die "open($access_log): $!";
$logfh->autoflush(1);

set plack_middlewares => [
    [
        AccessLog => [
             format => 'combined',
             logger => sub { $logfh->print( @_ ) },
        ],
    ],
    # add more middleware here...
];

...right above the call to Dancer::dance() in https://github.com/karenetheridge/Pinto-Server/blob/master/lib/Pinto/Server.pm. This doesn't seem to work, as hits to the server do not result in entries appearing in access.log.

@karenetheridge

Opened as per irc:

16:58 <@franck> ether: can you report a bug for this on dancer ? I think the doc is incorrect
16:59 <@franck> if you can copy/paste what you're trying to do, I'll take a look later at this
@karenetheridge

PS. It looks like the plack_middlewares setting is only looked at by Dancer::Handler::PSGI. So could it be that my script is using a different handler? How do I get PSGI back into the mix?

@karenetheridge

Ok, I sourcedived some more and found that explicitly setting $ENV{PLACK_ENV} would force the PSGI handler to be used (I'm skeptical that this is the preferred way of using this handler though). Now I get this error:

Odd number of elements in anonymous hash at /home/perlbrew/perls/perl-5.14.2/lib/site_perl/5.14.2/Plack/Middleware.pm line 23.

Inspection of Plack::Middleware shows that one of the arrayrefs in the middleware spec should instead be an array, i.e.:

set plack_middlewares => [
    [
        AccessLog => (  # <-- note change
             format => 'combined',
             logger => sub { $logfh->print( @_ ) },
        ),  # <-- and here
    ],
];

However, now the server just simply fails to start, so it's dying later on. Help? :)

@karenetheridge

I solved my issue by converting my executable to a psgi and applying the middleware there, then calling Dancer::dance($request) within the $app sub. However, it seems that programmatic application of middlewares should still work within Dancer, so I'm leaving this ticket open.

@xsawyerx
Owner

@karenetheridge My apologies for having this sit here for a while.

Could you perhaps minimize this to a very specific use case I could play with?

We've had the middlewares API change and we needed to update the docs, but I believe we've already done so. Have you tried other Plack middlewares? Perhaps this specific one has a side-effect by providing a coderef? Doesn't seem likely, I know, but I usually try to do that too just to make sure I'm not missing any side-effects by accident...

I'll take some time today to check this out. FWIW, Pinto++. :)

@pjf pjf referenced this issue from a commit in pjf/Dancer
@pjf pjf Fix Bug #844/#760 - Misleading plack middleware documentation
The Plack middlware interface expects an array of arrays, where
the first element of each one is the middleware name, and the rest
are the arguments to that middleware.  The Cookbook suggests a
name and array-reference is required.

This change adjusts the data structures used appropriately (both
in code and in YAML).

Tested against Dancer 1.3095 and Plack 0.9988
8255b66
@pjf pjf referenced this issue from a commit in pjf/Dancer
@pjf pjf Fix Bug #844/#760 - Misleading plack middleware documentation
The Plack middlware interface expects an array of arrays, where
the first element of each one is the middleware name, and the rest
are the arguments to that middleware.  The Cookbook suggests a
name and array-reference is required.

This change adjusts the data structures used appropriately (both
in code and in YAML).

Tested against Dancer 1.3095 and Plack 0.9988
1700f6e
@xsawyerx xsawyerx referenced this issue from a commit
@pjf pjf Fix Bug #844/#760 - Misleading plack middleware documentation
The Plack middlware interface expects an array of arrays, where
the first element of each one is the middleware name, and the rest
are the arguments to that middleware.  The Cookbook suggests a
name and array-reference is required.

This change adjusts the data structures used appropriately (both
in code and in YAML).

Tested against Dancer 1.3095 and Plack 0.9988
e8f8288
@mokko mokko referenced this issue from a commit in mokko/Dancer
@pjf pjf Fix Bug #844/#760 - Misleading plack middleware documentation
The Plack middlware interface expects an array of arrays, where
the first element of each one is the middleware name, and the rest
are the arguments to that middleware.  The Cookbook suggests a
name and array-reference is required.

This change adjusts the data structures used appropriately (both
in code and in YAML).

Tested against Dancer 1.3095 and Plack 0.9988
881506f
@bigpresh
Owner

I see changes were made to improve middleware docs - is this still an issue now, or have those changes taken care of it?

Also, sorry this issue has sat untouched for so long!

@pjf

Sorry for the super-slow response, AFAIK the documentation now matches uses (which works!), and so this issue can now be closed.

@xsawyerx
Owner

@pfenwick Thank you! :)

@xsawyerx xsawyerx closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.