Plack Advent Distilled - Week 1

If you are a web application or framework developer then PSGI and Plack are worth knowing about. Sensei Miyagawa put together a series of high quality articles describing quite a bit about PSGI and Plack. They are worth your time if you have any interest in the topic at all.

In this paper, I present a distillation of week one’s five articles for those who want a quick synopsis. In addition, for easy reference here are links to the original articles:

Sun Mon Tue Wed Thu Fri Sat
1
Getting Plack
2
Hello World
3
Using plackup
4
Reloading Applications
5
Static File Web Server
6
CGI to PSGI
7
PSGI Web Frameworks
8
Adapting Web Frameworks to PSGI
9
CGI on Plack
10
Middleware
11
Plack::Builder
12
Multiple Apps
13
Testing
14
Requests
15
Authentication
16
JSON
17
Serving Static Files
18
Conditonal Middleware
19
Cascade Apps
20
App Local on Internet
21
Lintify
22
More Middleware
23
Write Middleware
24
Reachout Beyond
25 26
27 28 29 30 31

1. Getting Plack

Install

cpanm PSGI Plack

Highlights

  • PSGI: A specification of the interface (just documentation, no code)
  • Plack: An implementation of the specification (code)
  • plackup: a command-line tool for launching PSGI apps. It is part of the Plack toolkit
  • Minimum Perl Required: 5.8.1

2. Hello World

Here is about the most trivial PSGI app. one can write:

# app.psgi
my $psgi_app = sub return [ 200, ['Content-type''text/html'], [ 'Konichiwa World']]; }

Notice that the coderef returns: status, header, and body per the spec. (see PSGI::FAQ).

Then one can run the applicaton as follows:

plackup app.psgi

By default plackup starts the app up on port 5000, but you can change it with -p $port_number

3. Using plackup

plackup:

  • is a command-line launcher of PSGI applications - runs your .psgi app (coderef) against whatever backend you choose (using Plack handlers)
  • defaults to run the file named app.psgi in current directory if no app specified on the command line
  • makes backend decisions: defaults to Standalone - HTTP::Server::PSGI, but will use environment variables and Module names to choose a more appropriate backend. One can always state explicitly via -s $backend_server e.g. -s Starman
  • defaults to have the following Middleware enabled: AccessLog and StackTrace. This can be controlled by -E (environment switch).
  • does port or socket listening.

4. Reloading Applications

During development it’s nice to have the backend server reload files when they change so one doesn’t have to restart the server manually. plackup can handle this with the -r option.

  • -r option to watch .psgi and .pm files for changes and reload the app as necessary. Default: watch under current directory, but one can specify a (comma separated) list of directories to watch using -R
  • caveats with reload: set the backend server explicitly with -s because backend server auto-detection mode may fail while using reload mode.
  • Shotgun -L, reload on every request (works damn well, but slow. Make sure you’re not using this option in production).
  • preload non-changing modules with: @Shotgun -L -MModule -MOtherModule

5. A Static File Web Server

Plack::App::File

Here’s a tiny app to serve up some HTML files:

# my_html_server.psgi
use Plack::App::File;
my $app = Plack::App::File->new(root => "$ENV{HOME}/public_html");

Run it:

plackup my_html_server.psgi

Plack::App::Directory

This module is similar to Plack::App::File, but includes a directory listing as well. Here is how one can serve up a directory hierarchy in a one liner:

 plackup -MPlack::App::Directory  -e 'Plack::App::Directory->new(root => "$ENV{HOME}/www");'
My tags:
 
Popular tags:
 
Powered by MojoMojo