Understand Plack and PSGI

[Some notes on a talk given by Peter Sergeant]

Back in ancient history, when people started creating dynamic web pages, the CGI specification was born. It defined the interface between the web server and the program that was generating the page. The program read input from STDIN and the environment, and wrote output to STDOUT.

CGI turned out to be a rather slow way to generate dynamic pages. So, people developed faster methods such as mod_perl and fastcgi. Each of these had different interfaces. This made it hard, for example, to take a CGI application and run it under mod_perl,

PSGI is a new interface that addresses these issues. It provides a single interface definition that works for many different web application environments. An application written to the PSGI spec can easily be switched from CGI to mod_perl or fastcgi (or a number of other deployment mechanisms). Like CGI before it, PSGI defines the inputs that your web application receives and the output that your web application should produce.

Because of the simplicity of the interface definition, it is easy to write a server that runs PSGI applications. Pete showed us one written in about twenty lines of code.

So, if PSGI is an interface specification, what is Plack? Plack is bundle of useful PSGI-aware tools. It contains PSGI handlers for various deployment environments (e.g. Plack::Handler::CGI), wrapper objects for PSGI requests and responses, and a number of middleware libraries.

It’s the middleware that is probably the most exciting part of the Plack bundle. The simplicity of the PSGI interface makes it almost trivial to write applications that wrap around other applications and alter their behaviour in various ways. One example is Plack::Middleware::Static, which can intercept requests for static assets and handle them without invoking the main (presumably quite heavyweight) part of your application. The talk finished with a demonstration of Plack::Middleware::Debug, which adds a very useful debug panel to the output of your web application containing lots of useful debugging information. With a small amount of poking, we even got it to display DBIC_TRACE output.

Various projects within NAP are starting to experiment with PSGI and Plack. The benefits will include flexibility in deployment and powerful development, debugging and testing tools.

You can read more about PSGI and Plack on their web site.

 

Print Friendly

Leave a Reply