Plack at OSCON 2010

25,892 views
23,368 views

Published on

Published in: Technology
0 Comments
15 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
25,892
On SlideShare
0
From Embeds
0
Number of Embeds
1,258
Actions
Shares
0
Downloads
130
Comments
0
Likes
15
Embeds 0
No embeds

No notes for slide










































































































































  • Plack at OSCON 2010

    1. Plack Superglue for Perl Web Frameworks Tatsuhiko Miyagawa YAPC::NA 2010
    2. Tatsuhiko Miyagawa • Lives in San Francisco • Software Engineer @ Six Apart • http://search.cpan.org/~miyagawa/ • @miyagawa • http://bulknews.typepad.com/
    3. Web Applications
    4. Hello World
    5. #!/usr/bin/perl use strict; print “Content-Type: text/plainrnrn”; print “Hello World”;
    6. use FCGI; my $req = FCGI::Request(); while ($req->Accept >= 0) { print “Content-Type: text/plainrnrn”; print “Hello World”; }
    7. 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;
    8. 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;
    9. All similar but slightly different
    10. Painful to support all of them :(
    11. There is (was) one common way to do all of this.
    12. #!/usr/bin/perl use CGI; my $q = CGI->new; print $q->header(‘text/plain’); print “Hello World”;
    13. Happens to work on: CGI, FastCGI, mod_perl (HTTP::Server::Simple::CGI)
    14. CGI.pm mod_perl CGI fastcgi Apache IIS lighttpd
    15. CGI.pm? meh
    16. Frameworks to the rescue!
    17. 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
    18. MANY web frameworks
    19. Let’s look how they handle web servers.
    20. CGI.pm mod_perl CGI fastcgi Apache IIS lighttpd
    21. CGI::Application Apache IIS lighttpd
    22. CGI::Application CGI.pm Apache IIS lighttpd
    23. CGI::Application CGI.pm mod_perl CGI fastcgi Apache IIS lighttpd
    24. CGI::Application Jifty CGI.pm mod_perl CGI fastcgi Apache IIS lighttpd
    25. CGI::Application Jifty Catalyst CGI.pm mod_perl CGI fastcgi Apache IIS lighttpd
    26. CGI::Application Jifty Catalyst CGI.pm Catalyst::Engine mod_perl CGI fastcgi Apache IIS lighttpd
    27. CGI::Application Jifty Catalyst CGI.pm Catalyst::Engine mod_perl CGI fastcgi Apache IIS lighttpd nginx
    28. CGI::Application Jifty Catalyst CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
    29. Mason CGI::Application Jifty Catalyst CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
    30. Mason CGI::Application Jifty Catalyst Mason::CGIHandler CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
    31. Gross.
    32. CGI.pm Jifty, CGI::Application, Spoon mod_perl centric Mason, Sledge, PageKit, WebGUI Adapters Catalyst, Maypole, Squatting
    33. That was 2008.
    34. Steal great idea from Python/Ruby
    35. WSGI (Python) Rack (Ruby)
    36. WSGI (PEP-333)
    37. # WSGI def hello(environ, start_response): start_response(“200 OK”, [ (‘Content-Type’, ‘text/plain’) ]) return [“Hello World”]
    38. WSGI • Django • mod_wsgi • Bottle • Paste • CherryPy • gunicorn • Tornado • uWSGI • Pylons • wsgiref • Flask • Google AppEngine
    39. Django Bottle Flask Tornado WSGI middleware WSGI wsgi handlers Apache lighttpd nginx mod_wsgi GAE
    40. Rack
    41. # Rack class Hello def call(env) return [ 200, { “Content-Type” => ”text/plain” }, [“Hello World”] ] end end
    42. Rack • Rails • Unicorn • Merb • Thin • Sinatra • Mongrel • Camping • Rainbows! • Ramaze • Phusion Passenger • etc. • Heroku
    43. Rails Merb Sinatra Ramaze Rack middleware Rack Rack handlers Apache lighttpd Thin Unicorn Mongrel
    44. PSGI Perl Web Server Gateway Interface
    45. Interface
    46. # WSGI def hello(environ, start_response): start_response(“200 OK”, [ (‘Content-Type’, ‘text/plain’) ]) return [“Hello World”]
    47. # Rack class Hello def call(env) return [ 200, { “Content-Type” => ”text/plain” }, [“Hello World”] ] end end
    48. # PSGI my $app = sub { my $env = shift; return [ 200, [ ‘Content-Type’, ‘text/plain’ ], [ ‘Hello World’ ], ]; };
    49. PSGI application code reference $app = sub {...};
    50. my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; CGI-like environment variables + psgi.input, psgi.errors etc.
    51. my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; Status code (int.): 200, 404 etc.
    52. my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; Array reference of header pairs: [ ‘Content-Type’, ‘text/html’, ... ]
    53. my $app = sub { my $env = shift; return [ $status, $header, $body ]; }; Array reference of content chunks Filehandle or IO::Handle-ish obejct
    54. That’s it. (There’s a callback based streaming interface as well)
    55. # PSGI my $app = sub { my $env = shift; return [ 200, [ ‘Content-Type’, ‘text/plain’ ], [ ‘Hello World’ ], ]; };
    56. Now you’ve got a PSGI application.
    57. PSGI makes it so simple to:
    58. Write a new Perl web app & framework
    59. Write a new Perl web server
    60. Mason CGI::Application Jifty Catalyst Mason::CGIHandler CGI.pm Catalyst::Engine mod_perl CGI fastcgi HTTP::Server ::Simple Apache IIS lighttpd nginx
    61. Catalyst CGI::App Jifty Tatsumaki Plack::Middleware PSGI Plack::Handler::* (CGI, FCGI, Apache) Apache lighttpd HTTP::Server::PSGI mod_psgi Perlbal
    62. PSGI adaptation
    63. 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
    64. 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
    65. Applications Movable Type 6, WebGUI 8
    66. # Catalyst use MyApp; MyApp->setup_engine(‘PSGI’); my $app = sub { MyApp->run(@_) }; # $app is a PSGI app!
    67. # Jifty use MyPonyApp; my $app = MyPonyApp->psgi_app; # $app is a PSGI app!
    68. # Dancer use Dancer; get ‘/’ => sub { “Hello World”; }; dance; # returns a PSGI app!
    69. # Mojolicious::Lite use Mojolicious::Lite; get ‘/:name’ => sub { my $self = shift; $self->render_text(‘Hello!’); }; app->start; # returns PSGI app
    70. # Web::Simple use Web::Simple ‘MyApp’; package MyApp; dispatch { sub(GET) { [ 200, [...], [ ‘Hello’ ] ]; } }; my $app = MyApp->as_psgi; # $app is a PSGI app!
    71. perldoc PSGI If you want to adapt your framework to PSGI.
    72. Plack “PSGI toolkit”
    73. HTTP::Server::PSGI Reference PSGI web server bundled in Plack
    74. Plackup Runs PSGI app instantly from CLI (inspired by rackup)
    75. > plackup app.psgi
    76. Plack::Handler Connects PSGI apps to Web servers CGI, FastCGI, Apache, SCGI
    77. PSGI Web Servers
    78. Starman UNIX Preforking HTTP servers (like Unicorn.rb) HTTP/1.1 chunk + keep-alives / Very Fast
    79. Starlet Simpler UNIX HTTP/1.0 Server Best used with Server::Starter and nginx/lighttpd
    80. Twiggy Non-blocking web server (like Thin.rb) based on AnyEvent framework
    81. Corona Coroutine for each connection based on Coro.pm
    82. HTTP::Server::Simple::PSGI Zero-deps other than HTTP::Server::Simple Best for embedding PSGI applications
    83. uWSGI http://projects.unbit.it/uwsgi/
    84. Perlbal plugin http://github.com/miyagawa/Perlbal-Plugin-PSGI
    85. nginx embedded perl http://github.com/yappo/nginx-psgi-patchs
    86. mod_psgi http://github.com/spiritloose/mod_psgi
    87. evpsgi http://github.com/sekimura/evpsgi
    88. Feersum http://github.com/stash/Feersum
    89. 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
    90. Middleware
    91. PSGI Middleware Wraps a PSGI application to add pre/post processing
    92. 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; };
    93. PSGI Middleware coderef -> coderef Higher-order functions
    94. Middleware Debug, Session, Logger, Runtime, Static, Lint, AccessLog, ConditionalGET, ErrorDocument, StackTrace, Auth::Basic, Auth::Digest, ReverseProxy, Refresh, Auth::OAuth, Hippie, Throttle
    95. Plack::Middleware reusable and extensible Middleware framework Plack::Builder DSL in .psgi
    96. my $app = sub { return [ $status, $header, $body ]; }; use Plack::Builder; builder { enable “Static”, root => “/htdocs”, path => qr!^/static/!; enable “Deflater”; # gzip/deflate $app; }
    97. plackup compatible plackup -e ‘enable “Foo”;’ app.psgi
    98. Middleware Write once, run in every framework
    99. DEMO
    100. Plack::App::URLMap Multiplex multiple apps Integrated with Builder DSL
    101. 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; }; }
    102. CGI::PSGI Easy migration from CGI.pm
    103. CGI::Emulate::PSGI CGI::Compile Easiest migration from CGI scripts (like Registry)
    104. Plack::Request like libapreq (Apache::Request) wrapper APIs for middleware developers
    105. Plack::Test Unified interface to write tests with Mock HTTP and Live HTTP
    106. 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; };
    107. 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; };
    108. Plack::App::* ready-to-use applications
    109. Plack::App::Directory Static content file server
    110. Plack::App::Proxy (non-blocking) proxy server Can be used as reverse proxy as well
    111. Plack::App::CGIBin mount /cgi-bin as PSGI applications
    112. Plack::App::FCGIDispatcher Connect to FCGI daemon (even in Ruby, Python, C)
    113. Plack::App::JSP Runs JavaScript PSGI apps :)
    114. Testimonials a.k.a. self-promotion
    115. “I love this! It’s exactly the right answer to what i was looking for.” - Benjamin Trott
    116. “Wow, this is nothing short of awesome.” - Stevan Little
    117. “Plack is so simple, easy and powerful.” - Jesse Vincent
    118. “Plack rocks. Miyagawa rocks.” - Piers Cawley
    119. “Is he on too many drugs or too few? Either way let’s make sure that never changes.” - Matt S.Trout
    120. Real World Plack/PSGI
    121. You should join!
    122. > cpanm Plack > cpanm Task::Plack
    123. Cloud? (Heroku, GAE)
    124. Sunaba http://sunaba.plackperl.org/
    125. Runs on dankogai’s Sandbox
    126. You can even try: system(“rm -fr /”); while (1) { }
    127. 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!
    128. Remember: Plack is < 1y old
    129. Lots of (exciting) things going on.
    130. http://blog.plackperl.org/
    131. http://github.com/miyagawa/Plack http://plackperl.org/ irc://irc.perl.org/#plack
    132. Questions?
    133. Thank you! Slides: http://slideshare.net/miyagawa

    ×