Converting Modperl configuration to Plack - running different handlers using the file extension

This is me trying to port my current Apache / Modperl site to Starman and you need to create app.psgi with different handlers for different file extensions. Somthing like in Apache:

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

Now I have:

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

but how to use the builder?

 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 }; 

Necessary "rules":

  • if the request does not have an extension, or the request ends with '/'
    • should be processed using $Mapp
  • if the request ends with some extension, then
    • .m and .mh must be handled by $Mapp
    • .p and .ph must be handled by $Papp
    • all other files with extensions (e.g. .css.js.pdf.jpg ...) should be treated as static.

Of course, it’s much easier to put each static file in some tree, but the current application will be given, and now I only want to move it to Startman, refactoring later.

+6
source share
1 answer
 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' 
+2
source

Source: https://habr.com/ru/post/918868/


All Articles