]> Dogcows Code - chaz/talk-introduction-to-psgi/blob - app.psgi
fix up for presentation
[chaz/talk-introduction-to-psgi] / app.psgi
1 #!/usr/bin/env perl
2
3 use Plack::App::Directory;
4 use Plack::App::File;
5 use Plack::Builder;
6 use Plack::Util;
7
8 my $sample_app = sub {
9 my $env = shift;
10 my $client_id = $env->{'REMOTE_ADDR'};
11
12 return [
13 '200',
14 [ 'Content-Type' => 'text/plain' ],
15 [ "Your IP address is ${client_id}." ], # or IO::Handle-like object
16 ];
17 };
18
19 my $debug_hider = sub {
20 my $app = shift;
21 return sub {
22 my $env = shift;
23 Plack::Util::response_cb($app->($env), sub {
24 my $res = shift;
25 my $headers = Plack::Util::headers($res->[1]);
26 if ( ! Plack::Util::status_with_no_entity_body($res->[0])
27 && ($headers->get('Content-Type') || '') =~ m!^(?:text/html|application/xhtml\+xml)!) {
28
29 my $content = q{
30 <script>
31 /* Only show Debug middleware on the "middleware-debug" slide. */
32 slideshow.on('showSlide', function (slide) {
33 if (slide.properties.name === 'middleware-debug') {
34 document.getElementById('plDebug').style.display = 'block';
35 }
36 });
37 slideshow.on('hideSlide', function (slide) {
38 document.getElementById('plDebug').style.display = 'none';
39 });
40 document.getElementById('plDebug').style.display = 'none';
41 </script>
42 };
43
44 return sub {
45 my $chunk = shift;
46 return unless defined $chunk;
47 $chunk =~ s!(?=</body>)!$content!i;
48 return $chunk;
49 };
50 }
51 });
52 };
53 };
54
55 builder {
56 enable $debug_hider;
57 enable 'Debug';
58
59 mount '/myip' => $sample_app;
60
61 mount '/css' => Plack::App::Directory->new(root => 'css')->to_app;
62 mount '/img' => Plack::App::Directory->new(root => 'img')->to_app;
63 mount '/js' => Plack::App::Directory->new(root => 'js')->to_app;
64 mount '/remark.min.js' => Plack::App::File->new(file => 'remark.min.js')->to_app;
65 mount '/' => Plack::App::File->new(file => 'slides-offline.html')->to_app;
66 };
67
This page took 0.031993 seconds and 4 git commands to generate.