Kansai.pm 10周年記念 Plack/PSGI 入門

10,067 views

Published on

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

No Downloads
Views
Total views
10,067
On SlideShare
0
From Embeds
0
Number of Embeds
122
Actions
Shares
0
Downloads
68
Comments
0
Likes
11
Embeds 0
No embeds

No notes for slide








































































































  • Kansai.pm 10周年記念 Plack/PSGI 入門

    1. 1. CGI mod_perl Catalyst FastCGI standalone CGI mod_perl Jifty FastCGI standalone
    2. 2. Catalyst CGI mod_perl PSGI Adaptor FastCGI standalone Jifty
    3. 3. use strict; sub psgi_app {     my $env = shift;     return [         200,         [ "Content-Type" => "text/plain" ],         [ "Hello, ", "World!" ]     ]; } # .psgi &psgi_app;
    4. 4. use strict; use File::Spec; sub psgi_app {     my $env = shift;     my $path = File::Spec->catfile( "/path/to/docs", $env->{PATH_INFO} );     if (-f $path) { my $fh;         if (! open( $fh, '<', $path ) ) {             return [                 500, [ "Content-Type" => "text/plain" ], [ "Server Error" ]             ];         }         return [ 200, [ "Content-Type" => "text/plain" ], $fh ];     } else {         return [ 404, [ "Content-Type" => "text/plain" ], [ "File not found" ] ];     } } # .psgi &psgi_app;
    5. 5. > plackup -a myapp.psgi HTTP::Server::PSGI: Accepting connections at http:// 0:5000/
    6. 6. > plackup -a myapp.psgi -s HANDLER
    7. 7. > plackup -a myapp.psgi -E ENV
    8. 8. > plackup -a myapp.psgi -S /path/to/socket
    9. 9. > plackup -e “sub { ... }” -I/path/to/lib -MModuleName
    10. 10. # Apache2 <Location /> SetHandler perl-script PerlResponseHandler Plack::Handler::Apache2 PerlSetVar psgi_app /path/to/app.psgi </Location>
    11. 11. # Run as a standalone daemon plackup -s FCGI --listen /tmp/fcgi.sock --daemonize --nproc 10
    12. 12. #!/usr/bin/env plackup ... CGI code ...
    13. 13. # myapp.psgi use strict; use Plack::Middleware::Foo; use Plack::Middleware::Bar; use Plack::Middleware::Baz; my $app = sub { ... }; $app = Plack::Middlewre::Foo->wrap($app); $app = Plack::Middlewre::Bar->wrap($app); $app = Plack::Middlewre::Baz->wrap($app); $app;
    14. 14. # myapp.psgi use strict; use Plack::Middleware::Foo; use Plack::Middleware::Bar; use Plack::Middleware::Baz; my $app = sub { ... }; $app = Plack::Middlewre::Foo->wrap($app); $app = Plack::Middlewre::Bar->wrap($app); $app = Plack::Middlewre::Baz->wrap($app); $app;
    15. 15. # myapp.psgi use strict; use Plack::Builder; builder {     enable "Foo";     enable "Bar";     enable "Bar";     sub { [ 200, [ "Content-Type" => "text/plain" ], [ "Hello World" ] }; };
    16. 16. #!/usr/bin/env perl use strict; use warnings; use MyApp; MyApp->setup_engine('PSGI'); my $app = sub { MyApp->run(@_) };
    17. 17. use strict; use AnyEvent; sub {     my $env = shift;     #     return sub {         my $start_response = shift;         my $writer = $start_response->([ 200, [ "Content-Type" => "text/plain" ] ]);         my $count = 1;         my $w; $w = AE::timer 1, 10, sub {             $writer->write( "Timer $countn" );             if ($count++ > 10) {                 undef $w;             }         };     } }
    18. 18. Q4M App client client client client
    19. 19. Event Server Q4M App client client client client
    20. 20. use strict; use Plack::Request; sub {     my $env = shift;     my $req = Plack::Request->new( $env );     my $path = $req->path_info;     my $param = $req->parma('param');     my $res = $req->new_response( 200 );     return $res->finalize(); };
    21. 21. accept app listen spawn
    22. 22. accept app listen spawn
    23. 23. accept app listen spawn accept app
    24. 24. accept app listen spawn accept app
    25. 25. listen spawn accept app
    26. 26. start_server --port=80 plackup -a myapp.psgi -s Starman
    27. 27. /var/lib/mycompany/sitename.mycompany.com/MyApp /etc/service/sitename.mycompany.com run ( MyApp/deploy/run config.yaml env/ logs
    28. 28. AppX lib extlib Catalyst 5.7 AppY lib extlib Catalyst 5.8
    29. 29. cpanm --installdeps --local-lib-contained=extlib .
    30. 30. http://github.com/lestrrat/daemontools-plack-runner
    31. 31. daemontools Server::Starter perl extlib plackup myapp.psgi Framework

    ×