perl – 从/ public以外的mojolicious提供静态目录

我已经查看了
places
variety,以便找到在
mojolicious应用程序中提供静态文件目录的最佳方式,这与我能够得到的一样接近:

package ExampleServer;
use Mojo::Base 'Mojolicious';
use Mojolicious::Static;

# This method will run once at server start
sub startup {
    my $self = shift;

    $ENV{MOJO_REVERSE_PROXY} = 1;

    # TODO: generalize
    my $static_path = '/www/example/docroot/.well-known/acme-challenge/';

    # Router
    my $r = $self->routes;

    # Normal route to controller
    $r->get('/')->to('example#welcome');

    # serve static directory
    $r->get('/.well-known/acme-challenge/*filename' => sub {
        my $self = shift;
        my $filename = $self->stash('filename');
        my $fqfn = $static_path . $filename;
        $self->app->log->debug($fqfn);
        my $static = Mojolicious::Static->new( paths => [ $static_path ] );

        $static->serve($self, $fqfn);
        $self->rendered;
    });
}

1;

这是正确地提取文件名,它只影响我想要的URL,但它提供空文件,无论它们是否存在于该目录中.我错过了什么?

最佳答案 可能最简单的方法是使用插件
RenderFile

package ExampleServer;
use Mojo::Base 'Mojolicious';
use Mojolicious::Static;

# This method will run once at server start
sub startup {
   my $self = shift;

   $self->plugin('RenderFile');

   $ENV{MOJO_REVERSE_PROXY} = 1;

   # TODO: generalize
   my $static_path = '/www/example/docroot/.well-known/acme-challenge/';

   # Router
   my $r = $self->routes;

   # Normal route to controller
   $r->get('/')->to('example#welcome');

   # serve static directory
   $r->get('/.well-known/acme-challenge/*filename' => sub {
       my $self = shift;
       my $filename = $self->stash('filename');
       my $fqfn = $static_path . $filename;
       $self->app->log->debug($fqfn);
       $self->render_file(filepath=> $fqfn, format => 'txt', content_disposition => 'inline' );
   });
}

或者你可以从source获得灵感.

点赞