From 8bbc156ca9a4bf3bfff8a9b7014a002808b1b7f0 Mon Sep 17 00:00:00 2001 From: Frédéric Buclin Date: Wed, 23 Dec 2015 20:46:43 +0100 Subject: Bug 1201113: Support to run Bugzilla as a PSGI application r=dylan --- app.psgi | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 app.psgi (limited to 'app.psgi') diff --git a/app.psgi b/app.psgi new file mode 100644 index 000000000..c04359fb1 --- /dev/null +++ b/app.psgi @@ -0,0 +1,70 @@ +#!/usr/bin/perl +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +use 5.10.1; +use strict; +use warnings; + +use File::Basename; +use lib dirname(__FILE__); +use Bugzilla::Constants (); +use lib Bugzilla::Constants::bz_locations()->{ext_libpath}; + +use Plack; +use Plack::Builder; +use Plack::App::URLMap; +use Plack::App::WrapCGI; +use Plack::Response; + +use constant STATIC => qw( + data/assets + data/webdot + docs + extensions/[^/]+/web + graphs + images + js + skins +); + +builder { + my $static_paths = join('|', STATIC); + enable 'Static', + 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; + + foreach my $cgi_script (@cgis) { + my $app = eval { Plack::App::WrapCGI->new(script => $cgi_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"; + next; + } + + my $wrapper = sub { + my $ret = Bugzilla::init_page(); + my $res = ($ret eq '-1' && $cgi_script ne 'editparams.cgi') ? $shutdown_app->(@_) : $app->(@_); + Bugzilla::_cleanup(); + return $res; + }; + + 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); + } + my $app = $map->to_app; +}; -- cgit v1.2.3-24-g4f1b