diff options
-rw-r--r-- | app.psgi | 45 |
1 files changed, 30 insertions, 15 deletions
@@ -46,6 +46,8 @@ use constant STATIC => qw( skins ); +$ENV{BZ_PLACK} = 'Plack/' . Plack->VERSION; + my $app = builder { my $static_paths = join( '|', sort { length $b <=> length $a || $a cmp $b } STATIC ); @@ -53,41 +55,54 @@ my $app = builder { path => qr{^/($static_paths)/}, root => Bugzilla::Constants::bz_locations->{cgi_path}; - $ENV{BZ_PLACK} = 'Plack/' . Plack->VERSION; - - my $map = Plack::App::URLMap->new; - - my @cgis = glob('*.cgi'); my $shutdown_app = Plack::App::WrapCGI->new( script => 'shutdown.cgi' )->to_app; + my @scripts = glob('*.cgi'); - foreach my $cgi_script (@cgis) { + my %cgi_app; + foreach my $script (@scripts) { + my $base_name = basename($script); + + next if $base_name eq 'shutdown.cgi'; my $app - = eval { Plack::App::WrapCGI->new( script => $cgi_script )->to_app }; + = eval { Plack::App::WrapCGI->new( script => $script )->to_app }; # Some CGI scripts won't compile if not all optional Perl modules are # installed. That's expected. - if ($@) { - warn "Cannot compile $cgi_script. Skipping!\n"; + unless ($app) { + warn "Cannot compile $script: $@\nSkipping!\n"; next; } my $wrapper = sub { my $ret = Bugzilla::init_page(); my $res - = ( $ret eq '-1' && $cgi_script ne 'editparams.cgi' ) + = ( $ret eq '-1' && $script ne 'editparams.cgi' ) ? $shutdown_app->(@_) : $app->(@_); Bugzilla::_cleanup(); return $res; }; + $cgi_app{$base_name} = $wrapper; + } - my $base_name = basename($cgi_script); - $map->map( '/' => $wrapper ) if $cgi_script eq 'index.cgi'; - $map->map( '/rest' => $wrapper ) if $cgi_script eq 'rest.cgi'; - $map->map( "/$base_name" => $wrapper ); + foreach my $cgi_name ( keys %cgi_app ) { + mount "/$cgi_name" => $cgi_app{$cgi_name}; } - $map->to_app; + + # so mount / => $app will make *all* files redirect to the index. + # instead we use an inline middleware to rewrite / to /index.cgi + enable sub { + my $app = shift; + return sub { + my $env = shift; + $env->{PATH_INFO} = '/index.cgi' if $env->{PATH_INFO} eq '/'; + return $app->($env); + }; + }; + + mount "/rest" => $cgi_app{"rest.cgi"}; + }; unless (caller) { |