37. Who’s on board
Benjamin Trott, Yuval Kogman, Stevan Little, Shawn M
Moore, Mark Stosberg, Matt S Trout, Jesse Vincent,
Chia-liang Kao, Dave Rolsky, John Beppu ...
64. 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;
};
69. 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;
};
}
71. CGI::Emulate::PSGI
CGI::Compile
Easiest migration from CGI scripts (like Registry)
72. Plack::Request
like libapreq (Apache::Request)
wrapper APIs for framework developers
73. use Plack::Request;
my $app = sub {
my $req = Plack::Request->new(shift);
my $body = “Hello “ . $req->param(‘n’);
my $res = $req->new_response(200);
$res->content_type(‘text/plain’);
$res->body($body);
return $res->finalize;
};
75. 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;
};
76. 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;
};
78. # Pull streaming for blocking server
use IO::Handle::Util qw(io_from_getline);
my $app = sub {
my $env = shift;
my $io = io_from_getline sub {
return $chunk; # undef when done
};
return [ $status, $header, $io ];
};
79. # Push streaming for non-blocking server
use AnyEvent;
use JSON;
my $app = sub {
my $env = shift;
return sub {
my $respond = shift;
my $w = $respond->([ 200, $headers ]);
AnyEvent::Example->fetch(sub {
$w->write(JSON::encode_json($_[0]));
$w->close;
});
};
};
91. Summary
• PSGI is an interface, Plack is the code.
• We have many (pretty fast) servers.
• We have adapters and tools for most web
frameworks.
• Use it!