Multiple application directories with Dancer perl

Is there a way to have one application in a dancer, but with several applications.

Or I could do something like this:

My project is in the foo directory. And let it be said that I have a dir 'bar' (not inside 'foo'), which has a directory called public. I, that my application is “foo”, to use this publication as its own publication, and if it is looking, let it say “/css/style.css” and it is not in “/ bar / public /”, it should search "/ foo / public /". How can i do this?

+5
source share
3 answers

One way is to write a plugin that displays static (and replaces some functions). You can use Dancer :: Plugin :: Thumbnail as an example.

Another way I see is monkey-patch get_file_response()in Dancer :: Renderer , which is actually not such a good idea.

The following code searches for static files in each directory from an array @dirs. He is dirty, ugly and insecure. This may be broken in a future version and may cause problems with other components of the Dancer framework that I am not familiar with. You are warned.

#!/usr/bin/env perl
use Dancer;
use Dancer::Renderer;
use MyWeb::App;

my $get_file_response_original = \&Dancer::Renderer::get_file_response;
my @dirs = ('foo');

*Dancer::Renderer::get_file_response = sub {
    my $app = Dancer::App->current;

    my $result;

    # Try to find static in default dir
    if ($result = $get_file_response_original->(@_)) {
        return $result;
    }

    # Save current settings
    my $path_backup = $app->setting('public');

    # Go through additional dirs
    foreach my $dir (@dirs) {
        $app->setting(public => $dir);
        if ($result = $get_file_response_original->(@_)) {
            last;
        }
    }

    # Restore public
    $app->setting('public' => $path_backup);

    return $result
};

dance;

The third way is to let nginx just do the job for you by writing the correct nginx configuration for your application.

+2
source

, . , .

, ​​Dancer, :

#!/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
+7

Maybe this module will help you? https://github.com/Perlover/Dancer-Plugin-Hosts You can configure virtual sites in Dancer using your own appdir settings and other directories Today I uploaded this module to github Coming soon

-1
source

All Articles