Plack basics for Perl websites - YAPC::EU 2011

323
Plack Basics Leo Lapworth @ YAPC::EU 2011 Much content from Tatsuhiko Miyagawa’s YAPC::NA 2010 talk

description

Run a website with Perl? - you should learn how to use Plack. Most Perl web frameworks support it and it makes your life a lot easier and a lot more fun

Transcript of Plack basics for Perl websites - YAPC::EU 2011

Page 1: Plack basics for Perl websites - YAPC::EU 2011

Plack Basics

Leo Lapworth @ YAPC::EU 2011

Much content from Tatsuhiko Miyagawa’sYAPC::NA 2010 talk

Page 2: Plack basics for Perl websites - YAPC::EU 2011

What is Plack?

Page 3: Plack basics for Perl websites - YAPC::EU 2011

What is Plack?

“Superglue for Perl 5 Web Frameworks and Web

Servers”

Page 4: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?

Page 5: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

Page 6: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

Page 7: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

Page 8: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

‣ Development

Page 9: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

‣ Development

‣ Testing

Page 10: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

‣ Development

‣ Testing

‣ Deployment

Page 11: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

‣ Development

‣ Testing

‣ Deployment

‣ World peace

Page 12: Plack basics for Perl websites - YAPC::EU 2011

How will that help me?‣ Flexibility

‣ Middleware (plugins)

‣ Apps

‣ Development

‣ Testing

‣ Deployment

‣ World peace

Page 13: Plack basics for Perl websites - YAPC::EU 2011

History...

Page 14: Plack basics for Perl websites - YAPC::EU 2011

Hello World

Page 15: Plack basics for Perl websites - YAPC::EU 2011

#!/usr/bin/perluse strict;

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

Page 16: Plack basics for Perl websites - YAPC::EU 2011

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 17: Plack basics for Perl websites - YAPC::EU 2011

use FCGI;

my $req = FCGI::Request();

while ($req->Accept >= 0) {

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

}

Page 18: Plack basics for Perl websites - YAPC::EU 2011

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 19: Plack basics for Perl websites - YAPC::EU 2011

All similar but slightly different

Page 20: Plack basics for Perl websites - YAPC::EU 2011

Painful to supportall of them

Page 21: Plack basics for Perl websites - YAPC::EU 2011

There wasone common way to do all of this.

Page 22: Plack basics for Perl websites - YAPC::EU 2011

CGI.pm

Page 23: Plack basics for Perl websites - YAPC::EU 2011

#!/usr/bin/perluse CGI;

my $q = CGI->new;

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

Page 24: Plack basics for Perl websites - YAPC::EU 2011

Works under...CGI

FastCGImod_perl

HTTP::Server::Simple::CGI

Page 25: Plack basics for Perl websites - YAPC::EU 2011

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 26: Plack basics for Perl websites - YAPC::EU 2011

CGI.pm?

Page 27: Plack basics for Perl websites - YAPC::EU 2011

CGI.pm?meh

Page 28: Plack basics for Perl websites - YAPC::EU 2011

Frameworks to the rescue!

Page 29: Plack basics for Perl websites - YAPC::EU 2011
Page 30: Plack basics for Perl websites - YAPC::EU 2011

Catalyst Maypole Mason Mojo Sledge 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 31: Plack basics for Perl websites - YAPC::EU 2011

Let’s look how they handled web servers.

Page 32: Plack basics for Perl websites - YAPC::EU 2011

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 33: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

Page 34: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

Page 35: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Page 36: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty

Page 37: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Page 38: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

Page 39: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

nginx

Page 40: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

Jifty Catalyst

Catalyst::Engine

nginxHTTP::Server::Simple

Page 41: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Catalyst::Engine

nginxHTTP::Server::Simple

Page 42: Plack basics for Perl websites - YAPC::EU 2011

CGI::Application

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Mason::CGIHandlerCatalyst::Engine

nginxHTTP::Server::Simple

Page 43: Plack basics for Perl websites - YAPC::EU 2011

Gross.

Page 44: Plack basics for Perl websites - YAPC::EU 2011

CGI.pmJifty, CGI::Application, Spoon

mod_perl centricMason, Sledge, PageKit, WebGUI

AdaptersCatalyst, Maypole, Squatting

Page 45: Plack basics for Perl websites - YAPC::EU 2011

That was 2008...

Page 46: Plack basics for Perl websites - YAPC::EU 2011

Gentleman thief &Double agent

Miyagawa

Page 47: Plack basics for Perl websites - YAPC::EU 2011

Acquired a great ideafrom Python/Ruby

Page 48: Plack basics for Perl websites - YAPC::EU 2011

WSGI (Python)Rack (Ruby)

Page 49: Plack basics for Perl websites - YAPC::EU 2011

WSGI (PEP-333)

Page 50: Plack basics for Perl websites - YAPC::EU 2011

WSGI Python Frameworks• Django

• Bottle

• CherryPy

• Tornado

• Pylons

• Flask

• mod_wsgi

• Paste

• gunicorn

• uWSGI

• wsgiref

• Google AppEngine

Page 51: Plack basics for Perl websites - YAPC::EU 2011

WSGI

WSGI middleware

Django Bottle Flask Tornado

Apache lighttpd nginx mod_wsgi

wsgi handlers

GAE

Page 52: Plack basics for Perl websites - YAPC::EU 2011

Rack

Page 53: Plack basics for Perl websites - YAPC::EU 2011

Rack Ruby Frameworks• Rails

• Merb

• Sinatra

• Camping

• Ramaze

• etc.

• Unicorn

• Thin

• Mongrel

• Rainbows!

• Phusion Passenger

• Heroku

Page 54: Plack basics for Perl websites - YAPC::EU 2011

Rack

Rack middleware

Rails Merb Sinatra Ramaze

Apache lighttpd Thin Unicorn

Rack handlers

Mongrel

Page 55: Plack basics for Perl websites - YAPC::EU 2011

Perl ?

Page 56: Plack basics for Perl websites - YAPC::EU 2011

Perl ?PSGIPerl Web Server Gateway Interface

Page 57: Plack basics for Perl websites - YAPC::EU 2011

Interface

Page 58: Plack basics for Perl websites - YAPC::EU 2011

Interface

Page 59: Plack basics for Perl websites - YAPC::EU 2011

InterfacePSGI != Plack

Page 60: Plack basics for Perl websites - YAPC::EU 2011

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

Page 61: Plack basics for Perl websites - YAPC::EU 2011

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

Page 62: Plack basics for Perl websites - YAPC::EU 2011

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

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

Page 63: Plack basics for Perl websites - YAPC::EU 2011

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

HTTP status code (int.): 200, 404 etc.

Page 64: Plack basics for Perl websites - YAPC::EU 2011

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

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

Page 65: Plack basics for Perl websites - YAPC::EU 2011

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

String, array reference of content chunks,Filehandle or IO::Handle-ish object

Page 66: Plack basics for Perl websites - YAPC::EU 2011

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

Page 67: Plack basics for Perl websites - YAPC::EU 2011

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

Page 68: Plack basics for Perl websites - YAPC::EU 2011

Now you’ve gota PSGI compatible

application.

Page 69: Plack basics for Perl websites - YAPC::EU 2011

Apache IIS lighttpd

CGI.pm

CGI fastcgimod_perl

JiftyMason Catalyst

Mason::CGIHandlerCatalyst::Engine

nginxHTTP::Server::Simple

CGI::App

Page 70: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Catalyst CGI::App Jifty Mason 2

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Page 71: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Catalyst CGI::App Jifty Mason 2

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

other PSGI Webservers

Page 72: Plack basics for Perl websites - YAPC::EU 2011

Web Servers

Page 73: Plack basics for Perl websites - YAPC::EU 2011

Plack::HandlerConnects PSGI compatible apps

to Web servers...

Page 76: Plack basics for Perl websites - YAPC::EU 2011

StarmanUNIX Preforking HTTP servers (like Unicorn.rb)

HTTP/1.1 chunk + keep-alives / Very Fast

Page 77: Plack basics for Perl websites - YAPC::EU 2011

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

Best for embedding PSGI applications

Page 78: Plack basics for Perl websites - YAPC::EU 2011

StarletSimpler UNIX HTTP/1.0 Server

Best used with Server::Starter and nginx/lighttpd

TwiggyNon-blocking web server (like Thin.rb)

based on AnyEvent framework

Page 79: Plack basics for Perl websites - YAPC::EU 2011

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

Page 80: Plack basics for Perl websites - YAPC::EU 2011

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

Page 82: Plack basics for Perl websites - YAPC::EU 2011

Gepokhttp://metacpan/module/Gepok

Pure Perl standalone HTTPSFirst released July 2011

evpsgihttp://github.com/sekimura/evpsgi

Feersumhttp://github.com/stash/Feersum

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

CoronaCoroutine for each connection

based on Coro.pm

Page 83: Plack basics for Perl websites - YAPC::EU 2011

PSGI

Plack::Middleware

Frameworks Apps Your own code

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

Page 84: Plack basics for Perl websites - YAPC::EU 2011

PSGI

Plack::Middleware

Frameworks Apps Your own code

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Starman Twiggy uWSGI Corona etc, etc

Page 85: Plack basics for Perl websites - YAPC::EU 2011

25+ Plack::Handlers

Page 86: Plack basics for Perl websites - YAPC::EU 2011

Adoption?

Page 87: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Starman Twiggy uWSGI Corona etc

Page 88: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

?

Starman Twiggy uWSGI Corona etc

Page 89: Plack basics for Perl websites - YAPC::EU 2011

Catalyst Maypole Mason Mojo Sledge 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 90: Plack basics for Perl websites - YAPC::EU 2011

Catalyst Maypole Mason Mojo Sledge 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 91: Plack basics for Perl websites - YAPC::EU 2011

• Amon

• Angelos

• Ark

• Catalyst

• CGI::Application

• Continuity

• Dancer

• Hydrant

• Jifty

• Mason

• Maypole

• Mojo

• Mojolicious

• Noe

• Schenker

• Sledge

• Squatting

• Tatsumaki

• Titanium

• Web::Simple

PSGI Perl Frameworks

Page 92: Plack basics for Perl websites - YAPC::EU 2011

ApplicationsMovable Type 6

WebGUI 8RT4

ACT (conference toolkit)Bricolage (if someone gets time)

Page 93: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Starman Twiggy uWSGI Corona evpsgi

Page 94: Plack basics for Perl websites - YAPC::EU 2011

PSGI Compatible App

Plack::Middleware

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

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

Starman Twiggy uWSGI Corona evpsgi

Catalyst CGI::App Jifty Tatsumaki

Dancer Mojoliscious MT6 Mason 2

Web::Simple WebGui 8 Mojo etc...

Page 95: Plack basics for Perl websites - YAPC::EU 2011

PSGI from a framework

Page 96: Plack basics for Perl websites - YAPC::EU 2011

use Dancer;

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

dance;

Page 97: Plack basics for Perl websites - YAPC::EU 2011

use Mojolicious::Lite;

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

app->start;

Page 98: Plack basics for Perl websites - YAPC::EU 2011

use My::Jifty::App;

my $app = My::Jifty::App->psgi_app;

Page 99: Plack basics for Perl websites - YAPC::EU 2011

use Web::Simple ‘MyApp’;

package MyApp;

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

my $app = MyApp->as_psgi;

Page 100: Plack basics for Perl websites - YAPC::EU 2011

use My::Catalyst::App;

My::Catalyst::App->setup_engine(‘PSGI’);

my $app = sub {

My::Catalyst::App->run(@_)

};

Page 101: Plack basics for Perl websites - YAPC::EU 2011
Page 102: Plack basics for Perl websites - YAPC::EU 2011
Page 103: Plack basics for Perl websites - YAPC::EU 2011
Page 104: Plack basics for Perl websites - YAPC::EU 2011

catalyst.pl My::Catalyst::App

Page 105: Plack basics for Perl websites - YAPC::EU 2011

Plack“PSGI implementation & toolkit”

Page 106: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

Page 107: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkitPlack::Handlers Connect PSGI apps and Web Servers

Page 108: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Handlers Connect PSGI apps and Web Servers

Page 109: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Loader (auto)load Plack Servers

Plack::Handlers Connect PSGI apps and Web Servers

Page 110: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Loader (auto)load Plack Servers

Plack::Handlers Connect PSGI apps and Web Servers

Plack::Middleware Easy-to-use PSGI Middleware

Page 111: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Loader (auto)load Plack Servers

Plack::Handlers Connect PSGI apps and Web Servers

Plack::Middleware Easy-to-use PSGI Middleware

Plack::Builder OO & DSL to enable Middleware

Page 112: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Loader (auto)load Plack Servers

Plack::Handlers Connect PSGI apps and Web Servers

Plack::Middleware Easy-to-use PSGI Middleware

Plack::Apps Apps

Plack::Builder OO & DSL to enable Middleware

Page 113: Plack basics for Perl websites - YAPC::EU 2011

Plack toolkit

plackup Command line launcher

Plack::Loader (auto)load Plack Servers

Plack::Handlers Connect PSGI apps and Web Servers

Plack::Middleware Easy-to-use PSGI Middleware

Plack::Apps Apps

Plack::Test Testing

Plack::Builder OO & DSL to enable Middleware

Page 114: Plack basics for Perl websites - YAPC::EU 2011

plackupRuns PSGI app instantly from CLI

(inspired by rackup)

Page 115: Plack basics for Perl websites - YAPC::EU 2011

> plackup app.psgi

Page 116: Plack basics for Perl websites - YAPC::EU 2011

> plackup app.psgiHTTP::Server::PSGI: Accepting connections at http://0:5000/

Page 117: Plack basics for Perl websites - YAPC::EU 2011

HTTP::Server::PSGIReference PSGI web server

bundled in Plack

Standalone, single-process HTTP servergreat for development and testing

Page 118: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware(160+ modules - July 2011)

Page 119: Plack basics for Perl websites - YAPC::EU 2011

MiddlewareDebug, Session, Logger, Static, Lint,

AccessLog, ErrorDocument, StackTrace, Auth::Basic, Auth::Digest, ReverseProxy, Refresh, Auth::OAuth,

Throttle....

Page 120: Plack basics for Perl websites - YAPC::EU 2011
Page 121: Plack basics for Perl websites - YAPC::EU 2011

Access AccessLog AccessLog::Timed Acme::YadaYada AddDefaultCharset AllowCrossSiteAJAX AMF Assets Auth::Digest Auth::Form Auth::Htpasswd Auth::Negotiate Auth::Basic AutoRefresh BufferedStreaming Cache Cached ChromeFrame Chunked Class::Refresh Compile Conditional ConditionalGET ConsoleLogger ContentLength ContentMD5 CrossOrigin CSRFBlock Dancer::Debug DBIC::QueryLog Debug Debug::CatalystPluginCache Debug::DBIC::QueryLog Debug::DBIProfile Debug::Profiler::NYTProf Debug::W3CValidate Deflater DoCoMoGUID Doorman ErrorDocument ESI ETag Expires File::Sass Firebug::Lite FirePHP ForceEnv Head Header HTMLify HTMLMinify HTTPExceptions IEnosniff IIS6ScriptNameFix Image::Scale Inline InteractiveDebugger IPAddressFilter iPhone JavaScript::Ectype JSConcat JSONP LighttpdScriptNameFix Lint Log::Contextual Log::Minimal Log4perl LogDispatch LogWarn MethodOverride Mirror NeverExpire NoDeflate NoMultipleSlashes NullLogger Options OptionsOK Precompressed ProxyMap RearrangeHeaders Recursive RefererCheck Refresh REPL Reproxy ReverseProxy Rewrite Runtime Scope::Container Scope::Session ServerStatus::Lite Session Session::SerializedCookie SetAccept SimpleContentFilter SimpleLogger SizeLimit SocketIO SSI StackTrace Static Static::Minifier StaticShared Status Test::StashWarnings Throttle TMT UseChromeFrame Watermark

Page 122: Plack basics for Perl websites - YAPC::EU 2011

Plack MiddlewareWraps a PSGI applicationto add pre/post processing

Page 123: Plack basics for Perl websites - YAPC::EU 2011
Page 124: Plack basics for Perl websites - YAPC::EU 2011

Logging

Page 125: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirect

Page 126: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirectError Handler

Page 127: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirectError HandlerCache Middleware

Page 128: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirectError HandlerCache MiddlewareSession Middleware

Page 129: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirectError HandlerCache MiddlewareSession MiddlewareRoutes Middleware

Page 130: Plack basics for Perl websites - YAPC::EU 2011

LoggingStatus code redirectError HandlerCache MiddlewareSession MiddlewareRoutes MiddlewareYour App

Page 131: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware::A

Plack::Middleware::B

PSGI Compatible App

Page 132: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 133: Plack basics for Perl websites - YAPC::EU 2011

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 134: Plack basics for Perl websites - YAPC::EU 2011

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 135: Plack basics for Perl websites - YAPC::EU 2011

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 136: Plack basics for Perl websites - YAPC::EU 2011

P::MW::B

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 137: Plack basics for Perl websites - YAPC::EU 2011

P::MW::A

P::MW::B

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 138: Plack basics for Perl websites - YAPC::EU 2011

Response out

P::MW::A

P::MW::B

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 139: Plack basics for Perl websites - YAPC::EU 2011

e.g. Redirect Response out

P::MW::A

P::MW::B

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 140: Plack basics for Perl websites - YAPC::EU 2011

e.g. Redirect

e.g. Static

Response out

P::MW::A

P::MW::B

PSGI App

P::MW::B

P::MW::A

Plack::Middleware::A

Request in

Plack::Middleware::B

PSGI Compatible App

Page 141: Plack basics for Perl websites - YAPC::EU 2011

Enabling Plack::Middleware

reusable and extensible Middleware framework

Plack::Builder DSL in .psgi

Page 142: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; enable “B”; $app;}

Page 143: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; enable “B”; $app;}

Page 144: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; enable “B”; $app;}

Page 145: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; enable “B”; $app;}

Page 146: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; # Plack::Middleware::A enable “B”; # Order matters $app;}

Page 147: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

return builder { enable “A”; enable “B”; $app;}

Page 148: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

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

Page 149: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

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

Page 150: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

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

Page 151: Plack basics for Perl websites - YAPC::EU 2011

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

use Plack::Builder;

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

Page 152: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware::Static

Plack::Middleware::Deflate

PSGI Compatible App

Page 153: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 154: Plack basics for Perl websites - YAPC::EU 2011

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 155: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 156: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

-

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 157: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

PSGI App

-

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 158: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

Deflate compresses

PSGI App

-

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 159: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

-

Deflate compresses

PSGI App

-

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 160: Plack basics for Perl websites - YAPC::EU 2011

=~ ^/static Response out

-

Deflate compresses

PSGI App

-

Static

Plack::Middleware::Static

Request in

Plack::Middleware::Deflate

PSGI Compatible App

Page 161: Plack basics for Perl websites - YAPC::EU 2011

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

Page 162: Plack basics for Perl websites - YAPC::EU 2011

MiddlewareWrite once, run in every framework

Page 163: Plack basics for Perl websites - YAPC::EU 2011

A few demos..

Page 164: Plack basics for Perl websites - YAPC::EU 2011

Assume...

use Plack::Builder;

my $body = ‘<html><body>Hello World</body></html>’;

my $app = sub { my $self = shift;

return [200, ['Content-Type' => 'text/html'], [ $body ]];};

Page 165: Plack basics for Perl websites - YAPC::EU 2011

Assume...

use Plack::Builder;

my $body = ‘<html><body>Hello World</body></html>’;

my $app = sub { my $self = shift;

return [200, ['Content-Type' => 'text/html'], [ $body ]];};

Page 166: Plack basics for Perl websites - YAPC::EU 2011

Assume...

use Plack::Builder;

my $body = ‘<html><body>Hello World</body></html>’;

my $app = sub { my $self = shift;

return [200, ['Content-Type' => 'text/html'], [ $body ]];};

Page 167: Plack basics for Perl websites - YAPC::EU 2011

Debugger

Page 168: Plack basics for Perl websites - YAPC::EU 2011

return builder { # Precious debug info. Right on your page! enable 'Debug';

$app;}

Page 169: Plack basics for Perl websites - YAPC::EU 2011
Page 170: Plack basics for Perl websites - YAPC::EU 2011
Page 171: Plack basics for Perl websites - YAPC::EU 2011
Page 172: Plack basics for Perl websites - YAPC::EU 2011
Page 173: Plack basics for Perl websites - YAPC::EU 2011
Page 174: Plack basics for Perl websites - YAPC::EU 2011
Page 175: Plack basics for Perl websites - YAPC::EU 2011
Page 176: Plack basics for Perl websites - YAPC::EU 2011
Page 177: Plack basics for Perl websites - YAPC::EU 2011

InteractiveDebugger

Page 178: Plack basics for Perl websites - YAPC::EU 2011

my $app = sub { my $foo = 'bar'; die "oops" if $foo eq 'bar';

[200, ['Content-Type' => 'text/html'], [ $body ]];};

return builder { # Enable Interactive debugging enable "InteractiveDebugger";

$app;}

Page 179: Plack basics for Perl websites - YAPC::EU 2011

my $app = sub { my $foo = 'bar'; die "oops" if $foo eq 'bar';

[200, ['Content-Type' => 'text/html'], [ $body ]];};

return builder { # Enable Interactive debugging enable "InteractiveDebugger";

$app;}

Page 180: Plack basics for Perl websites - YAPC::EU 2011

my $app = sub { my $foo = 'bar'; die "oops" if $foo eq 'bar';

[200, ['Content-Type' => 'text/html'], [ $body ]];};

return builder { # Enable Interactive debugging enable "InteractiveDebugger";

$app;}

Page 181: Plack basics for Perl websites - YAPC::EU 2011
Page 182: Plack basics for Perl websites - YAPC::EU 2011
Page 183: Plack basics for Perl websites - YAPC::EU 2011
Page 184: Plack basics for Perl websites - YAPC::EU 2011
Page 185: Plack basics for Perl websites - YAPC::EU 2011
Page 186: Plack basics for Perl websites - YAPC::EU 2011
Page 187: Plack basics for Perl websites - YAPC::EU 2011

NYTProf - profiler

Page 188: Plack basics for Perl websites - YAPC::EU 2011

First rule of Program Optimisation

Page 189: Plack basics for Perl websites - YAPC::EU 2011

Don’t do it!

Page 190: Plack basics for Perl websites - YAPC::EU 2011

Second rule of Program Optimisation

(for experts only)

Page 191: Plack basics for Perl websites - YAPC::EU 2011

Don’t do it - yet!

Page 192: Plack basics for Perl websites - YAPC::EU 2011

return builder { enable 'Debug', panels => [ [ 'Profiler::NYTProf' ] ]; $app;};

Page 193: Plack basics for Perl websites - YAPC::EU 2011
Page 194: Plack basics for Perl websites - YAPC::EU 2011
Page 195: Plack basics for Perl websites - YAPC::EU 2011
Page 196: Plack basics for Perl websites - YAPC::EU 2011
Page 197: Plack basics for Perl websites - YAPC::EU 2011
Page 198: Plack basics for Perl websites - YAPC::EU 2011

ServerStatus::Lite

Page 199: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Builder;

return builder { enable "ServerStatus::Lite", path => '/server-status', allow => [ '127.0.0.1'], scoreboard => '/tmp/score'; $app;}

Page 200: Plack basics for Perl websites - YAPC::EU 2011
Page 201: Plack basics for Perl websites - YAPC::EU 2011
Page 202: Plack basics for Perl websites - YAPC::EU 2011
Page 203: Plack basics for Perl websites - YAPC::EU 2011
Page 204: Plack basics for Perl websites - YAPC::EU 2011

SizeLimit

Page 205: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Builder;

return builder { enable "SizeLimit", max_unshared_size_in_kb => 3000, check_every_n_requests => 5; $app;}

Page 206: Plack basics for Perl websites - YAPC::EU 2011

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

Page 207: Plack basics for Perl websites - YAPC::EU 2011
Page 208: Plack basics for Perl websites - YAPC::EU 2011

Apache::ActionWrapper CGIBin Cascade CocProxy DAV Directory Directory::Xslate FCGIDispatcher File ImageMagick JSP PSGIBin Path::Router Proxy Proxy::Backend::AnyEvent::HTTP Proxy::Backend::LWP Proxy::Selective Proxy::Test URLMap WrapApacheReq WrapApacheReq::FakeRequest WrapCGI

Page 209: Plack basics for Perl websites - YAPC::EU 2011

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

Page 210: Plack basics for Perl websites - YAPC::EU 2011

CGI::PSGIEasy migration from CGI.pm

Page 211: Plack basics for Perl websites - YAPC::EU 2011

Plack::App::DirectoryStatic content file server

Page 212: Plack basics for Perl websites - YAPC::EU 2011

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

Can be used as reverse proxy as well

Page 213: Plack basics for Perl websites - YAPC::EU 2011

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

Page 214: Plack basics for Perl websites - YAPC::EU 2011

# app.psgi - Javascript! Plack::App::JSP->new( js => q{ function respond(body) { return [ 200, [ 'Content-type', 'text/html' ], [ body ] ] } respond("Five factorial is " + (function(x) { if ( x<2 ) return x; return x * arguments.callee(x - 1); })(5) ); });

Page 215: Plack basics for Perl websites - YAPC::EU 2011

# app.psgi - Javascript! Plack::App::JSP->new( js => q{ function respond(body) { return [ 200, [ 'Content-type', 'text/html' ], [ body ] ] } respond("Five factorial is " + (function(x) { if ( x<2 ) return x; return x * arguments.callee(x - 1); })(5) ); });

Page 216: Plack basics for Perl websites - YAPC::EU 2011

Plack::App::URLMapMultiplex multiple apps

Integrated with Builder DSL

Page 217: Plack basics for Perl websites - YAPC::EU 2011

use CatApp;use CGIApp;

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

use Plack::Builder;

return builder { mount “/cat” => $c1; mount “/cgi-app” => builder { enable “StackTrace”; $c2; };}

Page 218: Plack basics for Perl websites - YAPC::EU 2011

use CatApp;use CGIApp;

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

use Plack::Builder;

return builder { mount “/cat” => $c1; mount “/cgi-app” => builder { enable “StackTrace”; $c2; };}

Page 219: Plack basics for Perl websites - YAPC::EU 2011

use CatApp;use CGIApp;

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

use Plack::Builder;

return builder { mount “/cat” => $c1; mount “/cgi-app” => builder { enable “StackTrace”; $c2; };}

Page 220: Plack basics for Perl websites - YAPC::EU 2011

Some more demos...

Page 221: Plack basics for Perl websites - YAPC::EU 2011

Basic website

Page 222: Plack basics for Perl websites - YAPC::EU 2011

TemplateToolkit + Static

Page 223: Plack basics for Perl websites - YAPC::EU 2011

my $root = '/path/to/html_doc_root';

my $app = Plack::Middleware::TemplateToolkit->new( INCLUDE_PATH => root,)->to_app;

return builder {

enable 'Static', path =>

qr{\.[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$}, root => $root;

$app; }

Page 224: Plack basics for Perl websites - YAPC::EU 2011

my $root = '/path/to/html_doc_root';

my $app = Plack::Middleware::TemplateToolkit->new( INCLUDE_PATH => root,)->to_app;

return builder {

enable 'Static', path =>

qr{\.[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$}, root => $root;

$app; }

Page 225: Plack basics for Perl websites - YAPC::EU 2011

my $root = '/path/to/html_doc_root';

my $app = Plack::Middleware::TemplateToolkit->new( INCLUDE_PATH => root,)->to_app;

return builder {

enable 'Static', path =>

qr{\.[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$}, root => $root;

$app; }

Page 226: Plack basics for Perl websites - YAPC::EU 2011

my $root = '/path/to/html_doc_root';

my $app = Plack::Middleware::TemplateToolkit->new( INCLUDE_PATH => root,)->to_app;

return builder {

enable 'Static', path =>

qr{\.[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$}, root => $root;

$app; }

Page 227: Plack basics for Perl websites - YAPC::EU 2011

my $root = '/path/to/html_doc_root';

my $app = Plack::Middleware::TemplateToolkit->new( INCLUDE_PATH => root,)->to_app;

return builder {

enable 'Static', path =>

qr{\.[gif|png|jpg|swf|ico|mov|mp3|pdf|js|css]$}, root => $root;

$app; }

Page 228: Plack basics for Perl websites - YAPC::EU 2011

Creating utilities

Page 229: Plack basics for Perl websites - YAPC::EU 2011

Caching Proxy

Page 230: Plack basics for Perl websites - YAPC::EU 2011

Website

Page 231: Plack basics for Perl websites - YAPC::EU 2011

Website

Developing code

Page 232: Plack basics for Perl websites - YAPC::EU 2011

Website

Developing code

Page 233: Plack basics for Perl websites - YAPC::EU 2011

Website

Developing code

Slow website

Page 234: Plack basics for Perl websites - YAPC::EU 2011

Slow website

Developing code

Page 235: Plack basics for Perl websites - YAPC::EU 2011

Slow website

Developing code

Caching Proxy

Page 236: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Middleware::Cache;use Plack::App::Proxy;

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

return builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache'; $app;};

Page 237: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Middleware::Cache;use Plack::App::Proxy;

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

return builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache'; $app;};

Page 238: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Middleware::Cache;use Plack::App::Proxy;

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

return builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache'; $app;};

Page 239: Plack basics for Perl websites - YAPC::EU 2011
Page 240: Plack basics for Perl websites - YAPC::EU 2011
Page 241: Plack basics for Perl websites - YAPC::EU 2011
Page 242: Plack basics for Perl websites - YAPC::EU 2011

use LWP::Simple;

my $content = get(‘http://localhost:5000/’);

Page 243: Plack basics for Perl websites - YAPC::EU 2011

Caching Proxy + Domain hijack

Page 244: Plack basics for Perl websites - YAPC::EU 2011

use LWP::Simple;

my $content = get(‘http://localhost:5000/’);

Page 245: Plack basics for Perl websites - YAPC::EU 2011

use LWP::Simple;

my $content = get(‘http://london.pm.org/’);

Page 246: Plack basics for Perl websites - YAPC::EU 2011

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

return builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache2'; $app;};

Page 247: Plack basics for Perl websites - YAPC::EU 2011

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

return builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache2'; $app;};

Page 248: Plack basics for Perl websites - YAPC::EU 2011

my $app = Plack::App::Proxy->new(

remote => "http://london.pm.org/" )->to_app;

$app = builder { enable "Cache", match_url => '^/.*', # everything cache_dir => '/tmp/plack-cache2'; $app;};

Page 249: Plack basics for Perl websites - YAPC::EU 2011

# Hijack Any LWP::Useragent requestsLWP::Protocol::PSGI->register($app);

use LWP::Simple;

my $content = get("http://london.pm.org/");

say '\o/' if $content =~ /London Perl Mongers/;

Page 250: Plack basics for Perl websites - YAPC::EU 2011

# Hijack Any LWP::Useragent requestsLWP::Protocol::PSGI->register($app);

use LWP::Simple;

my $content = get("http://london.pm.org/");

say '\o/' if $content =~ /London Perl Mongers/;

Page 251: Plack basics for Perl websites - YAPC::EU 2011

# Hijack Any LWP::Useragent requestsLWP::Protocol::PSGI->register($app);

use LWP::Simple;

my $content = get("http://london.pm.org/");

say '\o/' if $content =~ /London Perl Mongers/;

Page 252: Plack basics for Perl websites - YAPC::EU 2011

# Hijack Any LWP::Useragent requestsLWP::Protocol::PSGI->register($app);

use LWP::Simple;

my $content = get("http://london.pm.org/");

say '\o/' if $content =~ /London Perl Mongers/;

Page 253: Plack basics for Perl websites - YAPC::EU 2011
Page 254: Plack basics for Perl websites - YAPC::EU 2011
Page 255: Plack basics for Perl websites - YAPC::EU 2011
Page 256: Plack basics for Perl websites - YAPC::EU 2011

Plack::TestUnified interface to write TAP testswith Mock HTTP and Live HTTP

Page 257: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 258: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 259: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 260: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 261: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 262: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req); ok $res->[0] == ‘200’, ‘Success’; };

Page 263: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req);

ok $res->[0] == ‘200’, ‘Success’; };

Page 264: Plack basics for Perl websites - YAPC::EU 2011

use Plack::Test;use HTTP::Request::Common;

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

test_psgi app => $app,client => sub { my $callback = shift;

my $req = GET “http://localhost/foo”; my $res = $callback->($req);

ok $res->[0] == ‘200’, ‘Success’; };

Page 265: Plack basics for Perl websites - YAPC::EU 2011

Test::WWW::Mechanize::PSGI

Page 266: Plack basics for Perl websites - YAPC::EU 2011

use Test::WWW::Mechanize::PSGI; my $mech = Test::WWW::Mechanize::PSGI->new(     app => $app, );

$mech->get_ok('/');

Page 267: Plack basics for Perl websites - YAPC::EU 2011

use Test::WWW::Mechanize::PSGI; my $mech = Test::WWW::Mechanize::PSGI->new(     app => $app, );

$mech->get_ok('/');

Page 268: Plack basics for Perl websites - YAPC::EU 2011

Testing your full configuration!

Page 269: Plack basics for Perl websites - YAPC::EU 2011

Network setup tip

Page 270: Plack basics for Perl websites - YAPC::EU 2011

Use a reverse proxy

Page 271: Plack basics for Perl websites - YAPC::EU 2011

• Sits in front of servers, not clients - “reverse”

Use a reverse proxy

Page 272: Plack basics for Perl websites - YAPC::EU 2011

• Sits in front of servers, not clients - “reverse”

•Makes servers more efficient

Use a reverse proxy

Page 273: Plack basics for Perl websites - YAPC::EU 2011

• Sits in front of servers, not clients - “reverse”

•Makes servers more efficient

• Add HTTPS easily

Use a reverse proxy

Page 274: Plack basics for Perl websites - YAPC::EU 2011

• Sits in front of servers, not clients - “reverse”

•Makes servers more efficient

• Add HTTPS easily

•Makes scaling easier

Use a reverse proxy

Page 275: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Page 276: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Page 277: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Page 278: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Page 279: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Reverse ProxyNGINX Perlbal Pound

Page 280: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Reverse ProxyNGINX Perlbal Pound

Page 281: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Reverse ProxyNGINX Perlbal Pound

Page 282: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Reverse ProxyNGINX Perlbal Pound

Page 283: Plack basics for Perl websites - YAPC::EU 2011

Webserver App

Internet / Users

Reverse ProxyNGINX Perlbal Pound

Page 284: Plack basics for Perl websites - YAPC::EU 2011

Server

Reverse ProxyNGINX Perlbal Pound

Page 285: Plack basics for Perl websites - YAPC::EU 2011

Server 1

Reverse ProxyNGINX Perlbal Pound

Server 2

Page 286: Plack basics for Perl websites - YAPC::EU 2011

Server 1

Reverse ProxyNGINX Perlbal Pound

Server 2

Server 3

Page 287: Plack basics for Perl websites - YAPC::EU 2011

Server 1

Reverse ProxyNGINX Perlbal Pound

Server 2

Server 3

Page 288: Plack basics for Perl websites - YAPC::EU 2011

Plack::Middleware::ReverseProxy

Updates $env->{REMOTE_ADDRESS}

Page 289: Plack basics for Perl websites - YAPC::EU 2011

Why use Plack?

Page 290: Plack basics for Perl websites - YAPC::EU 2011

Why use Plack?

‣ Flexibility

‣ Middleware

‣ Apps

‣ Development

‣ Testing

‣ Deployment

Page 291: Plack basics for Perl websites - YAPC::EU 2011

Flexibility

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

Starman Twiggy uWSGI Corona etc

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

Page 292: Plack basics for Perl websites - YAPC::EU 2011

Flexibility• Easy to change webserver (25+!)

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

Starman Twiggy uWSGI Corona etc

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

Page 293: Plack basics for Perl websites - YAPC::EU 2011

Flexibility• Easy to change webserver (25+!)

Apache lighttpd HTTP::Server::PSGI Perlbalmod_psgi

Starman Twiggy uWSGI Corona etc

• Starman seems to be used most

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

Page 294: Plack basics for Perl websites - YAPC::EU 2011

Middleware

Page 295: Plack basics for Perl websites - YAPC::EU 2011

Middleware

• Easy to reuse with any PSGI app

Page 296: Plack basics for Perl websites - YAPC::EU 2011

Middleware

• Easy to reuse with any PSGI app

• Many tools to make your life easy

Page 297: Plack basics for Perl websites - YAPC::EU 2011

Middleware

• Easy to reuse with any PSGI app

• Many tools to make your life easy

• 160+ on CPAN now

Page 298: Plack basics for Perl websites - YAPC::EU 2011

App

Page 299: Plack basics for Perl websites - YAPC::EU 2011

App

• URLMapping / Routing

Page 300: Plack basics for Perl websites - YAPC::EU 2011

App

• URLMapping / Routing

• Static files

Page 301: Plack basics for Perl websites - YAPC::EU 2011

App

• URLMapping / Routing

• Static files

• Proxying

Page 302: Plack basics for Perl websites - YAPC::EU 2011

Development

Page 303: Plack basics for Perl websites - YAPC::EU 2011

Development

• plackup

Page 304: Plack basics for Perl websites - YAPC::EU 2011

Development

• plackup

• Restarter (monitor changes on disk)

Page 305: Plack basics for Perl websites - YAPC::EU 2011

Development

• plackup

• Restarter (monitor changes on disk)

• HTTP::Server::PSGI

Page 306: Plack basics for Perl websites - YAPC::EU 2011

Development

• plackup

• Restarter (monitor changes on disk)

• HTTP::Server::PSGI

• Debugging middleware

Page 307: Plack basics for Perl websites - YAPC::EU 2011

Development

• plackup

• Restarter (monitor changes on disk)

• HTTP::Server::PSGI

• Debugging middleware

• Profiler

Page 308: Plack basics for Perl websites - YAPC::EU 2011

Testing

Page 309: Plack basics for Perl websites - YAPC::EU 2011

Testing

• Testing your full configuration

Page 310: Plack basics for Perl websites - YAPC::EU 2011

Testing

• Testing your full configuration

• Test::WWW::Mechanize::PSGI

Page 311: Plack basics for Perl websites - YAPC::EU 2011

Testing

• Testing your full configuration

• Test::WWW::Mechanize::PSGI

• Plack::Test

Page 312: Plack basics for Perl websites - YAPC::EU 2011

Deployment

Page 313: Plack basics for Perl websites - YAPC::EU 2011

Deployment

• No separate configuration files

Page 314: Plack basics for Perl websites - YAPC::EU 2011

Deployment

• No separate configuration files

• Easy to choose/change webserver

Page 315: Plack basics for Perl websites - YAPC::EU 2011

Deployment

• No separate configuration files

• Easy to choose/change webserver

• DotCloud etc - cloud deployment

Page 316: Plack basics for Perl websites - YAPC::EU 2011

Summary

Page 317: Plack basics for Perl websites - YAPC::EU 2011

Summary

✦ PSGI is an interface, Plack is the code.

Page 318: Plack basics for Perl websites - YAPC::EU 2011

Summary

✦ PSGI is an interface, Plack is the code.

✦ Many fast PSGI servers.

Page 319: Plack basics for Perl websites - YAPC::EU 2011

Summary

✦ PSGI is an interface, Plack is the code.

✦ Many fast PSGI servers.

✦ Adapters and tools for frameworks and webservers.

Page 320: Plack basics for Perl websites - YAPC::EU 2011

Summary

✦ PSGI is an interface, Plack is the code.

✦ Many fast PSGI servers.

✦ Adapters and tools for frameworks and webservers.

✦ An amazing amount of middleware

Page 321: Plack basics for Perl websites - YAPC::EU 2011

Summary

✦ PSGI is an interface, Plack is the code.

✦ Many fast PSGI servers.

✦ Adapters and tools for frameworks and webservers.

✦ An amazing amount of middleware

✦ Used in many production systems

Page 322: Plack basics for Perl websites - YAPC::EU 2011

Use Plack

Page 323: Plack basics for Perl websites - YAPC::EU 2011

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

http://plackperl.org/

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