我已经查看了很多地方,以找到从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;
Run Code Online (Sandbox Code Playgroud)
这会正确提取文件名,并且只会影响我想要的 URL,但它会提供空文件,无论它们是否存在于该目录中。我缺少什么?
小智 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' );
});
}
Run Code Online (Sandbox Code Playgroud)
或者你可以从源头获得灵感。
归档时间: |
|
查看次数: |
1145 次 |
最近记录: |