=head1 NAME Mojolicious::Plugin::PlackMiddleware - Plack::Middleware inside Mojolicious =head1 SYNOPSIS # Mojolicious sub startup { my $self = shift; $self->plugin(plack_middleware => [ 'MyMiddleware1', 'MyMiddleware2', {arg1 => 'some_vale'}, 'MyMiddleware3', sub {$condition}, 'MyMiddleware4', sub {$condition}, {arg1 => 'some_vale'} ]); } # Mojolicious::Lite plugin plack_middleware => [ 'MyMiddleware1', 'MyMiddleware2', {arg1 => 'some_vale'}, 'MyMiddleware3', sub {$condition}, 'MyMiddleware4', sub {$condition}, {arg1 => 'some_vale'} ]; package Plack::Middleware::MyMiddleware1; use strict; use warnings; use base qw( Plack::Middleware ); sub call { my($self, $env) = @_; # pre-processing $env my $res = $self->app->($env); # post-processing $res return $res; } =head1 DESCRIPTION Mojolicious::Plugin::PlackMiddleware allows you to enable Plack::Middleware inside Mojolicious by wrapping on_proccess so that the portability of your app covers pre/post process too. It also aimed at those who used to Mojolicious bundle servers. Note that if you can run your application on a plack server, there is proper ways to use middlewares. See L. =head2 OPTIONS This plugin takes an argument in Array reference which contains some middlewares. Each middleware can be followed by callback function for conditional activation, and attributes for middleware. my $condition = sub { my $c = shift; # Mojolicious controller if (...) { return 1; # causes the middleware hooked } }; plugin plack_middleware => [ Plack::Middleware::MyMiddleware, $condition, {arg1 => 'some_vale'}, ]; =head1 METHODS =head2 register $plugin->register; Register plugin hooks in L application. =head2 mojo_req_to_psgi_env This is a utility method. This is for internal use. my $plack_env = mojo_req_to_psgi_env($mojo_req) =head2 psgi_res_to_mojo_res This is a utility method. This is for internal use. my $mojo_res = psgi_res_to_mojo_res($psgi_res) =head2 mojo_res_to_psgi_res This is a utility method. This is for internal use. my $psgi_res = mojo_res_to_psgi_res($mojo_res) =head1 Example1 $self->plugin(plack_middleware => [ 'Auth::Basic' => sub {shift->req->url =~ qr{^/?path1/}}, { authenticator => sub { my ($user, $pass) = @_; return $user eq 'user1' && $pass eq 'pass'; } }, 'Auth::Basic' => sub {shift->req->url =~ qr{^/?path2/}}, { authenticator => sub { my ($user, $pass) = @_; return $user eq 'user2' && $pass eq 'pass2'; } }, ]); =head1 AUTHOR Sugama Keita, Esugama@jamadam.comE =head1 COPYRIGHT AND LICENSE Copyright (C) 2011 by Sugama Keita. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut