Skip to content

Instantly share code, notes, and snippets.

@soh335
Created May 2, 2010 15:19
Show Gist options
  • Save soh335/387195 to your computer and use it in GitHub Desktop.
Save soh335/387195 to your computer and use it in GitHub Desktop.
use Plack::Request;
use Plack::App::File;
use LWP::UserAgent;
use File::Basename;
use Path::Class qw/dir/;
my %cache;
return sub {
my $env = shift;
my $req = Plack::Request->new($env);
my $res;
if ( !defined($cache{$req->request_uri}) ) {
my $file_path = dir('.', basename($req->path_info) )->absolute->stringify;
if ( -f $file_path ) {
$res = Plack::App::File->new( file => $file_path )->call($env);
}
else
{
my $_req = HTTP::Request->new(
$req->method,
$req->request_uri,
$req->headers,
$req->raw_body
);
my $ua = LWP::UserAgent->new;
my $ua_res = $ua->request( $_req );
my @res_header;
$ua_res->headers->scan(sub{ push @res_header, @_; });
$res = [ $ua_res->code, \@res_header, [$ua_res->content] ];
}
$cache{$req->request_uri} = $res;
}
$cache{$req->request_uri};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment