NAME

CatalystX::Controller::PSGI - use a PSGI app in a Catalyst Controller

SYNOPSIS

package TestApp::Controller::File;
use Moose;
use namespace::autoclean;

BEGIN { extends 'CatalystX::Controller::PSGI'; }

use Plack::App::File;
use Plack::Response;

has 'app_file' => (
    is      => 'ro',
    default => sub {
        return Plack::App::File->new(
            file            => __FILE__,
            content_type    => 'text/plain',
        )->to_app;
    },
);

sub call {
    my ( $self, $env ) = @_;

    $self->app_file->( $env );
}

my $hello_app = sub {
    my ( $self, $env ) = @_;

    my $res = Plack::Response->new(200);
    $res->content_type('text/plain');
    $res->body("hello world");

    return $res->finalize;
};

__PACKAGE__->mount( '/hello/world' => $hello_app );

__PACKAGE__->meta->make_immutable;

DESCRIPTION

Use PSGI apps inside Catalyst Controllers.

Combine this with Catalyst::Component::InstancePerContext if you want to access $c in your psgi app

Usage

call method

If this method is provided, it will be called as the root action of that controller.

package TestApp::Controller::File;
use Moose;
use namespace::autoclean;

BEGIN { extends 'CatalystX::Controller::PSGI'; }

use Plack::App::File;

has 'app_file' => (
    is      => 'ro',
    default => sub {
        return Plack::App::File->new(
            file            => __FILE__,
            content_type    => 'text/plain',
        )->to_app;
    },
);

sub call {
    my ( $self, $env ) = @_;

    $self->app_file->( $env );
}

__PACKAGE__->meta->make_immutable;

E.g. in the above example it will be /file/

Works similar to Plack::Component, except that as well as $env being passed in, $self is as well. Where $env is the psgi env, and $self is the Catalyst Controller.

mount

Mount a path within the controller to an app.

package TestApp::Controller::Hello;
use Moose;
use namespace::autoclean;

BEGIN { extends 'CatalystX::Controller::PSGI'; }

use Plack::Response;

my $hello_app = sub {
    my ( $self, $env ) = @_;

    my $res = Plack::Response->new(200);
    $res->content_type('text/plain');
    $res->body("hello world");

    return $res->finalize;
};

__PACKAGE__->mount( '/world' => $hello_app );

__PACKAGE__->meta->make_immutable;

In the above example the url /hello/world will be bound to the $hello_app. As with call, $self and $env will be passed in.

EXAMPLES

http://www.catalystframework.org/calendar/2013/16 http://www.catalystframework.org/calendar/2013/17

There is also an example app in the test suite

AUTHOR

Mark Ellis <markellis@cpan.org>

SEE ALSO

Catalyst::Component::InstancePerContext

LICENSE

Copyright 2014 by Mark Ellis <markellis@cpan.org>

This library is free software, you can redistribute it and/or modify it under the same terms as Perl itself.