1Plack::Middleware::CondUisteironCaoln(t3r)ibuted Perl DoPcluamcekn:t:aMtiidodnleware::Conditional(3)
2
3
4
6 Plack::Middleware::Conditional - Conditional wrapper for Plack
7 middleware
8
10 use Plack::Builder;
11
12 builder {
13 enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 'StackTrace', force => 1;
14 $app;
15 };
16
17 # or using the OO interface:
18 $app = Plack::Middleware::Conditional->wrap(
19 $app,
20 condition => sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' },
21 builder => sub { Plack::Middleware::StackTrace->wrap($_[0], force => 1) },
22 );
23
25 Plack::Middleware::Conditional is a piece of meta-middleware, to run a
26 specific middleware component under runtime conditions. The goal of
27 this middleware is to avoid baking runtime configuration options in
28 individual middleware components, and rather share them as another
29 middleware component.
30
32 Note that some of the middleware component names are just made up for
33 the explanation and might not exist.
34
35 # Minify JavaScript if the browser is Firefox
36 enable_if { $_[0]->{HTTP_USER_AGENT} =~ /Firefox/ } 'JavaScriptMinifier';
37
38 # Enable Stacktrace when being accessed from the local network
39 enable_if { $_[0]->{REMOTE_ADDR} =~ /^10\.0\.1\.*/ } 'StackTrace';
40
41 # Work with other conditional setter middleware:
42 # Transcode Jpeg on the fly for mobile clients
43 builder {
44 enable 'MobileDetector';
45 enable_if { $_[0]->{'plack.mobile_detected'} }
46 'TranscodeJpeg', max_size => 30_000;
47 $app;
48 };
49
50 Note that in the last example MobileDetector should come first because
51 the conditional check runs in pre-run conditions, which is from outer
52 to inner: that is, from the top to the bottom in the Builder DSL code.
53
55 Tatsuhiko Miyagawa
56
57 Steve Cook
58
60 Plack::Builder
61
62
63
64perl v5.28.1 2018-02-10 Plack::Middleware::Conditional(3)