Skip to content

Instantly share code, notes, and snippets.

@knutov
Last active December 11, 2015 02:48
Show Gist options
  • Save knutov/4533244 to your computer and use it in GitHub Desktop.
Save knutov/4533244 to your computer and use it in GitHub Desktop.
# http://blogs.perl.org/users/joel_berger/2013/01/a-quick-static-file-webserver.html
# alias pws="plackup -MPlack::App::File -e 'Plack::App::File->new->to_app'"
# theory: directory requests (by that i assume directory indexes) would work if you s/File/Directory/g :)
#!/usr/bin/env perl
package Plack::App::IndexFile;
use parent 'Plack::App::File';
sub locate_file
{
my ($self, $env) = @_;
my $path = $env->{PATH_INFO} || '';
return $self->SUPER::locate_file( $env ) unless $path && $path =~ m{/$};
$env->{PATH_INFO} .= 'index.html';
return $self->SUPER::locate_file( $env );
}
package main;
use Plack::Runner;
my $app = Plack::App::IndexFile->new({ root => shift })->to_app;
my $runner = Plack::Runner->new;
$runner->parse_options( '--access-log' => '/dev/null', @ARGV );
$runner->run( $app );
#-----
__END__
pws () {
plackup -MPlack::App::File -e '
package PWS;
use parent "Plack::App::File";
sub locate_file {
$_[1]->{PATH_INFO} .= "index.html"
if $_[1]->{PATH_INFO} =~ m{/$};
$_[0]->SUPER::locate_file($_[1]);
}
PWS->new->to_app;
'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment