line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mackerel::Webhook::Receiver; |
2
|
3
|
|
|
3
|
|
80405
|
use 5.008001; |
|
3
|
|
|
|
|
11
|
|
3
|
3
|
|
|
3
|
|
13
|
use strict; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
60
|
|
4
|
3
|
|
|
3
|
|
14
|
use warnings; |
|
3
|
|
|
|
|
13
|
|
|
3
|
|
|
|
|
129
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.01"; |
7
|
|
|
|
|
|
|
|
8
|
3
|
|
|
3
|
|
1923
|
use JSON; |
|
3
|
|
|
|
|
26542
|
|
|
3
|
|
|
|
|
18
|
|
9
|
3
|
|
|
3
|
|
3100
|
use Plack::Request; |
|
3
|
|
|
|
|
205908
|
|
|
3
|
|
|
|
|
131
|
|
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
1812
|
use Mackerel::Webhook::Receiver::Event; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
121
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
14
|
3
|
|
|
|
|
12
|
new => 1, |
15
|
3
|
|
|
3
|
|
21
|
); |
|
3
|
|
|
|
|
5
|
|
16
|
12
|
|
100
|
12
|
0
|
144
|
sub events { shift->{events} ||= {} } |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub to_app { |
19
|
2
|
|
|
2
|
0
|
9
|
my $self = shift; |
20
|
|
|
|
|
|
|
sub { |
21
|
6
|
|
|
6
|
|
77345
|
my $env = shift; |
22
|
6
|
|
|
|
|
53
|
my $req = Plack::Request->new($env); |
23
|
6
|
50
|
|
|
|
68
|
if ($req->method ne 'POST') { |
24
|
0
|
|
|
|
|
0
|
return [400, [], ['BAD REQUEST']]; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
6
|
100
|
|
|
|
307
|
my $payload = eval { decode_json $req->content } |
|
6
|
|
|
|
|
26
|
|
28
|
|
|
|
|
|
|
or return [400, [], ['BAD REQUEST']]; |
29
|
|
|
|
|
|
|
|
30
|
4
|
|
|
|
|
15485
|
my $event_name = $payload->{event}; |
31
|
4
|
|
|
|
|
50
|
my $event = Mackerel::Webhook::Receiver::Event->new( |
32
|
|
|
|
|
|
|
payload => $payload, |
33
|
|
|
|
|
|
|
event => $event_name, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
4
|
50
|
|
|
|
46
|
if (my $code = $self->events->{''}) { |
37
|
4
|
|
|
|
|
21
|
$code->($event, $req); |
38
|
|
|
|
|
|
|
} |
39
|
4
|
100
|
|
|
|
20
|
if (my $code = $self->events->{$event_name}) { |
40
|
2
|
|
|
|
|
10
|
$code->($event, $req); |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
4
|
|
|
|
|
79
|
[200, [], ['OK']]; |
44
|
2
|
|
|
|
|
11
|
}; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub on { |
48
|
4
|
|
|
4
|
0
|
32
|
my $self = shift; |
49
|
4
|
|
|
|
|
8
|
my ($event, $code) = @_; |
50
|
4
|
100
|
|
|
|
13
|
if (ref $event eq 'CODE') { |
51
|
2
|
|
|
|
|
4
|
$code = $event; |
52
|
2
|
|
|
|
|
5
|
$event = ''; |
53
|
|
|
|
|
|
|
} |
54
|
4
|
|
|
|
|
10
|
$self->events->{$event} = $code; |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
sub run { |
58
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
59
|
0
|
0
|
|
|
|
|
my %opts = @_ == 1 ? %{$_[0]} : @_; |
|
0
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my %server; |
62
|
0
|
|
|
|
|
|
my $server = delete $opts{server}; |
63
|
0
|
0
|
|
|
|
|
$server{server} = $server if $server; |
64
|
|
|
|
|
|
|
|
65
|
0
|
|
|
|
|
|
my @options = %opts; |
66
|
0
|
|
|
|
|
|
require Plack::Runner; |
67
|
|
|
|
|
|
|
|
68
|
0
|
|
|
|
|
|
my $runner = Plack::Runner->new( |
69
|
|
|
|
|
|
|
%server, |
70
|
|
|
|
|
|
|
options => \@options, |
71
|
|
|
|
|
|
|
); |
72
|
0
|
|
|
|
|
|
$runner->run($self->to_app); |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
1; |
76
|
|
|
|
|
|
|
__END__ |