secret
Last active

Example counter PSGI app for Twiggy server

  • Download Gist
counter.psgi
Perl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
# Invoke with:
# twiggy counter.psgi
 
use AnyEvent;
 
my @timers;
$main::timer = AnyEvent->timer(after => 2, interval => 2, cb => sub {
@timers = grep { $_->() } @timers;
});
 
my $app = sub {
my $env = shift;
 
return sub {
my $respond = shift;
my $writer = $respond->([200, ['Content-Type', 'text/html']]);
my $count = 5;
 
push @timers, sub {
my $dt = localtime;
$writer->write("[ $dt ]\n");
 
if (--$count == 0) {
$writer->close();
}
 
return $count > 0;
};
};
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.