ठीक है, यह करने का अच्छा तरीका है। यह निश्चित रूप से एक प्लगइन हो सकता है।
आप डांसर की कोर के अंदर हैकिंग से बातें इस तरह कभी नहीं करना चाहिए, यदि आप इसके बजाय हमेशा एक मार्ग हैंडलर को लागू करने काम करने के लिए विचार करना चाहिए:
#!/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;
इस अनुप्रयोग चल रहा का एक उदाहरण:
$ 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
में होगा विचार है कि दोनों निर्देशिका अलग रहने चाहिए कर सकते हैं। – bliof
@bliof मैंने अपना जवाब अपडेट किया है, आप इसे आजमा सकते हैं, लेकिन बेहतर आप nginx के साथ जाते हैं। – yko
मैं कुछ और सोच रहा था। क्या मैं एक प्लगइन 'foo' बना सकता हूं और इसे 'बार' में उपयोग कर सकता हूं? वास्तविक सवाल यह है कि डांसर प्लगइन में नर्तक ऐप की कार्यक्षमता होती है। मेरा मतलब है कि मैं उससे यूआरएल मैप कर सकता हूं और अपने 'विचार' फ़ोल्डर से टेम्पलेट लोड कर सकता हूं?/मैंने कभी प्लगइन/ – bliof