OK, ecco il modo migliore per farlo. Ovviamente può essere un plugin.
Non si dovrebbe mai fare questo tipo di cose con l'hacking all'interno nucleo del ballerino, si dovrebbe invece sempre considerare l'implementazione di un gestore di percorso per fare il lavoro:
#!/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;
Un esempio di questa applicazione in esecuzione:
$ 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
L'idea è che entrambe le directory dovrebbero rimanere separati. – bliof
@bliof Ho aggiornato la mia risposta, si può provare questo, ma meglio si va con nginx. – yko
Stavo pensando qualcos'altro. Potrei creare un plug-in "foo" e usarlo in "bar"? La vera domanda è che un plugin di Dancer ha la funzionalità di un'app ballerina. Voglio dire potrei mappare url da esso e caricare i modelli dalla sua cartella 'views'?/Non ho mai scritto un plug-in/ – bliof