line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Narada; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
146546
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
225145
|
|
|
2
|
|
|
|
|
16
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
our $VERSION = 'v1.0.1'; |
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
2489
|
use MojoX::Log::Fast; |
|
2
|
|
|
|
|
74400
|
|
|
2
|
|
|
|
|
24
|
|
8
|
2
|
|
|
2
|
|
883
|
use Narada::Config qw( get_config get_config_line ); |
|
2
|
|
|
|
|
36878
|
|
|
2
|
|
|
|
|
9
|
|
9
|
2
|
|
|
2
|
|
2290
|
use Narada::Lock qw( unlock ); |
|
2
|
|
|
|
|
3971
|
|
|
2
|
|
|
|
|
7
|
|
10
|
2
|
|
|
2
|
|
3101
|
use Scalar::Util qw( weaken ); |
|
2
|
|
|
|
|
7
|
|
|
2
|
|
|
|
|
1320
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
my ($Log, $Ident); |
13
|
|
|
|
|
|
|
our $IN_CB = 0; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub register { |
17
|
0
|
|
|
0
|
1
|
|
my ($self, $app, $conf) = @_; |
18
|
|
|
|
|
|
|
|
19
|
0
|
|
|
|
|
|
$Log = MojoX::Log::Fast->new($conf->{log}); |
20
|
0
|
|
|
|
|
|
$Ident = $Log->ident(); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Replace default logger with Log::Fast. |
23
|
0
|
|
|
|
|
|
$app->log($Log); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Load Mojo-specific config files. |
26
|
0
|
0
|
|
|
|
|
if ($app->can('secrets')) { |
27
|
0
|
|
|
|
|
|
$app->secrets([split /\n/ms, get_config('cookie.secret')]); |
28
|
|
|
|
|
|
|
} else { |
29
|
0
|
|
|
|
|
|
$app->secret(get_config_line('cookie.secret')); |
30
|
|
|
|
|
|
|
} |
31
|
0
|
|
|
|
|
|
$app->config(hypnotoad => { |
32
|
|
|
|
|
|
|
listen => [split /\n/ms, get_config('hypnotoad/listen')], |
33
|
|
|
|
|
|
|
proxy => get_config_line('hypnotoad/proxy'), |
34
|
|
|
|
|
|
|
accepts => get_config_line('hypnotoad/accepts'), |
35
|
|
|
|
|
|
|
workers => get_config_line('hypnotoad/workers'), |
36
|
|
|
|
|
|
|
pid_file => 'var/hypnotoad.pid', |
37
|
|
|
|
|
|
|
}); |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
# * Fix url->path and url->base->path. |
40
|
|
|
|
|
|
|
# * Set correct ident while handler runs. |
41
|
|
|
|
|
|
|
# * unlock() if handler died. |
42
|
0
|
|
|
|
|
|
my $realbase = Mojo::Path->new( get_config_line('basepath') ) |
43
|
|
|
|
|
|
|
->trailing_slash(0) |
44
|
|
|
|
|
|
|
->leading_slash(1) |
45
|
|
|
|
|
|
|
->to_string; |
46
|
|
|
|
|
|
|
$app->hook(around_dispatch => sub { |
47
|
0
|
|
|
0
|
|
|
my ($next, $c) = @_; |
48
|
0
|
|
|
|
|
|
my $url = $c->req->url; |
49
|
0
|
|
|
|
|
|
my $base = $url->base->path; |
50
|
0
|
|
|
|
|
|
my $path = $url->path; |
51
|
0
|
0
|
0
|
|
|
|
if ($base eq q{} && $path =~ m{\A\Q$realbase\E(.*)\z}mso) { |
52
|
0
|
|
|
|
|
|
$path->parse($1); |
53
|
|
|
|
|
|
|
} |
54
|
0
|
|
|
|
|
|
$base->parse($realbase); |
55
|
0
|
|
|
|
|
|
$path->leading_slash(1); |
56
|
0
|
|
|
|
|
|
$Log->ident($url->path); |
57
|
0
|
0
|
|
|
|
|
my $err = eval { $next->(); 1 } ? undef : $@; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
unlock(); |
59
|
0
|
0
|
|
|
|
|
die $err if defined $err; ## no critic(RequireCarping) |
60
|
0
|
|
|
|
|
|
}); |
61
|
|
|
|
|
|
|
|
62
|
0
|
|
|
0
|
|
|
$app->helper(proxy => sub { return _proxy(0, @_) }); |
|
0
|
|
|
|
|
|
|
63
|
0
|
|
|
0
|
|
|
$app->helper(weak_proxy => sub { return _proxy(1, @_) }); |
|
0
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
return; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub _proxy { |
69
|
0
|
|
|
0
|
|
|
my ($is_weak, $this, $cb, @p) = @_; |
70
|
0
|
0
|
|
|
|
|
if ($is_weak) { |
71
|
0
|
|
|
|
|
|
weaken($this); |
72
|
|
|
|
|
|
|
} |
73
|
0
|
|
|
|
|
|
my $is_global_cb= ref $this eq 'Mojolicious::Controller'; |
74
|
0
|
0
|
|
|
|
|
my $ident = $is_global_cb ? $Ident : $Log->ident; |
75
|
0
|
|
|
|
|
|
my $__warn__ = $SIG{__WARN__}; |
76
|
|
|
|
|
|
|
return sub { |
77
|
0
|
0
|
|
0
|
|
|
return if !$this; |
78
|
0
|
|
|
|
|
|
my $cur_ident = $Log->ident($ident); |
79
|
0
|
|
|
|
|
|
local $SIG{__WARN__} = $__warn__; |
80
|
0
|
0
|
|
|
|
|
my $err = eval { local $IN_CB=1; $cb->($this, @p, @_); 1 } ? undef : $@; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
81
|
0
|
0
|
|
|
|
|
if (defined $err) { |
82
|
0
|
|
|
|
|
|
$Log->ident($ident); |
83
|
0
|
0
|
|
|
|
|
if (!$IN_CB) { |
84
|
0
|
|
|
|
|
|
unlock() |
85
|
|
|
|
|
|
|
} |
86
|
0
|
0
|
0
|
|
|
|
if ($is_global_cb || $is_weak) { |
87
|
0
|
|
|
|
|
|
die $err; ## no critic(RequireCarping) |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else { |
90
|
0
|
|
|
|
|
|
$this->reply->exception($err); |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
} |
93
|
0
|
|
|
|
|
|
$Log->ident($cur_ident); |
94
|
0
|
|
|
|
|
|
}; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
1; # Magic true value required at end of module |
99
|
|
|
|
|
|
|
__END__ |