Tamam, işte yapmanız gereken en iyi yol. Elbette bir eklenti olabilir.
Sen Dancer çekirdeğinde içine hack yoluyla bu tür şeyleri yapmaya asla
, yerine her zaman işi yapmak için bir rota işleyicisi uygulamak düşünmelisiniz:
#!/usr/bin/env perl
use Dancer;
use File::Spec;
use Dancer::FileUtils 'read_file_content';
use Dancer::MIME;
use HTTP::Date;
# your routes here
# then the catchall route for
# serving static files
# better in config
my @public_dirs = qw(/tmp/test/foo /tmp/test/bar /tmp/test/baz);
get '/**' => sub {
my $path = request->path;
my $mime = Dancer::MIME->instance;
# security checks
return send_error("unauthrorized request", 403) if $path =~ /\0/;
return send_error("unauthrorized request", 403) if $path =~ /\.\./;
# decompose the path_info into a file path
my @path = split '/', $path;
for my $location (@public_dirs) {
my $file_path = File::Spec->catfile($location, @path);
next if ! -f $file_path;
my $content = read_file_content($file_path);
my $content_type = $mime->for_file($file_path);
my @stat = stat $file_path;
header 'Content-Type', $content_type;
header 'Content-Length', $stat[7];
header 'Last-Modified', HTTP::Date::time2str($stat[9]);
return $content;
}
pass;
};
start;
Bu uygulamanın çalışması örneği:
$ mkdir -p /tmp/test/foo /tmp/test/bar /tmp/test/baz
$ echo 1 > /tmp/test/foo/foo.txt
$ echo 2 > /tmp/test/bar/bar.txt
$ echo 3 > /tmp/test/baz/baz.txt
$ ./bin/app.pl
$ curl -I http://0:3000/baz.txt
HTTP/1.0 200 OK
Content-Length: 2
Content-Type: text/plain
Last-Modified: Fri, 14 Oct 2011 11:28:03 GMT
X-Powered-By: Perl Dancer 1.3051
olacak Yakında GitHub'dan bugün bu modülü yüklendi edebilirsiniz. – bliof
@bliof Cevabımı güncelledim, bunu deneyebilirsiniz, ancak nginx ile devam etseniz iyi olur. – yko
Başka bir şey düşünüyordum. Bir eklentiyi 'foo' yapabilir miyim ve 'bar' içinde kullanabilir miyim? Asıl soru, bir Dancer eklentisinin bir dansçı uygulamasının işlevselliğine sahip olmasıdır. URL'leri ondan eşleyebilir ve 'views' klasöründen şablon yükleyebilir miyim?/Hiç bir eklenti yazmamıştım/ – bliof