Mojolicious::Plugin::PlackMiddleware - Plack::Middleware inside Mojolicious
# 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; }
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 http://blog.kraih.com/mojolicious-and-plack.
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'}, ];
$plugin->register;
Register plugin hooks in Mojolicious application.
This is a utility method. This is for internal use.
my $plack_env = mojo_req_to_psgi_env($mojo_req)
my $mojo_res = psgi_res_to_mojo_res($psgi_res)
my $psgi_res = mojo_res_to_psgi_res($mojo_res)
$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'; } }, ]);
Sugama Keita, <sugama@jamadam.com>
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.
To install Mojolicious::Plugin::PlackMiddleware, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::PlackMiddleware
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::PlackMiddleware
For more information on module installation, please visit the detailed CPAN module installation guide.