将Modperl配置转换为Plack – 通过文件扩展名运行不同的处理程序
发布时间:2020-12-15 23:36:32 所属栏目:大数据 来源:网络整理
导读:我正在尝试将我当前的Apache / Modperl站点转移到Starman,并且需要使用不同的处理程序构建app.psgi以用于不同的文件扩展名.像Apache一样的东西: LocationMatch "(.m|.mh|/)$" SetHandler perl-script PerlHandler MyApp::Mhandler/LocationMatchLocation
我正在尝试将我当前的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处理 当然,将每个静态文件放入某个树会更容易,但是当前的应用程序已经给出了,现在我只想将它移动到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 %sn",$req->path_info,$location_match{$req->path_info}; $res = $location_match{$req->path_info}; } else { die sprintf "no match for path [%s],check routing configurationn",$req->path_info; } return $res->($env); }; builder { enable 'Static',path => sub { my ($path) = @_; if ($location_match{$path}) { print "redispatchn"; return; } elsif ($path =~ qr'/ [^/]+ [.] [^/]+ $'x) { return 1; } else { die "no match for path [$path],check routing configurationn"; } },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' (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |