Skip to content

Instantly share code, notes, and snippets.

@akanehara
Created January 5, 2018 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akanehara/43481f58703257e0d8fb28930538aa94 to your computer and use it in GitHub Desktop.
Save akanehara/43481f58703257e0d8fb28930538aa94 to your computer and use it in GitHub Desktop.
ストリーミングPlackアプリケーションとMiddlewareのスケルトン
#!/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;
};
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