X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Ftalk-introduction-to-psgi;a=blobdiff_plain;f=app.psgi;fp=app.psgi;h=2175cd899d4c3be81fcfcad1fb054456accf9c84;hp=0000000000000000000000000000000000000000;hb=c3702aa22054180570f09c504f29b7a3b183225b;hpb=499f996ae29359b7fd5179cad7e6cb367023354d diff --git a/app.psgi b/app.psgi new file mode 100644 index 0000000..2175cd8 --- /dev/null +++ b/app.psgi @@ -0,0 +1,66 @@ +#!/usr/bin/env perl + +use Plack::App::Directory; +use Plack::App::File; +use Plack::Builder; +use Plack::Util; + +my $sample_app = sub { + my $env = shift; + my $client_id = $env->{'REMOTE_ADDR'}; + + return [ + '200', + [ 'Content-Type' => 'text/plain' ], + [ "Your IP address is ${client_id}." ], # or IO::Handle-like object + ]; +}; + +my $debug_hider = sub { + my $app = shift; + return sub { + my $env = shift; + Plack::Util::response_cb($app->($env), sub { + my $res = shift; + my $headers = Plack::Util::headers($res->[1]); + if ( ! Plack::Util::status_with_no_entity_body($res->[0]) + && ($headers->get('Content-Type') || '') =~ m!^(?:text/html|application/xhtml\+xml)!) { + + my $content = q{ + + }; + + return sub { + my $chunk = shift; + return unless defined $chunk; + $chunk =~ s!(?=)!$content!i; + return $chunk; + }; + } + }); + }; +}; + +builder { + enable $debug_hider; + enable 'Debug'; + + mount '/myip' => $sample_app; + + mount '/css' => Plack::App::Directory->new(root => 'css')->to_app; + mount '/img' => Plack::App::Directory->new(root => 'img')->to_app; + mount '/remark.min.js' => Plack::App::File->new(file => 'remark.min.js')->to_app; + mount '/' => Plack::App::File->new(file => 'slides-offline.html')->to_app; +}; +