5

ダンサーに1つのアプリを含めるが、複数のappdirを使用する方法はありますか?

または私はこのようなことをすることができます:

私のプロジェクトはdir'foo'にあります。そして、「public」というディレクトリを持つdir「bar」(「foo」内ではない)があるとしましょう。私のアプリ「foo」は、このパブリックを独自のパブリックとして使用します。「/ css / style.css」を検索し、「/ bar / public /」にない場合は、「/foo/」を検索する必要があります。公衆/'。どうやってやるの?

4

3 に答える 3

7

OK、これが良い方法です。もちろんプラグインにすることもできます。

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
于 2011-10-14T11:41:17.883 に答える
2

静的にレンダリングする(そして一部の機能を置き換える)プラグインを作成する場合の方法の1つ。例として、Dancer :: Plugin::Thumbnailを使用できます。

私が見る他の方法は、 Dancer::Rendererでモンキーパッチget_file_response()を適用することです。これはあまり良い考えではありません。

@dirs次のコードは、配列から各ディレクトリで静的ファイルを検索します。それは汚く、醜く、安全ではありません。これは将来のバージョンで壊れる可能性があり、私がよく知らないDancerフレームワークの他の部分で問題を引き起こす可能性があります。警告されます。

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

3番目の方法は、アプリケーションに適切なnginx構成を記述して、nginxにこの作業を実行させることです。

于 2011-09-07T13:09:39.370 に答える
-1

このモジュールはあなたに役立つかもしれませんか? https://github.com/Perlover/Dancer-Plugin-Hosts 独自のappdirと他のディレクトリ設定を使用してDancerで仮想サイトをセットアップできます今日このモジュールをgithubにアップロードしましたまもなくCPANになります

于 2011-10-17T14:09:38.673 に答える