SlideShare a Scribd company logo
Plack
Superglue for Perl Web Frameworks
         Tatsuhiko Miyagawa
           YAPC::NA 2010
Tatsuhiko Miyagawa

• Lives in San Francisco
• Software Engineer @ Six Apart
• http://search.cpan.org/~miyagawa/
• @miyagawa
• http://bulknews.typepad.com/
Web Applications
Hello World
#!/usr/bin/perl
use strict;

print “Content-Type: text/plainrnrn”;
print “Hello World”;
use FCGI;

my $req = FCGI::Request();
while ($req->Accept >= 0) {
  print “Content-Type: text/plainrnrn”;
  print “Hello World”;
}
package HelloWorld;
use strict;
use Apache::RequestRec;
use Apache::RequestIO;
use Apache::Const -compile => qw(OK);

sub handler {
  my $r = shift;

     $r->content_type(‘text/plain’);
     $r->print(“Hello World”);

     return Apache::Const::OK;
}

1;
package HelloWorld;
use base qw(HTTP::Server::Simple::CGI);

sub handle_request {
  my($self, $cgi) = @_;
  print “HTTP/1.0 200 OKrn”;
  print “Content-Type: text/plainrnrn”;
  print “Hello World”;
}

1;
All similar
but slightly different
There is (was)
one common way
 to do all of this.
#!/usr/bin/perl
use CGI;

my $q = CGI->new;

print $q->header(‘text/plain’);
print “Hello World”;
Happens to work on:
    CGI, FastCGI, mod_perl
  (HTTP::Server::Simple::CGI)
CGI.pm?
  meh
Frameworks
to the rescue!
Maypole Mason Mojo Sledge Catalyst Spoon
 PageKit AxKit Egg Gantry Continuity Solstice
 Mojolicious Tripletail Konstrukt Reaction Jifty
  Cyclone3 WebGUI OpenInteract Squatting
 Dancer CGI::Application Nanoa Ark Angelos
        Noe Schenker Tatsumaki Amon
Apache2::WebApp Web::Simple Apache2::REST
          SweetPea Hydrant Titanium
MANY
web frameworks
CGI.pm
  Jifty, CGI::Application, Spoon


mod_perl centric
Mason, Sledge, PageKit, WebGUI


       Adapters
  Catalyst, Maypole, Squatting
That was 2008.
Steal great idea
from Python/Ruby
WSGI (Python)
 Rack (Ruby)
WSGI (PEP-333)
Plack at YAPC::NA 2010
# WSGI
def hello(environ, start_response):
 start_response(“200 OK”, [
   (‘Content-Type’, ‘text/plain’)
 ])
 return [“Hello World”]
WSGI
•   Django       •   mod_wsgi

•   Bottle       •   Paste

•   CherryPy     •   gunicorn

•   Tornado      •   uWSGI

•   Pylons       •   wsgiref

•   Flask        •   Google AppEngine
Django              Bottle         Flask        Tornado


                                             WSGI middleware

                             WSGI


             wsgi handlers


Apache     lighttpd          nginx   mod_wsgi          GAE
Rack
Plack at YAPC::NA 2010
# Rack
class Hello
 def call(env)
   return [
     200,
     { “Content-Type” => ”text/plain” },
     [“Hello World”]
   ]
 end
end
Rack
•   Rails       •   Unicorn

•   Merb        •   Thin

•   Sinatra     •   Mongrel

•   Camping     •   Rainbows!

•   Ramaze      •   Phusion Passenger

•   etc.        •   Heroku
Rails              Merb          Sinatra     Ramaze


                                         Rack middleware

                             Rack


             Rack handlers


Apache     lighttpd          Thin    Unicorn      Mongrel
PSGI
Perl Web Server Gateway Interface
Interface
# WSGI
def hello(environ, start_response):
 start_response(“200 OK”, [
   (‘Content-Type’, ‘text/plain’)
 ])
 return [“Hello World”]
# Rack
class Hello
 def call(env)
   return [
     200,
     { “Content-Type” => ”text/plain” },
     [“Hello World”]
   ]
 end
end
# PSGI
my $app = sub {
   my $env = shift;
   return [
      200,
      [ ‘Content-Type’, ‘text/plain’ ],
      [ ‘Hello World’ ],
   ];
};
PSGI application
   code reference
   $app = sub {...};
my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};
environment hash
$env: CGI-like env variables
+ psgi.input, psgi.errors etc.
my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};
Response
 array ref with three elements
status code, headers (array ref)
and body (IO-like or array ref)
my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};
$body
  IO::Handle-like
getline() and close()
That’s it.
# PSGI
my $app = sub {
   my $env = shift;
   return [
      200,
      [ ‘Content-Type’, ‘text/plain’ ],
      [ ‘Hello World’ ],
   ];
};
Now you’ve got
a PSGI application.
PSGI makes it
so simple to:
Write a new Perl
web app & framework
Write a new Perl
 web server
Catalyst            CGI::App              Jifty        Tatsumaki


                                                    Plack::Middleware

                                PSGI


    Plack::Handler::* (CGI, FCGI, Apache)


Apache       lighttpd       HTTP::Server::PSGI      mod_psgi   Perlbal
PSGI adaptation
Maypole Mason Mojo Sledge Catalyst Spoon PageKit
 AxKit Egg Gantry Continuity Solstice Mojolicious
Tripletail Konstrukt Reaction Jifty Cyclone3 WebGUI
  OpenInteract Squatting Dancer CGI::Application
 Nanoa Ark Angelos Noe Schenker Tatsumaki Amon
   Apache2::WebApp Web::Simple Apache2::REST
             SweetPea Hydrant Titanium
Maypole Mason Mojo Sledge Catalyst Spoon PageKit
 AxKit Egg Gantry Continuity Solstice Mojolicious
Tripletail Konstrukt Reaction Jifty Cyclone3 WebGUI
  OpenInteract Squatting Dancer CGI::Application
 Nanoa Ark Angelos Noe Schenker Tatsumaki Amon
   Apache2::WebApp Web::Simple Apache2::REST
             SweetPea Hydrant Titanium
Applications
Movable Type 6, WebGUI 8
# Catalyst
use MyApp;

MyApp->setup_engine(‘PSGI’);
my $app = sub { MyApp->run(@_) };

# $app is a PSGI app!
# Jifty
use MyPonyApp;
my $app = MyPonyApp->psgi_app;

# $app is a PSGI app!
# Dancer
use Dancer;

get ‘/’ => sub {
   “Hello World”;
};

dance; # returns a PSGI app!
# Mojolicious::Lite
use Mojolicious::Lite;

get ‘/:name’ => sub {
   my $self = shift;
   $self->render_text(‘Hello!’);
};

shagadelic; # returns PSGI app
# Web::Simple
use Web::Simple ‘MyApp’;

package MyApp;
dispatch {
  sub(GET) {
    [ 200, [...], [ ‘Hello’ ] ];
  }
};

my $app = MyApp->as_psgi;

# $app is a PSGI app!
Plack
“PSGI toolkit”
HTTP::Server::PSGI
 Reference PSGI web server
      bundled in Plack
Plackup
Runs PSGI app instantly from CLI
      (inspired by rackup)
> plackup app.psgi
Plack::Handler
Connects PSGI apps to Web servers
   CGI, FastCGI, Apache, SCGI
PSGI Web Servers
Starman
UNIX Preforking HTTP servers (like Unicorn.rb)
  HTTP/1.1 chunk + keep-alives / Very Fast
Starlet
        Simpler UNIX HTTP/1.0 Server
Best used with Server::Starter and nginx/lighttpd
Twiggy
Non-blocking web server (like Thin.rb)
   based on AnyEvent framework
Corona
Coroutine for each connection based on Coro.pm
HTTP::Server::Simple::PSGI
 Zero-deps other than HTTP::Server::Simple
    Best for embedding PSGI applications
uWSGI
http://projects.unbit.it/uwsgi/
Perlbal plugin
http://github.com/miyagawa/Perlbal-Plugin-PSGI
nginx embedded perl
 http://github.com/yappo/nginx-psgi-patchs
mod_psgi
http://github.com/spiritloose/mod_psgi
evpsgi
http://github.com/sekimura/evpsgi
Feersum
http://github.com/stash/Feersum
Catalyst            CGI::App               Jifty        Tatsumaki


                                                     Plack::Middleware

                                PSGI


    Plack::Handler::* (CGI, FCGI, Apache)


Apache       lighttpd       HTTP::Server::PSGI       mod_psgi     Perlbal


Starman      Twiggy        uWSGI            Corona       evpsgi
Middleware
Plack at YAPC::NA 2010
PSGI Middleware
 Wraps a PSGI application
to add pre/post processing
my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};

my $mw = sub {
   my $env = shift;
   # do something with $env
   my $res = $app->($env);
   # do something with $res;
   return $res;
};
PSGI Middleware
  coderef -> coderef
 Higher-order functions
Middleware
   Debug, Session, Logger, Runtime, Static, Lint,
   AccessLog, ConditionalGET, ErrorDocument,
StackTrace, Auth::Basic, Auth::Digest, ReverseProxy,
      Refresh, Auth::OAuth, Hippie, Throttle
Plack::Middleware
  reusable and extensible
  Middleware framework
 Plack::Builder DSL in .psgi
my $app = sub {
   return [ $status, $header, $body ];
};

use Plack::Builder;

builder {
  enable “Static”, root => “/htdocs”,
    path => qr!^/static/!;
  enable “Deflater”; # gzip/deflate
  $app;
}
plackup compatible
plackup -e ‘enable “Foo”;’ app.psgi
Middleware
Write once, run in every framework
DEMO
Plack::App::URLMap
    Multiplex multiple apps
 Integrated with Builder DSL
use CatApp;
use CGIApp;

my $c1 = sub { CatApp->run };
my $c2 = sub { CGIApp->run_psgi };

use Plack::Builder;

builder {
  mount “/cat” => $c1;
  mount “/cgi-app” => builder {
    enable “StackTrace”;
    $c2;
  };
}
CGI::PSGI
Easy migration from CGI.pm
CGI::Emulate::PSGI
    CGI::Compile
Easiest migration from CGI scripts (like Registry)
Plack::Request
    like libapreq (Apache::Request)
wrapper APIs for middleware developers
Plack::Test
 Unified interface to write tests
with Mock HTTP and Live HTTP
use Plack::Test;
use HTTP::Request::Common;

my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};

test_psgi app => $app, client => sub {
   my $cb = shift;
   my $req = GET “http://localhost/foo”;
   my $res = $cb->($req);
   # test $res;
};
use Plack::Test;
use HTTP::Request::Common;
$Plack::Test::Impl = “Server”;

my $app = sub {
   my $env = shift;
   return [ $status, $header, $body ];
};

test_psgi app => $app, client => sub {
   my $cb = shift;
   my $req = GET “http://localhost/foo”;
   my $res = $cb->($req);
   # test $res;
};
Plack::App::*
ready-to-use applications
Plack::App::Directory
   Static content file server
Plack::App::Proxy
    (non-blocking) proxy server
Can be used as reverse proxy as well
Plack::App::CGIBin
mount /cgi-bin as PSGI applications
Plack::App::FCGIDispatcher
   Connect to FCGI daemon
   (even in Ruby, Python, C)
Plack::App::JSP
Runs JavaScript PSGI apps :)
Real World
Plack/PSGI
Plack at YAPC::NA 2010
You should join!
> cpanm Plack
> cpanm Task::Plack
Haven’t used cpanm?
Come to Nationwide (Cartoon 2)
       Wednesday 9:30
Cloud?
(Heroku, GAE)
Sunaba
http://sunaba.plackperl.org/
Plack at YAPC::NA 2010
Runs on dankogai’s
    Sandbox
You can even try:
 system(“rm -fr /”);
     while (1) { }
Summary

• PSGI is an interface, Plack is the code.
• We have many (pretty fast) PSGI servers.
• We have adapters and tools for most web
  frameworks.
• Use it!
Remember:
Plack is < 1y old
Lots of (exciting)
 things going on.
http://blog.plackperl.org/
http://github.com/miyagawa/Plack
        http://plackperl.org/
      irc://irc.perl.org/#plack
Questions?

More Related Content

Plack at YAPC::NA 2010

Editor's Notes