Top Banner
Plack Superglue for Perl Web Frameworks Tatsuhiko Miyagawa YAPC::NA 2010
138

Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

May 24, 2018

Download

Documents

vuongnhu
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PlackSuperglue for Perl Web Frameworks

Tatsuhiko MiyagawaYAPC::NA 2010

Page 2: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Tatsuhiko Miyagawa

• Lives in San Francisco

• Software Engineer @ Six Apart

• http://search.cpan.org/~miyagawa/

• @miyagawa

• http://bulknews.typepad.com/

Page 3: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Web Applications

Page 4: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Hello World

Page 5: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

#!/usr/bin/perluse strict;

print “Content‐Type: text/plain\r\n\r\n”;print “Hello World”;

Page 6: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

use FCGI;

my $req = FCGI::Request();while ($req‐>Accept >= 0) {  print “Content‐Type: text/plain\r\n\r\n”;  print “Hello World”;}

Page 7: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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;

Page 8: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

sub handle_request {    my($self, $cgi) = @_;    print “HTTP/1.0 200 OK\r\n”;    print “Content‐Type: text/plain\r\n\r\n”;    print “Hello World”;}

1;

Page 9: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

All similar but slightly different

Page 10: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Painful to supportall of them :(

Page 11: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

There is (was)one common way to do all of this.

Page 12: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

#!/usr/bin/perluse CGI;

my $q = CGI‐>new;

print $q‐>header(‘text/plain’);print “Hello World”;

Page 13: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Happens to work on:CGI, FastCGI, mod_perl

(HTTP::Server::Simple::CGI)

Page 14: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 15: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI.pm?meh

Page 16: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Frameworks to the rescue!

Page 17: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

Page 18: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

MANYweb frameworks

Page 19: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Let’s look how they handle web servers.

Page 20: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 21: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

Page 22: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

Page 23: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 24: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty

Page 25: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Page 26: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

Page 27: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

nginx

Page 28: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

nginxHTTP::Server::Simple

Page 29: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Catalyst::Engine

nginxHTTP::Server::Simple

Page 30: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Mason::CGIHandlerCatalyst::Engine

nginxHTTP::Server::Simple

Page 31: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Gross.

Page 32: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI.pmJifty, CGI::Application, Spoon

mod_perl centricMason, Sledge, PageKit, WebGUI

AdaptersCatalyst, Maypole, Squatting

Page 33: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

That was 2008.

Page 34: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Steal great ideafrom Python/Ruby

Page 35: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

WSGI (Python)Rack (Ruby)

Page 36: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

WSGI (PEP-333)

Page 37: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious
Page 38: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# WSGIdef hello(environ, start_response):  start_response(“200 OK”, [    (‘Content‐Type’, ‘text/plain’)  ])  return [“Hello World”]

Page 39: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

WSGI• Django

• Bottle

• CherryPy

• Tornado

• Pylons

• Flask

• mod_wsgi

• Paste

• gunicorn

• uWSGI

• wsgiref

• Google AppEngine

Page 40: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

WSGI

WSGI middleware

Django Bottle Flask Tornado

Apache lighttpd nginx mod_wsgi

wsgi handlers

GAE

Page 41: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Rack

Page 42: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious
Page 43: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Rackclass Hello  def call(env)    return [       200,       { “Content‐Type” => ”text/plain” },        [“Hello World”]    ]  endend

Page 44: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Rack• Rails

• Merb

• Sinatra

• Camping

• Ramaze

• etc.

• Unicorn

• Thin

• Mongrel

• Rainbows!

• Phusion Passenger

• Heroku

Page 45: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Rack

Rack middleware

Rails Merb Sinatra Ramaze

Apache lighttpd Thin Unicorn

Rack handlers

Mongrel

Page 46: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGIPerl Web Server Gateway Interface

Page 47: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Interface

Page 48: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# WSGIdef hello(environ, start_response):  start_response(“200 OK”, [    (‘Content‐Type’, ‘text/plain’)  ])  return [“Hello World”]

Page 49: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Rackclass Hello  def call(env)    return [       200,       { “Content‐Type” => ”text/plain” },        [“Hello World”]    ]  endend

Page 50: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# PSGImy $app = sub {    my $env = shift;    return [         200,        [ ‘Content‐Type’, ‘text/plain’ ],        [ ‘Hello World’ ],    ];};

Page 51: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI applicationcode reference$app = sub {...};

Page 52: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

CGI-like environment variables+ psgi.input, psgi.errors etc.

Page 53: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

Status code (int.): 200, 404 etc.

Page 54: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

Array reference of header pairs:[ ‘Content-Type’, ‘text/html’, ... ]

Page 55: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

Array reference of content chunksFilehandle or IO::Handle-ish obejct

Page 56: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

That’s it.(There’s a callback based streaming interface as well)

Page 57: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# PSGImy $app = sub {    my $env = shift;    return [         200,        [ ‘Content‐Type’, ‘text/plain’ ],        [ ‘Hello World’ ],    ];};

Page 58: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Now you’ve gota PSGI application.

Page 59: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI makes itso simple to:

Page 60: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Write a new Perlweb app & framework

Page 61: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Write a new Perlweb server

Page 62: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Mason::CGIHandlerCatalyst::Engine

nginxHTTP::Server::Simple

Page 63: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI

Plack::Middleware

Catalyst CGI::App Jifty Tatsumaki

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Page 64: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI adaptation

Page 65: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Tripletail Konstrukt Reaction Jifty Cyclone3 WebGUIOpenInteract Squatting Dancer CGI::Application

Nanoa Ark Angelos Noe Schenker Tatsumaki Amon Apache2::WebApp Web::Simple Apache2::REST

SweetPea Hydrant Titanium

Page 66: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Tripletail Konstrukt Reaction Jifty Cyclone3 WebGUIOpenInteract Squatting Dancer CGI::Application

Nanoa Ark Angelos Noe Schenker Tatsumaki Amon Apache2::WebApp Web::Simple Apache2::REST

SweetPea Hydrant Titanium

Page 67: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

ApplicationsMovable Type 6, WebGUI 8

Page 68: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Catalystuse MyApp;

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

# $app is a PSGI app!

Page 69: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Jiftyuse MyPonyApp;my $app = MyPonyApp‐>psgi_app;

# $app is a PSGI app!

Page 70: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Danceruse Dancer;

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

dance; # returns a PSGI app!

Page 71: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Mojolicious::Liteuse Mojolicious::Lite;

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

app‐>start; # returns PSGI app

Page 72: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

# Web::Simpleuse Web::Simple ‘MyApp’;

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

my $app = MyApp‐>as_psgi;

# $app is a PSGI app!

Page 73: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

perldoc PSGIIf you want to adapt your framework to PSGI.

Page 74: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack“PSGI toolkit”

Page 75: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

HTTP::Server::PSGIReference PSGI web server

bundled in Plack

Page 76: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PlackupRuns PSGI app instantly from CLI

(inspired by rackup)

Page 77: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

> plackup app.psgi

Page 78: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::HandlerConnects PSGI apps to Web servers

CGI, FastCGI, Apache, SCGI

Page 79: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI Web Servers

Page 80: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

StarmanUNIX Preforking HTTP servers (like Unicorn.rb)

HTTP/1.1 chunk + keep-alives / Very Fast

Page 81: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

StarletSimpler UNIX HTTP/1.0 Server

Best used with Server::Starter and nginx/lighttpd

Page 82: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

TwiggyNon-blocking web server (like Thin.rb)

based on AnyEvent framework

Page 83: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CoronaCoroutine for each connection based on Coro.pm

Page 84: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

HTTP::Server::Simple::PSGIZero-deps other than HTTP::Server::Simple

Best for embedding PSGI applications

Page 85: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

uWSGIhttp://projects.unbit.it/uwsgi/

Page 86: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Perlbal pluginhttp://github.com/miyagawa/Perlbal-Plugin-PSGI

Page 87: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

nginx embedded perlhttp://github.com/yappo/nginx-psgi-patchs

Page 88: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

mod_psgihttp://github.com/spiritloose/mod_psgi

Page 89: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

evpsgihttp://github.com/sekimura/evpsgi

Page 90: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Feersumhttp://github.com/stash/Feersum

Page 91: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI

Plack::Middleware

Catalyst CGI::App Jifty Tatsumaki

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Starman Twiggy uWSGI Corona evpsgi

Page 92: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Middleware

Page 93: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious
Page 94: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI MiddlewareWraps a PSGI applicationto add pre/post processing

Page 95: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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;};

Page 96: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

PSGI Middlewarecoderef -> coderef

Higher-order functions

Page 97: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

MiddlewareDebug, Session, Logger, Runtime, Static, Lint,AccessLog, ConditionalGET, ErrorDocument,

StackTrace, Auth::Basic, Auth::Digest, ReverseProxy, Refresh, Auth::OAuth, Hippie, Throttle

Page 98: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::Middlewarereusable and extensible Middleware framework

Plack::Builder DSL in .psgi

Page 99: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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

use Plack::Builder;

builder {    enable “Static”, root => “/htdocs”,      path => qr!^/static/!;    enable “Deflater”; # gzip/deflate    $app;}

Page 100: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

plackup compatibleplackup -e ‘enable “Foo”;’ app.psgi

Page 101: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

MiddlewareWrite once, run in every framework

Page 102: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

DEMO

Page 103: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::URLMapMultiplex multiple apps

Integrated with Builder DSL

Page 104: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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;  };}

Page 105: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::PSGIEasy migration from CGI.pm

Page 106: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

CGI::Emulate::PSGICGI::Compile

Easiest migration from CGI scripts (like Registry)

Page 107: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::Requestlike libapreq (Apache::Request)

wrapper APIs for middleware developers

Page 108: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::TestUnified interface to write tests

with Mock HTTP and Live HTTP

Page 109: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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;};

Page 110: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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;};

Page 111: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::*ready-to-use applications

Page 112: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::DirectoryStatic content file server

Page 113: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::Proxy(non-blocking) proxy server

Can be used as reverse proxy as well

Page 114: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::CGIBinmount /cgi-bin as PSGI applications

Page 115: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::FCGIDispatcher

Connect to FCGI daemon(even in Ruby, Python, C)

Page 116: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Plack::App::JSPRuns JavaScript PSGI apps :)

Page 117: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Testimonialsa.k.a. self-promotion

Page 118: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

“I love this! It’s exactly the right answer to what i was looking for.” - Benjamin Trott

Page 119: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

“Wow, this is nothing short of awesome.”- Stevan Little

Page 120: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

“Plack is so simple, easy and powerful.”- Jesse Vincent

Page 121: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

“Plack rocks. Miyagawa rocks.”- Piers Cawley

Page 122: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

“Is he on too many drugs or too few? Either way let’s make sure that never changes.”

- Matt S. Trout

Page 123: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Real WorldPlack/PSGI

Page 124: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious
Page 125: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

You should join!

Page 126: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

> cpanm Plack> cpanm Task::Plack

Page 127: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Cloud?(Heroku, GAE)

Page 128: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Sunabahttp://sunaba.plackperl.org/

Page 129: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious
Page 130: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Runs on dankogai’sSandbox

Page 131: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

You can even try:

system(“rm ‐fr /”);while (1) { }

Page 132: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

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!

Page 133: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Remember:Plack is < 1y old

Page 134: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Lots of (exciting)things going on.

Page 135: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

http://blog.plackperl.org/

Page 136: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

http://github.com/miyagawa/Plackhttp://plackperl.org/

irc://irc.perl.org/#plack

Page 137: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Questions?

Page 138: Plack - O'Reilly Mediaassets.en.oreilly.com/1/event/45/Plack_ State of the Art Web...Maypole Mason Mojo Sledge Catalyst Spoon PageKit AxKit Egg Gantry Continuity Solstice Mojolicious

Thank you!Slides: http://slideshare.net/miyagawa