Skip to content

Instantly share code, notes, and snippets.

@mala
Created January 23, 2013 19:55
Show Gist options
  • Save mala/4612270 to your computer and use it in GitHub Desktop.
Save mala/4612270 to your computer and use it in GitHub Desktop.
use strict;
use Plack::Builder;
use Plack::Request;
use Plack::App::Directory;
use Path::Class;
use Digest::MD5 qw(md5_hex);
my $upload_dir = "./files/";
builder {
mount "/files" => Plack::App::Directory->new(root => $upload_dir);
mount "/upload" => sub {
my $env = shift;
my $req = Plack::Request->new($env);
if ($req->method eq "POST") {
my $file = file($upload_dir . md5_hex($req->param("key")));
my $appender = $file->open('a') or die $!;
$appender->print($req->raw_body);
$appender->close;
# warn $req->raw_body;
}
[200, [], ["OK"]];
};
mount "/" => sub {
my $env = shift;
if ($env->{PATH_INFO} eq "/") {
return Plack::App::File->new(file => './static/index.html')->call($env);
}
Plack::App::File->new(root => './static/')->call($env);
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment