Intro to PSGI and Plack

Tatsuhiko Miyagawa
Tatsuhiko MiyagawaSoftware Engineer
PSGI and Plack
   Tatsuhiko Miyagawa
PSGI
Perl Web Server Gateway Interface
Plack
PSGI ref. implementations
          Utilities
A Perl port of:
Python’s WSGI and Ruby’s Rack
  (will talk about them later)
WHY
Web Frameworks
Maypole Mason Mojo Sledge Catalyst Spoon PageKit
  AxKit Egg Gantry Continuity Solstice Mojolicious
    Tripletail Konstrukt Reflection Jifty Cyclone3
OpenInteract Squatting Dancer CGI::Application Nanoa
              Ark Angelos Noe Schenker
Most of them run on
 mod_perl and CGI
Some run on FCGI
Some run standalone
Very few supports
  non-blocking
Because:
No common server
environment layers
CGI.pm
Runs fine on:
    CGI, FastCGI, mod_perl (1 & 2)
Standalone (with HTTP::Server::Simple)
CGI.pm = LCD
   It’s also Perl core
> grep ‘(CGI.pm|ENV)’ lib/MT/App.pm
         if ( my $path_info = $ENV{PATH_INFO} ) {
                # defined which interferes with CGI.pm determining the
                delete $ENV{PATH_INFO};
  # CGI.pm has this terrible flaw in that if a POST is in effect,
         my $query_string = $ENV{'QUERY_STRING'}
             if defined $ENV{'QUERY_STRING'};
         $query_string ||= $ENV{'REDIRECT_QUERY_STRING'}
             if defined $ENV{'REDIRECT_QUERY_STRING'};
         my $len = $ENV{CONTENT_LENGTH} || 0;
      return $ENV{ 'HTTP_' . $key };
         $app->{request_method} = $ENV{REQUEST_METHOD} || '';
      ## Older versions of CGI.pm didn't have an 'upload' method.
  if ( my $host = $ENV{HTTP_HOST} ) {
    : $ENV{REMOTE_ADDR});
      $cwd = $ENV{DOCUMENT_ROOT} || $app->mt_dir;
Not cool.
Catalyst
The most popular framework as of today
Catalyst::Engine::*
            Server abstractions.
Well supported Apache, FCGI and Standalone
Problems:
       Duplicated efforts
No fair performance evaluations
Catalyst::Engine
for the rest of us
= HTTP::Engine
HTTP::Engine
Lots of adapters (FCGI, Apache2, POE)
     Clean Request/Response API
Written by Yappo, tokuhirom and others
Problem
Mo[ou]se everywhere
 Mouse is light but still overspec for some env.
Monolithic
All implementations share HTTP::Engine roles
and builders, which is sometimes hard to adapt
      and has less place for optimizations.
APIs everywhere
Most frameworks have their request/response API
          Sometimes there are gaps.
    Annoying to write bridges and wrappers
Solution
Steal good stuff
from Python/Ruby
WSGI (Python)
   Rack
WSGI (PEP-333)
mod_wsgi, Tornado, Paste, GAE
  Django, CherryPy, Pylons
Rack
Passenger, Thin, rack, Heroku
     Rails, Merb, Sinatra
Split HTTP::Engine
 into three parts
Interface
Implementations
    Utilities
PSGI (interface)
Plack::Server (implementations)
      Plack::* (utilities)
PSGI
Interface
my $app = sub {
   my $env = shift;
   # ...
   return [ $status, $header, $body ];
};
PSGI application
   code reference
   $app = sub {...};
environment hash
$env: CGI-like env variables
+ psgi.input, psgi.errors etc.
Why a big hash?
Easy to adapt if you have CGI adapter
  Also follows what WSGI/Rack do
Response
 array ref with three elements
status code, headers (array ref)
and body (IO-like or array ref)
$body
  IO::Handle-like
getline() and close()
IO::Handle-like
       We really envy Python/Ruby
          for built-in iterators
(Perl’s filehandle is also an object, but it really sucks)
Frameworks
Write an adapter to return
PSGI application code ref.
Servers
   Set up $env, run the app
and emits response out of $res
Middleware
   Plays both side
“Application wrapper”
Plack
Plack::Server
 reference server implementations
  Standalone, FCGI, Apache2, CGI
Standalone, Prefork, AnyEvent, Coro
Very fast
 5000 QPS on standalone
15000 QPS with prefork :)
Framework Adapters
  CGI::Application, Catalyst, Maypole
Mason, Squatting, Mojo, HTTP::Engine etc.
Applications
MT::App, WebGUI
Utilities
Plackup
Run PSGI app instantly from CLI
     (inspired by rackup)
DEMO
Middlewares
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;
};
Middlewares
Static, AccessLog, ConditionalGET
 ErrorDocument, StackTrace etc.
Plack::Middleware
  reusable and extensible
  Middleware framework
 Plack::Builder DSL in .psgi
CGI::PSGI
Easy migration from CGI.pm
Plack::Request
    like libapreq (Apache::Request)
wrapper APIs for framework 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;
};
Streaming
event loop / long-poll
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 ];
};
use IO::Writer; # TBD: API might change
use AnyEvent::Timer;

my $app = sub {
   my $env = shift;
   my $io = writer {
      my $h = shift;
      my $t; $t = AE::timer 0, 1, sub {
         $t;
         $h->push_write($stuff);
      };
   };
   return [ $status, $header, $io ];
};
Other Servers
nginx embedded perl
 http://github.com/yappo/nginx-psgi-patchs
mod_psgi
http://github.com/spiritloose/mod_psgi
Gearman Dispatcher?
Cloud
WSGI (PEP-333)
mod_wsgi, Tornado, Paste, GAE
  Django, CherryPy, Pylons
Rack
Passenger, Thin, rack, Heroku
     Rails, Merb, Sinatra
What if GAE Perl
comes with PSGI ...
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!
http://github.com/miyagawa/Plack
        http://plackperl.org/
Questions?
1 of 79

Recommended

Plack at YAPC::NA 2010 by
Plack at YAPC::NA 2010Plack at YAPC::NA 2010
Plack at YAPC::NA 2010Tatsuhiko Miyagawa
3.7K views117 slides
Plack - LPW 2009 by
Plack - LPW 2009Plack - LPW 2009
Plack - LPW 2009Tatsuhiko Miyagawa
2.4K views93 slides
Building a desktop app with HTTP::Engine, SQLite and jQuery by
Building a desktop app with HTTP::Engine, SQLite and jQueryBuilding a desktop app with HTTP::Engine, SQLite and jQuery
Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
4.2K views112 slides
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQuery by
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryRemedie: Building a desktop app with HTTP::Engine, SQLite and jQuery
Remedie: Building a desktop app with HTTP::Engine, SQLite and jQueryTatsuhiko Miyagawa
39.1K views131 slides
Plack at OSCON 2010 by
Plack at OSCON 2010Plack at OSCON 2010
Plack at OSCON 2010Tatsuhiko Miyagawa
42K views138 slides
Tatsumaki by
TatsumakiTatsumaki
TatsumakiTatsuhiko Miyagawa
12.5K views59 slides

More Related Content

What's hot

PSGI and Plack from first principles by
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principlesPerl Careers
2K views42 slides
Plack basics for Perl websites - YAPC::EU 2011 by
Plack basics for Perl websites - YAPC::EU 2011Plack basics for Perl websites - YAPC::EU 2011
Plack basics for Perl websites - YAPC::EU 2011leo lapworth
18K views323 slides
Deploying Plack Web Applications: OSCON 2011 by
Deploying Plack Web Applications: OSCON 2011Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011Tatsuhiko Miyagawa
8.2K views143 slides
Modern Perl by
Modern PerlModern Perl
Modern PerlDave Cross
6.6K views107 slides
Psgi Plack Sfpm by
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpmsom_nangia
1.8K views92 slides
A reviravolta do desenvolvimento web by
A reviravolta do desenvolvimento webA reviravolta do desenvolvimento web
A reviravolta do desenvolvimento webWallace Reis
932 views83 slides

What's hot(20)

PSGI and Plack from first principles by Perl Careers
PSGI and Plack from first principlesPSGI and Plack from first principles
PSGI and Plack from first principles
Perl Careers2K views
Plack basics for Perl websites - YAPC::EU 2011 by leo lapworth
Plack basics for Perl websites - YAPC::EU 2011Plack basics for Perl websites - YAPC::EU 2011
Plack basics for Perl websites - YAPC::EU 2011
leo lapworth18K views
Deploying Plack Web Applications: OSCON 2011 by Tatsuhiko Miyagawa
Deploying Plack Web Applications: OSCON 2011Deploying Plack Web Applications: OSCON 2011
Deploying Plack Web Applications: OSCON 2011
Tatsuhiko Miyagawa8.2K views
Modern Perl by Dave Cross
Modern PerlModern Perl
Modern Perl
Dave Cross6.6K views
Psgi Plack Sfpm by som_nangia
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
som_nangia1.8K views
A reviravolta do desenvolvimento web by Wallace Reis
A reviravolta do desenvolvimento webA reviravolta do desenvolvimento web
A reviravolta do desenvolvimento web
Wallace Reis932 views
No callbacks, No Threads - Cooperative web servers in Ruby 1.9 by Ilya Grigorik
No callbacks, No Threads - Cooperative web servers in Ruby 1.9No callbacks, No Threads - Cooperative web servers in Ruby 1.9
No callbacks, No Threads - Cooperative web servers in Ruby 1.9
Ilya Grigorik12.8K views
Web Development in Perl by Naveen Gupta
Web Development in PerlWeb Development in Perl
Web Development in Perl
Naveen Gupta15.5K views
Using Sinatra to Build REST APIs in Ruby by LaunchAny
Using Sinatra to Build REST APIs in RubyUsing Sinatra to Build REST APIs in Ruby
Using Sinatra to Build REST APIs in Ruby
LaunchAny9.5K views
Information security programming in ruby by Hiroshi Nakamura
Information security programming in rubyInformation security programming in ruby
Information security programming in ruby
Hiroshi Nakamura1.2K views
Beyond Breakpoints: A Tour of Dynamic Analysis by Fastly
Beyond Breakpoints: A Tour of Dynamic AnalysisBeyond Breakpoints: A Tour of Dynamic Analysis
Beyond Breakpoints: A Tour of Dynamic Analysis
Fastly347 views
Web frameworks don't matter by Tomas Doran
Web frameworks don't matterWeb frameworks don't matter
Web frameworks don't matter
Tomas Doran593 views
Deploying Symfony | symfony.cat by Pablo Godel
Deploying Symfony | symfony.catDeploying Symfony | symfony.cat
Deploying Symfony | symfony.cat
Pablo Godel2.8K views
Lightweight Webservices with Sinatra and RestClient by Adam Wiggins
Lightweight Webservices with Sinatra and RestClientLightweight Webservices with Sinatra and RestClient
Lightweight Webservices with Sinatra and RestClient
Adam Wiggins24.1K views
"Swoole: double troubles in c", Alexandr Vronskiy by Fwdays
"Swoole: double troubles in c", Alexandr Vronskiy"Swoole: double troubles in c", Alexandr Vronskiy
"Swoole: double troubles in c", Alexandr Vronskiy
Fwdays874 views
Making Symofny shine with Varnish - SymfonyCon Madrid 2014 by Barel Barelon
Making Symofny shine with Varnish - SymfonyCon Madrid 2014Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Making Symofny shine with Varnish - SymfonyCon Madrid 2014
Barel Barelon14.7K views
Sinatra Rack And Middleware by Ben Schwarz
Sinatra Rack And MiddlewareSinatra Rack And Middleware
Sinatra Rack And Middleware
Ben Schwarz16.9K views

Viewers also liked

Carton CPAN dependency manager by
Carton CPAN dependency managerCarton CPAN dependency manager
Carton CPAN dependency managerTatsuhiko Miyagawa
4.1K views39 slides
Network Programming With Anyevent by
Network Programming With AnyeventNetwork Programming With Anyevent
Network Programming With AnyeventPedro Melo
2K views15 slides
CPAN Realtime feed by
CPAN Realtime feedCPAN Realtime feed
CPAN Realtime feedTatsuhiko Miyagawa
7.1K views27 slides
Wight: Phantom’s Perl friend - YAPC::Asia 2012 by
Wight: Phantom’s Perl friend - YAPC::Asia 2012Wight: Phantom’s Perl friend - YAPC::Asia 2012
Wight: Phantom’s Perl friend - YAPC::Asia 2012Hiroshi Shibamura
10.1K views60 slides
No Hugging, No Learning by
No Hugging, No LearningNo Hugging, No Learning
No Hugging, No LearningOlaf Alders
1.1K views84 slides
ZeroMQ in PHP by
ZeroMQ in PHPZeroMQ in PHP
ZeroMQ in PHPJosé Lorenzo Rodríguez Urdaneta
13.3K views34 slides

Similar to Intro to PSGI and Plack

Psgi Plack Sfpm by
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpmwilburlo
593 views92 slides
Clojure and the Web by
Clojure and the WebClojure and the Web
Clojure and the Webnickmbailey
1.9K views25 slides
How to build a High Performance PSGI/Plack Server by
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server Masahiro Nagano
18.6K views134 slides
Mojolicious - A new hope by
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hopeMarcus Ramberg
12.1K views98 slides
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani by
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswanivvaswani
39.9K views62 slides
PHP and COM by
PHP and COMPHP and COM
PHP and COMWez Furlong
4.3K views39 slides

Similar to Intro to PSGI and Plack(20)

Psgi Plack Sfpm by wilburlo
Psgi Plack SfpmPsgi Plack Sfpm
Psgi Plack Sfpm
wilburlo593 views
Clojure and the Web by nickmbailey
Clojure and the WebClojure and the Web
Clojure and the Web
nickmbailey1.9K views
How to build a High Performance PSGI/Plack Server by Masahiro Nagano
How to build a High Performance PSGI/Plack Server How to build a High Performance PSGI/Plack Server
How to build a High Performance PSGI/Plack Server
Masahiro Nagano18.6K views
Mojolicious - A new hope by Marcus Ramberg
Mojolicious - A new hopeMojolicious - A new hope
Mojolicious - A new hope
Marcus Ramberg12.1K views
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani by vvaswani
Creating REST Applications with the Slim Micro-Framework by Vikram VaswaniCreating REST Applications with the Slim Micro-Framework by Vikram Vaswani
Creating REST Applications with the Slim Micro-Framework by Vikram Vaswani
vvaswani39.9K views
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later by Haehnchen
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years laterSymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
SymfonyCon Berlin 2016 - Symfony Plugin for PhpStorm - 3 years later
Haehnchen1.7K views
Implementing Comet using PHP by King Foo
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
King Foo30.4K views
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber... by dantleech
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
Building and Incredible Machine with Pipelines and Generators in PHP (IPC Ber...
dantleech1.1K views
Incredible Machine with Pipelines and Generators by dantleech
Incredible Machine with Pipelines and GeneratorsIncredible Machine with Pipelines and Generators
Incredible Machine with Pipelines and Generators
dantleech264 views
Tasks: you gotta know how to run them by Filipe Ximenes
Tasks: you gotta know how to run themTasks: you gotta know how to run them
Tasks: you gotta know how to run them
Filipe Ximenes221 views
How and why i roll my own node.js framework by Ben Lin
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
Ben Lin3.3K views
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308) by Amazon Web Services
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
AWS re:Invent 2016: Chalice: A Serverless Microframework for Python (DEV308)
Amazon Web Services1.7K views
Flask and Angular: An approach to build robust platforms by Ayush Sharma
Flask and Angular:  An approach to build robust platformsFlask and Angular:  An approach to build robust platforms
Flask and Angular: An approach to build robust platforms
Ayush Sharma197 views
From Ruby to Node.js by jubilem
From Ruby to Node.jsFrom Ruby to Node.js
From Ruby to Node.js
jubilem1K views
Node js introduction by Alex Su
Node js introductionNode js introduction
Node js introduction
Alex Su2.7K views
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease by KAI CHU CHUNG
GDG Devfest 2019 - Build go kit microservices at kubernetes with easeGDG Devfest 2019 - Build go kit microservices at kubernetes with ease
GDG Devfest 2019 - Build go kit microservices at kubernetes with ease
KAI CHU CHUNG926 views

More from Tatsuhiko Miyagawa

cpanminus at YAPC::NA 2010 by
cpanminus at YAPC::NA 2010cpanminus at YAPC::NA 2010
cpanminus at YAPC::NA 2010Tatsuhiko Miyagawa
1.6K views31 slides
Asynchronous programming with AnyEvent by
Asynchronous programming with AnyEventAsynchronous programming with AnyEvent
Asynchronous programming with AnyEventTatsuhiko Miyagawa
6.5K views70 slides
Remedie OSDC.TW by
Remedie OSDC.TWRemedie OSDC.TW
Remedie OSDC.TWTatsuhiko Miyagawa
7.8K views45 slides
Why Open Matters It Pro Challenge 2008 by
Why Open Matters It Pro Challenge 2008Why Open Matters It Pro Challenge 2008
Why Open Matters It Pro Challenge 2008Tatsuhiko Miyagawa
7.9K views103 slides
20 modules i haven't yet talked about by
20 modules i haven't yet talked about20 modules i haven't yet talked about
20 modules i haven't yet talked aboutTatsuhiko Miyagawa
4.9K views78 slides
Web::Scraper for SF.pm LT by
Web::Scraper for SF.pm LTWeb::Scraper for SF.pm LT
Web::Scraper for SF.pm LTTatsuhiko Miyagawa
3.2K views33 slides

More from Tatsuhiko Miyagawa(15)

Intro to PSGI and Plack