Skip to content

Instantly share code, notes, and snippets.

@bobtfish
Forked from jjn1056/gist:2036529
Created March 14, 2012 13:49
Show Gist options
  • Save bobtfish/2036607 to your computer and use it in GitHub Desktop.
Save bobtfish/2036607 to your computer and use it in GitHub Desktop.
package Shutterstock::Example::Nonblock;
use Web::Simple;
use Plack::App::File;
use File::ShareDir::ProjectDistDir;
use File::Spec::Functions;
use Imager::Simple;
sub share { our $share ||= dist_dir 'Shutterstock-Example-Nonblock' }
sub dispatch_request {
sub (/) {
Plack::App::File->new(file =>
catfile shift->share, 'html', 'index.html')},
sub (/static/...) {
Plack::App::File->new(root =>
catdir shift->share, 'static')},
sub (/img/*) {
my ($self, $id) = @_;
[sub {
my $w = (my $respond = shift)
->([ 200, ['Content-Type' => 'image/jpeg'] ]);
ny $fn = catfile $self->share, 'data', "$id.jpg";
AnyEvent::Util::fork_call {
my $fn = shift;
my $img = Imager::Simple
->read($fn)
->scale(200, 200, 'min');
return $img->data;
}, $fn, sub {
my $data = shift;
$w->write($data);
$w->close;
undef $w;
};
}]
}
}
__PACKAGE__->run_if_script;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment