]> Dogcows Code - chaz/talk-introduction-to-psgi/commitdiff
add example psgi app and slide of debug middleware
authorCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 11 Jul 2016 23:51:53 +0000 (17:51 -0600)
committerCharles McGarvey <chazmcgarvey@brokenzipper.com>
Mon, 11 Jul 2016 23:55:20 +0000 (17:55 -0600)
Makefile
README.md
app.psgi [new file with mode: 0644]
slides.html

index eb0b95f79ef59f90571d72a102a7e45abef1d148..0f98a52f0386ff2e4468647f24a33639a6298fca 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -2,6 +2,7 @@
 SLIDES  = introduction-to-psgi
 BROWSER = chrome
 DOT     = dot
+PLACKUP = plackup
 
 dotfiles = $(shell find . -iname '*.dot')
 svgfiles = $(patsubst %.dot,%.svg,$(dotfiles))
@@ -18,6 +19,9 @@ pdf: $(SLIDES).pdf
 run: $(svgfiles)
        $(BROWSER) slides.html
 
+run-app: offline
+       $(PLACKUP)
+
 run-offline: offline
        $(BROWSER) slides-offline.html
 
@@ -34,5 +38,5 @@ slides-offline.html: slides.html
 remark.min.js:
        curl -Lo $@ https://gnab.github.io/remark/downloads/remark-latest.min.js
 
-.PHONY: all clean offline pdf run run-offline
+.PHONY: all clean offline pdf run run-app run-offline
 
index 9ca631dbbe842ab554021f70bbc1bd6704ba4f10..7b4a993c8ff15d93b7b8b2798facadb05d3247ce 100644 (file)
--- a/README.md
+++ b/README.md
@@ -12,6 +12,10 @@ To view the slides:
 
     make run
 
+To build slides and run as a PSGI app:
+
+    make run-app
+
 To build a PDF version of the slides:
 
     make pdf
diff --git a/app.psgi b/app.psgi
new file mode 100644 (file)
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{
+<script>
+/* Only show Debug middleware on the "middleware-debug" slide. */
+slideshow.on('showSlide', function (slide) {
+    if (slide.properties.name === 'middleware-debug') {
+        document.getElementById('plDebug').style.display = 'block';
+    }
+});
+slideshow.on('hideSlide', function (slide) {
+    document.getElementById('plDebug').style.display = 'none';
+});
+document.getElementById('plDebug').style.display = 'none';
+</script>
+                };
+
+                return sub {
+                    my $chunk = shift;
+                    return unless defined $chunk;
+                    $chunk =~ s!(?=</body>)!$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;
+};
+
index 5781fc8c771872a822103ff5aea0e17d69688726..fef0bc2a56e47c841dfd48358d97a74927100f8d 100644 (file)
@@ -1338,6 +1338,16 @@ layout: true
 
 ---
 
+name:   middleware-debug
+
+### [`Debug`](https://metacpan.org/pod/Plack::Middleware::Debug)
+
+```perl
+enable 'Debug';
+```
+
+---
+
 ### [`ReverseProxy`](https://metacpan.org/pod/Plack::Middleware::ReverseProxy)
 
 ```perl
@@ -1418,6 +1428,7 @@ layout: false
   - Logging
   - Error handling
   - Sessions
+  - Rate limiters
 
 --
 - PSGI also specifies a way to delay or stream responses to the server.
@@ -1472,5 +1483,5 @@ Leave me feedback, if you want:
 ]
 ]
 
-</textarea><script src="https://gnab.github.io/remark/downloads/remark-latest.min.js"></script><script>remark.create({countIncrementalSlides: true, highlightLanguage: 'perl', highlightLines: true, ratio: '16:9', /*slideNumberFormat: '',*/ navigation: {scroll: false, touch: false, click: false}})</script></body></html>
+</textarea><script src="https://gnab.github.io/remark/downloads/remark-latest.min.js"></script><script>var slideshow = remark.create({countIncrementalSlides: true, highlightLanguage: 'perl', highlightLines: true, ratio: '16:9', /*slideNumberFormat: '',*/ navigation: {scroll: false, touch: false, click: false}})</script></body></html>
 <!-- vim: set ts=4 sts=4 sw=4 tw=120 et ft=markdown nowrap: -->
This page took 0.025681 seconds and 4 git commands to generate.