The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Mojolicious::Plugin::DirectoryServer - Serve static files from document root with directory index

SYNOPSIS

  # simple usage
  use Mojolicious::Lite;
  plugin( 'DirectoryServer', root => "/path/to/htdocs" )->start;

  # with handler
  use Text::Markdown qw{ markdown };
  use Path::Class;
  use Encode qw{ decode_utf8 };
  plugin('DirectoryServer', root => "/path/to/htdocs", handler => sub {
      my ($c, $path) = @_;
      if ( -f $path && $path =~ /\.(md|mkdn)$/ ) {
          my $text = file($path)->slurp;
          my $html = markdown( decode_utf8($text) );
          $c->render( inline => $html );
      }
  })->start;

or

  > perl -Mojo -E 'a->plugin("DirectoryServer", root => "/path/to/htdocs")->start' daemon

or

  > perl -Miniweb

DESCRIPTION

Mojolicious::Plugin::DirectoryServer is a static file server with a directory index similar to Apache's mod_autoindex.

Methods

Mojolicious::Plugin::DirectoryServer inherits all methods from Mojolicious::Plugin.

Options

Mojolicious::Plugin::DirectoryServer supports the following options.

  • root

      # Mojolicious::Lite
      plugin DirectoryServer => { root => "/path/to/htdocs" };

    Document root directory. Defaults to the current directory.

    If root is a file, serve only root file.

  • auto_index

       # Mojolicious::Lite
       plugin DirectoryServer => { auto_index => 0 };

    Automatically generate index page for directory, default true.

  • dir_index

      # Mojolicious::Lite
      plugin DirectoryServer => { dir_index => [qw/index.html index.htm/] };

    Like a Apache's DirectoryIndex directive.

  • dir_page

      # Mojolicious::Lite
      plugin DirectoryServer => { dir_page => $template_str };

    a HTML template of index page

  • handler

      # Mojolicious::Lite
      use Text::Markdown qw{ markdown };
      use Path::Class;
      use Encode qw{ decode_utf8 };
      plugin DirectoryServer => {
          handler => sub {
              my ($c, $path) = @_;
              if ($path =~ /\.(md|mkdn)$/) {
                  my $text = file($path)->slurp;
                  my $html = markdown( decode_utf8($text) );
                  $c->render( inline => $html );
              }
          }
      };

    CODEREF for handle a request file.

    If not rendered in CODEREF, serve as static file.

  • json

      # Mojolicious::Lite
      # /dir (Accept: application/json)
      # /dir?_format=json
      plugin DirectoryServer => { json => 1 };

    Enable json response.

AUTHOR

The original author was hayajo <hayajo@cpan.org>.

The module was forked by brian d foy <bdfoy@cpan.org> after the module was abandoned.

CONTRIBUTORS

Many thanks to the contributors for their work.

ChinaXing

SEE ALSO

Plack::App::Directory, iniweb.

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.