forked from metabrainz/musicbrainz-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.psgi
102 lines (87 loc) · 3.22 KB
/
app.psgi
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
#!/usr/bin/env perl
use strict;
use warnings;
use DBDefs;
use Moose::Util qw( ensure_all_roles );
use Plack::Builder;
BEGIN {
# We've hit some pathological cases where different JSON backends are
# used on different systems (e.g. JSON::PP instead of JSON::XS, due to the
# latter not being installed despite existing in the Makefile), which
# then breaks the code in some opaque way (e.g. JSON::PP doesn't assign a
# ($) prototype to decode_json).
#
# The effect of the environment variable here is to force the use of
# JSON::XS as the JSON backend, and die if it's not available.
$ENV{PERL_JSON_BACKEND} = 2;
}
BEGIN {
if (DBDefs->CATALYST_DEBUG) {
require Plack::Middleware::Debug::DAOLogger;
require Plack::Middleware::Debug::ExclusiveTime;
require Plack::Middleware::Debug::TemplateToolkit;
}
}
# Has to come before requiring MusicBrainz::Server
BEGIN {
use Template;
if (DBDefs->CATALYST_DEBUG) {
$Template::Config::CONTEXT = 'My::Template::Context';
$INC{'My/Template/Context.pm'} = 1;
}
}
use MusicBrainz::Server;
BEGIN {
if (DBDefs->FORK_RENDERER) {
if (my $child = fork) {
use POSIX;
my $action = POSIX::SigAction->new(sub {
kill 'TERM', $child;
exit;
});
$action->safe(1);
POSIX::sigaction(SIGTERM, $action);
} else {
exec './script/start_renderer.pl',
'--socket', DBDefs->RENDERER_SOCKET;
}
}
}
debug_method_calls() if DBDefs->CATALYST_DEBUG;
builder {
if (DBDefs->CATALYST_DEBUG) {
enable 'Debug', panels => [ qw( Memory Session Timer DAOLogger ExclusiveTime TemplateToolkit Parameters ) ];
}
if ($ENV{'MUSICBRAINZ_USE_PROXY'}) {
enable 'Plack::Middleware::ReverseProxy';
}
enable 'Static', path => qr{^/(static/|browserconfig\.xml|favicon\.ico$)}, root => 'root';
# AccessLog is a default middleware when PLACK_ENV=development
# (the default) [1], but we also want it enabled in production to
# aid debugging. The conditional ensures it's not enabled twice in
# development -- only in production when PLACK_ENV=deployment.
#
# [1] https://metacpan.org/pod/plackup#-E,-env,-the-PLACK_ENV-environment-variable.
enable_if {
my $plack_env = $ENV{PLACK_ENV};
defined $plack_env && $plack_env eq 'deployment'
} 'Plack::Middleware::AccessLog',
format => '%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i" %{X-Runtime}o';
enable 'Plack::Middleware::Runtime';
MusicBrainz::Server->psgi_app;
};
sub debug_method_calls {
for my $model (sort MusicBrainz::Server->models) {
my $m = MusicBrainz::Server->model($model);
$m->meta->make_mutable;
for my $method ($m->meta->get_all_methods) {
next if uc($method->name) eq $method->name;
next if $method->name =~ /^(new|does|can|c|sql|dbh)$/;
unless ($method->name =~ /^_/) {
Plack::Middleware::Debug::DAOLogger::install_logging($m->meta, $method);
}
Plack::Middleware::Debug::ExclusiveTime::install_timing($m->meta, $method);
}
$m->meta->make_immutable;
}
}