Fork me on GitHub

Day 7: Use web application framework in PSGI

Since we started the Plack and PSGI project in September 2009 there has been a lot of feedback from the authors of popular frameworks such as Catalyst, Jifty, and CGI::Application.

CGI::Application is one of the most "traditional" CGI-based web application framework and it uses exclusively to handle web server environments just like we discussed yesterday.

Mark Stosberg, the current maintainer of CGI::Application, and I have been collaborating on adding PSGI support to CGI::Application. We thought of multiple approaches including adding native PSGI support to, but we ended up implementing CGI::PSGI as a wrapper and then using CGI::Application::PSGI to run existing CGI::Application code unmodified in a PSGI compatible mode.

All you have to do is install CGI::Application::PSGI from CPAN and write a .psgi file that looks like this:

 use CGI::Application::PSGI;
 use WebApp;

 my $app = sub {
     my $env = shift;
     my $app = WebApp->new({ QUERY => CGI::PSGI->new($env) });

Then use plackup to run the application with a standalone server or any of the other backends.

Similarly, most web frameworks that support PSGI provide a plugin, engine, or adapter to make the framework run in PSGI mode. For instance, Catalyst has a Catalyst::Engine::* web server abstraction and Catalyst::Engine::PSGI is the engine to adapt Catalyst to run on PSGI. (EDIT: As of Catalyst 5.8 released in 2011, Catalyst supports PSGI by default and there's no need to install a separate engine.)

The point is that with support from web frameworks you often won't need to modify a single line of code in your application to use PSGI. And by switching to PSGI there are lots of benefits like being able to use the toolchain of plackup, Plack::Test, and middleware which we'll discuss in future advent entries.