Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Last active October 26, 2017 10:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/092705c2ccb246d79d85 to your computer and use it in GitHub Desktop.
Save briandfoy/092705c2ccb246d79d85 to your computer and use it in GitHub Desktop.
A small static file server with a default index file, in Mojolicious
#!/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