From c3702aa22054180570f09c504f29b7a3b183225b Mon Sep 17 00:00:00 2001 From: Charles McGarvey Date: Mon, 11 Jul 2016 17:51:53 -0600 Subject: [PATCH] add example psgi app and slide of debug middleware --- Makefile | 6 ++++- README.md | 4 ++++ app.psgi | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++ slides.html | 13 ++++++++++- 4 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 app.psgi diff --git a/Makefile b/Makefile index eb0b95f..0f98a52 100644 --- 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 diff --git a/README.md b/README.md index 9ca631d..7b4a993 100644 --- 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 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; +}; + diff --git a/slides.html b/slides.html index 5781fc8..fef0bc2 100644 --- a/slides.html +++ b/slides.html @@ -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: ] ] - + -- 2.43.0