summaryrefslogtreecommitdiffstats
path: root/app.psgi
blob: 1da6d43510138f6c30bbc999684cf318ff24a659 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/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;

BEGIN { $main::BUGZILLA_PERSISTENT = 1 }

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

# This loads most of our modules.
use Bugzilla::PSGI qw(compile_cgi);
use Bugzilla::Logging;
use Bugzilla ();
use Bugzilla::Constants ();
use Bugzilla::BugMail ();
use Bugzilla::CGI ();
use Bugzilla::Extension ();
use Bugzilla::Install::Requirements ();
use Bugzilla::Util ();
use Bugzilla::RNG ();

use Plack;
use Plack::Builder;
use Plack::App::URLMap;
use Plack::App::WrapCGI;
use Plack::Response;

use constant STATIC => qw(
    data/webdot
    docs
    extensions/[^/]+/web
    graphs
    images
    js
    skins
);

# Pre-load all extensions
Bugzilla::Extension->load_all();
Bugzilla->preload_features();

# Force instantiation of template so Bugzilla::Template::PreloadProvider can do its magic.
Bugzilla->template;

my $ses_index = builder {
    my $auth_user = Bugzilla->localconfig->{ses_username};
    my $auth_pass = Bugzilla->localconfig->{ses_password};
    enable "Auth::Basic", authenticator => sub {
        my ($username, $password, $env) = @_;
        return (   $auth_user
                && $auth_pass
                && $username
                && $password
                && $username eq $auth_user
                && $password eq $auth_pass );
    };
    compile_cgi("ses/index.cgi");
};

my $bugzilla_app = builder {
    my $static_paths = join( '|', STATIC );

    enable 'Log4perl', category => 'Plack';

    enable 'Static',
        path => sub { s{^/(?:static/v\d+\.\d+/)?($static_paths)/}{$1/}gs },
        root => Bugzilla::Constants::bz_locations->{cgi_path};

    my @scripts = glob('*.cgi');

    my %mount;

    foreach my $script (@scripts) {
        my $name = basename($script);
        $mount{$name} = compile_cgi($script);
    }

    $mount{'ses/index.cgi'} = $ses_index;

    Bugzilla::Hook::process('psgi_builder', { mount => \%mount });

    foreach my $name ( keys %mount ) {
        mount "/$name" => $mount{$name};
    }

    # 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 '/robots.txt' => $mount{'robots.cgi'};
    mount '/rest' => $mount{'rest.cgi'};

};

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

return $bugzilla_app;