Skip to content

jamadam/Mojolicious-Plugin-PlackMiddleware

Repository files navigation

NAME

Mojolicious::Plugin::PlackMiddleware - Plack::Middleware inside Mojolicious

SYNOPSIS

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

DESCRIPTION

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.

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
    my $env = shift; # PSGI env
    if (...) {
        return 1; # causes the middleware hooked
    }
};
plugin plack_middleware => [
    Plack::Middleware::MyMiddleware, $condition, {arg1 => 'some_value'},
];

METHODS

register

$plugin->register;

Register plugin hooks in Mojolicious application.

psgi_env_to_mojo_req

This is a utility method. This is for internal use.

my $mojo_req = psgi_env_to_mojo_req($psgi_env)

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)

Example

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

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.