SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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
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
15.
#!/usr/bin/perl
use strict;
print “Content-Type: text/plainrnrn”;
print “Hello World”;
16.
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;
17.
use FCGI;
my $req = FCGI::Request();
while ($req->Accept >= 0) {
print “Content-Type: text/plainrnrn”;
print “Hello World”;
}
18.
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;
75.
Starman
UNIX Preforking HTTP servers (like Unicorn.rb)
HTTP/1.1 chunk + keep-alives / Very Fast
76.
HTTP::Server::Simple::PSGI
Zero-deps other than HTTP::Server::Simple
Best for embedding PSGI applications
77.
Twiggy
Non-blocking web server (like Thin.rb)
based on AnyEvent framework
Starlet
Simpler UNIX HTTP/1.0 Server
Best used with Server::Starter and nginx/lighttpd
81.
Corona uWSGI
Coroutine for each connection http://projects.unbit.it/uwsgi/
based on Coro.pm
Feersum evpsgi
http://github.com/stash/Feersum http://github.com/sekimura/evpsgi
Gepok
http://metacpan/module/Gepok
Pure Perl standalone HTTPS
First released July 2011
82.
Frameworks Apps Your own code
Plack::Middleware
PSGI
Plack::Handler::* (CGI, FCGI, Apache)
132.
Plack::Middleware::A
Plack::Middleware::B
PSGI Compatible App
Request in Response out
P::MW::A P::MW::A
P::MW::B P::MW::B
PSGI App
133.
Plack::Middleware::A
Plack::Middleware::B
PSGI Compatible App
Request in e.g. Redirect Response out
P::MW::A P::MW::A
P::MW::B P::MW::B
PSGI App
134.
Plack::Middleware::A
Plack::Middleware::B
PSGI Compatible App
Request in e.g. Redirect Response out
P::MW::A P::MW::A
e.g. Static
P::MW::B P::MW::B
PSGI App
135.
Enabling
Plack::Middleware
reusable and extensible
Middleware framework
Plack::Builder DSL in .psgi
136.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”;
enable “B”;
$app;
}
137.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”;
enable “B”;
$app;
}
138.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”;
enable “B”;
$app;
}
139.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”;
enable “B”;
$app;
}
140.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”; # Plack::Middleware::A
enable “B”; # Order matters
$app;
}
141.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “A”;
enable “B”;
$app;
}
142.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “Static”, root => “/htdocs”,
path => qr!^/static/!;
enable “Deflater”;
$app;
}
143.
my $app = sub {
return [ $status, $header, $body ];
};
use Plack::Builder;
return builder {
enable “Static”, root => “/htdocs”,
path => qr!^/static/!;
enable “Deflater”;
$app;
}
184.
# 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)
);
});
185.
# 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)
);
});
186.
Plack::App::URLMap
Multiplex multiple apps
Integrated with Builder DSL
187.
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;
};
}
188.
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;
};
}
189.
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;
};
}
216.
# Hijack Any LWP::Useragent requests
LWP::Protocol::PSGI->register($app);
use LWP::Simple;
my $content = get("http://london.pm.org/");
say 'o/' if $content =~ /London Perl Mongers/;
217.
# Hijack Any LWP::Useragent requests
LWP::Protocol::PSGI->register($app);
use LWP::Simple;
my $content = get("http://london.pm.org/");
say 'o/' if $content =~ /London Perl Mongers/;
218.
# Hijack Any LWP::Useragent requests
LWP::Protocol::PSGI->register($app);
use LWP::Simple;
my $content = get("http://london.pm.org/");
say 'o/' if $content =~ /London Perl Mongers/;
219.
# Hijack Any LWP::Useragent requests
LWP::Protocol::PSGI->register($app);
use LWP::Simple;
my $content = get("http://london.pm.org/");
say 'o/' if $content =~ /London Perl Mongers/;
220.
Plack::Test
Unified interface to write TAP tests
with Mock HTTP and Live HTTP
221.
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’;
};
222.
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’;
};
223.
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’;
};
224.
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’;
};
225.
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’;
};
226.
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’;
};
227.
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’;
};
228.
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’;
};
281.
Summary
✦ PSGI is an interface, Plack is the code.
282.
Summary
✦ PSGI is an interface, Plack is the code.
✦ Many fast PSGI servers.
283.
Summary
✦ PSGI is an interface, Plack is the code.
✦ Many fast PSGI servers.
✦ Adapters and tools for frameworks and
webservers.
284.
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
285.
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