从除 /public 以外的 mojolicious 提供静态目录
serve static directory from mojolicious other than /public
我查看了一个 variety of places to find the best way to serve a directory of static files from within a 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中获得灵感。
我查看了一个 variety of places to find the best way to serve a directory of static files from within a 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中获得灵感。