Enhancing web applications with Plack

[ Perl tips index ]
[ Subscribe to Perl tips ]

A few months ago, we described how to write a twitter clone using the Dancer framework. Today, we're going to learn how we can use Plack to enhance that application, without needing to change the application at all.

Plack is a set of utilities that work with the Perl Server Gateway Interface (PSGI) specification, which describes how web servers and applications communicate. Any application that adheres to the PSGI specification can be used with Plack.

Getting started

In this tip, we're going to use our existing trillr application; a basic twitter clone that we wrote in a previous Perl tip. You can grab a copy of it with git (no authentication necessary):

    git://github.com/pjf/trillr.git

Because our application is written with Dancer, we don't need to make any changes at all to use the Plack framework. Provided we have Plack installed, we can start it with:

    $ plackup trill.pl

By default, we'll get a message like:

    HTTP::Server::PSGI: Accepting connections at http://0:5000/

The HTTP::Server::PSGI is a reference implementation webserver that's perfect for development. However you can also run your Plack-wrapped applications in Apache and other servers, too.

Adding middleware

One of the biggest reasons for using Plack is the large amount of Plack middleware that can be used with any PSGI application.

These middlewares can do anything from authentication, caching, stream compression, logging, or anything else that you can think of between content passing from an application to the client (or vice-versa).

One of the most useful layers is the Plack::Middleware::Debug layer, which dynamically adds extra debugging to any PSGI application.

Since Dancer is already Plack-aware, we can load middleware layers just by modifying our config.yml file:

    plack_middlewares:
        -
            - Debug

Now, if we run our application with plackup, each page has a debug pane added to the right. By default, it allows us to examine the environment, response, time spent, memory usage, and a full database trace:

Tweets sent via trill

That's right, a full database trace, right out of the box:

fill database trace

In fact, we can customise the panels that appear in the debugging pane, and even write our own! For example, if we had the Dancer::Debug distribution installed, we could use:

    plack_middlewares:
        -
            - Debug
            - panels
            -
                - Environment
                - Response
                - Timer
                - Memory
                - DBITrace
                - Dancer::Version
                - Dancer::Settings
                - Dancer::Logger

Of course, we can insert multiple middleware layers if we wish. The following configuration will also compress output before serving it, saving on bandwidth:

    plack_middlewares:
        -
            - Deflater
        -
            - Debug
            - panels
            -
                - Environment
                - Response
                - Timer
                - Memory
                - DBITrace
                - Dancer::Version
                - Dancer::Settings
                - Dancer::Logger

Note that order that middleware is loaded is important. Modules lower on the list are run "closer" to the application. In our case, the Debug layer can only wrap uncompressed content, so we run it first before passing its output "upwards" to the Deflate layer and webserver.

Why this is beyond cool

We've just added some very nifty layers to our existing Dancer application, but the whole point of Plack is that it's framework agnostic. Provided your application can be made to talk the PSGI specification, it can interface with Plack and Plack middlewares.

At the time of writing, all major Perl web frameworks (Dancer, Catalyst, Mojolicious, CGI::Application, Jifty, etc) support Plack. If you're working with legacy CGI code, then the CGI::PSGI and CGI::Emulate::PSGI modules will allow you to convert to using Plack/PSGI in just a few lines of code.

Learning more

[ Perl tips index ]
[ Subscribe to Perl tips ]


This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

If you have any questions please don't hesitate to contact us:

Email: contact@perltraining.com.au
Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS