summaryrefslogtreecommitdiffstats
path: root/app.psgi
blob: 5ac3cf14f7c3c790415d15af4de4ee423253a498 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/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.14.0;
use strict;
use warnings;

use File::Basename;
use File::Spec;

BEGIN {
    require lib;
    my $dir = File::Spec->rel2abs( dirname(__FILE__) );
    lib->import(
        $dir,
        File::Spec->catdir( $dir, "lib" ),
        File::Spec->catdir( $dir, qw(local lib perl5) )
    );

    # disable "use lib" from now on
    no warnings qw(redefine);
    *lib::import = sub { };
}

use Bugzilla::Constants ();

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

my $app = builder {
    my $static_paths
        = join( '|', sort { length $b <=> length $a || $a cmp $b } 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 );
    }
    $map->to_app;
};

unless (caller) {
    require Plack::Runner;
    my $runner = Plack::Runner->new;
    $runner->parse_options(@ARGV);
    $runner->run($app);
    exit 0;
}

return $app;