Created
January 5, 2018 03:08
-
-
Save akanehara/43481f58703257e0d8fb28930538aa94 to your computer and use it in GitHub Desktop.
ストリーミングPlackアプリケーションとMiddlewareのスケルトン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use warnings; | |
use FindBin; | |
use lib $FindBin::Bin . ''; | |
use POSIX qw(EPIPE); | |
use IO::Select; | |
use Data::Dumper; | |
use Plack::Builder; | |
my $app = sub { | |
my $env = shift; | |
return sub { | |
my $responder = shift; | |
my $writer = $responder->([200, ['Content-Type', 'text/plain']]); | |
for (my $i=1; $i <= 10; $i++) { | |
my $w = $writer->write("$i\n"); | |
if ($! == EPIPE) { | |
warn "Broken pipe.\n"; | |
last; | |
} | |
} | |
warn "DONE\n"; | |
$writer->close; | |
}; | |
}; | |
builder { | |
enable "Foo", file_etag => [qw/size/]; | |
$app; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Plack::Middleware::Foo; | |
use parent qw( Plack::Middleware ); | |
sub call { | |
my ($self, $env) = @_; | |
warn "middleware pre\n"; | |
my $res = $self->app->($env); | |
warn "middleware post\n"; | |
return Plack::Util::response_cb($res, | |
sub { | |
my $res = shift; | |
# do something with $res; | |
warn "middleware response_cb\n"; | |
} | |
); | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment