NAME

MojoX::Util::PlackMiddleware - Plack::Middleware inside Mojolicious

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;
  }

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 http://blog.kraih.com/mojolicious-and-plack.

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'},
];

METHODS

register

$plugin->register;

Register plugin hooks in Mojolicious application.

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)

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)

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)

Example1

$self->plugin(plack_middleware => [
    'Auth::Basic' => sub {shift->req->url =~ qr{/path1/}}, {
        authenticator => sub {
            my ($user, $pass) = @_;
            return $username eq 'user1' && $password eq 'pass';
        }
    },
    'Auth::Basic' => sub {shift->req->url =~ qr{/path2/}}, {
        authenticator => sub {
            my ($user, $pass) = @_;
            return $username eq 'user2' && $password eq 'pass2';
        }
    },
]);

AUTHOR

Sugama Keita, <sugama@jamadam.com>

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.