Last active
October 26, 2017 10:29
-
-
Save briandfoy/092705c2ccb246d79d85 to your computer and use it in GitHub Desktop.
A small static file server with a default index file, in Mojolicious
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/Users/brian/bin/perls/perl5.20.0 | |
use v5.10; | |
say "ARGV is @ARGV"; | |
use Mojolicious::Lite; | |
my $default_index = 'index.html'; | |
app->static->paths->[0] = $ARGV[1]; | |
app->hook( | |
before_dispatch => sub { | |
my $c = shift; | |
state $base = $ARGV[1]; | |
my $url_path = $c->req->url; | |
my $path = "$base$url_path"; | |
if( -d $path ) { | |
$url_path .= '/' unless $path =~ m|/\z|; | |
$url_path .= $default_index; | |
$c->req->url->path( $url_path ); | |
} | |
} | |
); | |
any '/' => sub { | |
shift->reply->static('index.html'); | |
}; | |
app->start; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment