我已经查看了各种地方,以找到从繁琐的应用程序内提供静态文件目录的最佳方式,这是我所能得到的:
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,但它提供空文件,无论它们是否存在于该目录中。 我错过了什么?
可能最简单的方法是使用插件渲染文件:
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' );
});
}
或者你可以从源头获得灵感。