gpt4 book ai didi

perl - 将 Modperl 配置转换为 Plack - 按文件扩展名运行不同的处理程序

转载 作者:行者123 更新时间:2023-12-02 13:44:50 24 4
gpt4 key购买 nike

我正在尝试将当前的 Apache/Modperl 站点转移到 Starman,并且需要使用针对不同文件扩展名的不同处理程序构建 app.psgi。与 Apache 中的情况相同:

<LocationMatch "(\.m|\.mh|\/)$">
SetHandler perl-script
PerlHandler MyApp::Mhandler
</LocationMatch>

<LocationMatch "(\.p|\.ph)$">
SetHandler perl-script
PerlHandler MyApp::Phandler
</LocationMatch>

现在我有:

#app for handle .m and .mh
my $Mapp = Some::PSGI->handler( sub {
...
});

#app for handling .p and .ph
my $Papp = SomeOther::PSGI->handler( sub {
...
});

但是如何使用构建器?

builder {

#any extension what is not .m .mh .p .ph - handle as static
#but, only when the request have any extension
enable "Plack::Middleware::Static",
path => __what here__, ???
root => "/my/doc/root";

#and what here to achieve the following "rules".

#??? $Papp
#default $Mapp
};

需要的“规则”:

  • 如果请求没有任何扩展名,或者请求以“/”结尾
    • 应使用$Mapp处理
  • 如果请求以某些扩展名结束,那么
    • .m.mh 应由 $Mapp
    • 处理
    • .p.ph 应由 $Papp 处理
    • 所有其他带有扩展名的文件(例如 .css .js .pdf .jpg ...)应作为静态文件处理。

当然,将每个静态文件放入某个树中会容易得多,但是当前的应用程序已给出,现在我只想将其移动到 Startman 中,稍后进行重构。

最佳答案

use strictures;
use Plack::Request qw();
use Plack::Builder qw(builder enable);
use Tie::REHash do_cache => 1;

tie my %location_match, 'Tie::REHash';
%location_match = (
qr'(\.m|\.mh|/|/[^.]+)$' => sub {[200,[],['Mhandler']]},
qr'(\.p|\.ph)$' => sub {[200,[],['Phandler']]},
);

my $app = sub {
my ($env) = @_;
my $req = Plack::Request->new($env);
my $res;
if ($location_match{$req->path_info}) {
printf "path [%s] dispatches to %s\n", $req->path_info, $location_match{$req->path_info};
$res = $location_match{$req->path_info};
} else {
die sprintf "no match for path [%s], check routing configuration\n", $req->path_info;
}
return $res->($env);
};

builder {
enable 'Static', path => sub {
my ($path) = @_;
if ($location_match{$path}) {
print "redispatch\n";
return;
} elsif ($path =~ qr'/ [^/]+ [.] [^/]+ $'x) {
return 1;
} else {
die "no match for path [$path], check routing configuration\n";
}
}, root => './htdocs/';
$app;
}

__END__
GET 'http://localhost:5000/foo?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo/?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.m?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.mh?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.p?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.ph?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.css?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.js?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.pdf?bar=baz;quux#fnord'
GET 'http://localhost:5000/foo.jpg?bar=baz;quux#fnord'

关于perl - 将 Modperl 配置转换为 Plack - 按文件扩展名运行不同的处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11176786/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com