summaryrefslogtreecommitdiffstats
path: root/Bugzilla/Quantum/Plugin/Glue.pm
blob: ef4a291f8490bcf6bb75651d1ac9c459841702bf (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
# 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.

package Bugzilla::Quantum::Plugin::Glue;
use Mojo::Base 'Mojolicious::Plugin';

use Try::Tiny;
use Bugzilla::Constants;
use Bugzilla::Quantum::Template;
use Bugzilla::Logging;
use Bugzilla::RNG ();
use JSON::MaybeXS qw(decode_json);

sub register {
    my ( $self, $app, $conf ) = @_;

    my %D;
    if ($ENV{BUGZILLA_HTTPD_ARGS}) {
        my $args = decode_json($ENV{BUGZILLA_HTTPD_ARGS});
        foreach my $arg (@$args) {
            if ($arg =~ /^-D(\w+)$/) {
                $D{$1} = 1;
            }
            else {
                die "Unknown httpd arg: $arg";
            }
        }
    }

    # hypnotoad is weird and doesn't look for MOJO_LISTEN itself.
    $app->config(
        hypnotoad => {
            listen => [ $ENV{MOJO_LISTEN} ],
        },
    );

    Mojo::IOLoop->next_tick(
        sub {
            Bugzilla::RNG::srand();
            srand();
            eval { Bugzilla->dbh->ping };
        }
    );

    $app->hook(
        around_dispatch => sub {
            my ($next, $c) = @_;

             if ($D{HTTPD_IN_SUBDIR}) {
                my $path = $c->req->url->path;
                $path =~ s{^/bmo}{}s;
                $c->req->url->path($path);
             }
             $next->();
        }
    );

    Bugzilla::Extension->load_all();
    if ($app->mode ne 'development') {
        Bugzilla->preload_features();
        DEBUG("preloading templates");
        Bugzilla->preload_templates();
        DEBUG("done preloading templates");
    }
    $app->secrets([Bugzilla->localconfig->{side_wide_secret}]);

    $app->renderer->add_handler(
        'bugzilla' => sub {
            my ( $renderer, $c, $output, $options ) = @_;
            my $vars = delete $c->stash->{vars};

            # Helpers
            my %helper;
            foreach my $method ( grep {m/^\w+\z/} keys %{ $renderer->helpers } ) {
                my $sub = $renderer->helpers->{$method};
                $helper{$method} = sub { $c->$sub(@_) };
            }
            $vars->{helper} = \%helper;

            # The controller
            $vars->{c} = $c;
            my $name = $options->{template};
            unless ($name =~ /\./) {
                $name = sprintf '%s.%s.tmpl', $options->{template}, $options->{format};
            }
            my $template = Bugzilla->template;
            $template->process( $name, $vars, $output )
                or die $template->error;
        }
    );

    $app->log(
        MojoX::Log::Log4perl::Tiny->new(
            logger => Log::Log4perl->get_logger(ref $app)
        )
    );
}




1;