Plack::Builder::Cascade?

30 views
Skip to first unread message

jakob...@gbv.de

unread,
Oct 4, 2012, 2:40:51 PM10/4/12
to psgi-...@googlegroups.com
Hi,

There is Plack::App::Cascade to query a list of apps, one after another, until a non-404 response is returned, but can you do the same with a middleware? I was looking for something like this:

builder {
  cascade {
     # first try basic authentification
     via "Auth::Basic", authentificator => \&foo;
     # otherwise look for access token
     via "Auth::AccessToken", authentificator => \&bar;
     # if both fail, try some rules, for instance access from some IPs
     via "Access", rules => $rules;
  };
  # each of the middlewares above delegates to this app
  $app;
};

Depending on the type of cascade middleware, the underlying $app would be called multiple times, until a non-error-response is returned. Could this be a problem, for instance when the request body has to be read again? The middleware cascade should make sure to seek the input stream for each request path.

Am I missing something are is there an easier solution to cascading middleware? This might work, but very ugly to type:

Plack::App::Cascade->new( apps => [
    map { builder { enable @$_; $app } } (
     Plack::Middleware::Auth::Basic->new( authentificator => \&foo ),
     Plack::Middleware::Auth::AccessToken->new( authentificator => \&bar ),
     Plack::Middleware::Access->new( rules => $rules )
    )
], codes => qw/^[45]/ );

Cheers,
Jakob

Tatsuhiko Miyagawa

unread,
Oct 4, 2012, 8:58:23 PM10/4/12
to psgi-...@googlegroups.com
Don't think it's ugly - it's just mouthful.

I don't think it makes much of a use but if you use that pattern a
lot, you can make a standalone DSL exporter on your own distribution.
I don't see a value in Plack core diet.
> --
>
>
>



--
Tatsuhiko Miyagawa
Reply all
Reply to author
Forward
0 new messages