我查看了variety places,以便找到在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,但它会提供空文件,无论它们是否存在于该目录中。我错过了什么?
答案 0 :(得分:2)
可能最简单的方法是使用插件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获得灵感。