Mojolicious::Plugin::PlackMiddleware - Plack::Middleware inside Mojolicious
# Mojolicious sub startup { my $self = shift; $self->plugin(plack_middleware => [ 'MyMiddleware1', 'MyMiddleware2', {arg1 => 'some_vale'}, 'MyMiddleware3', $condition_code_ref, 'MyMiddleware4', $condition_code_ref, {arg1 => 'some_value'} ]); } # Mojolicious::Lite plugin plack_middleware => [ 'MyMiddleware1', 'MyMiddleware2', {arg1 => 'some_vale'}, 'MyMiddleware3', $condition_code_ref, 'MyMiddleware4', $condition_code_ref, {arg1 => 'some_value'} ]; 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 using around_dispatch hook 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://mojolicio.us/perldoc/Mojolicious/Guides/Cookbook#Plack-middleware.
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 my $env = shift; # PSGI env if (...) { return 1; # causes the middleware hooked } }; plugin plack_middleware => [ Plack::Middleware::MyMiddleware, $condition, {arg1 => 'some_value'}, ];
$plugin->register;
Register plugin hooks in Mojolicious application.
This is a utility method. This is for internal use.
my $mojo_req = psgi_env_to_mojo_req($psgi_env)
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)
Plack::Middleware::Auth::Basic
$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'; } }, ]);
Plack::Middleware::ErrorDocument
$self->plugin('plack_middleware', [ ErrorDocument => { 500 => "$FindBin::Bin/errors/500.html" }, ErrorDocument => { 404 => "/errors/404.html", subrequest => 1, }, Static => { path => qr{^/errors}, root => $FindBin::Bin }, ]);
Plack::Middleware::JSONP
$self->plugin('plack_middleware', [ JSONP => {callback_key => 'json.p'}, ]);
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.