lib/OneTool/Daemon.pm

filter: Statementstmt 77.9% 53 / 68 Branchbran 25.0% 2 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 93.7% 15 / 16 Podpod 100% 2 / 2 totaltotal 72.7% 72 / 99 CC 6 SCAR 20.8
CC
6
Cov
70%
Listener
30.0
Log
2.0
BEGIN
1.0
CRAP 8.0
↑ Summary blib/lib/OneTool/App.pm »
1 package OneTool::Daemon;
2  
3 =head1 NAME
4  
5 OneTool::Daemon - OneTool Daemon module
6  
7 =cut
8  
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Daemon;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 #use HTTP::Daemon::SSL;
16 1 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Log::Log4perl::Level;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =head1 MOOSE OBJECT
27  
28 =cut
29  
30 1 has 'ip' => (
31 is => 'rw',
32 isa => 'Str',
33 required => 1,
34 );
35  
36 1 has 'port' => (
37 is => 'rw',
38 isa => 'Int',
39 required => 1,
40 );
41  
42 1 has 'api' => (
43 is => 'rw',
44 isa => 'HashRef',
45 required => 1,
46 );
47  
48 1 has 'logger' => (
49 is => 'rw',
50 isa => 'Log::Log4perl::Logger',
51 required => 1,
52 );
53  
54 =head1 SUBROUTINES/METHODS
55  
56 =head2 Listener()
57  
58 Listener for HTTP/HTTPS API requests
59  
60 =cut
61  
62 sub Listener
63 0 ▶{
Subroutine Listener: not called CC=5 SCAR=34.0
Pod documented
64 0 my $self = shift;
65  
66 my $daemon = HTTP::Daemon->new(
67 ReuseAddr => 1,
68 LocalAddr => $self->{ip},
69 LocalPort => $self->{port}
70 0 );
71  
72 =head2 comment
73 my $daemon = HTTP::Daemon::SSL->new(
74 ReuseAddr => 1, LocalAddr => $self->{ip}, LocalPort => $self->{port},
75 SSL_cert_file => "$FindBin::Bin/../conf/certs/server-cert.pem",
76 SSL_key_file => "$FindBin::Bin/../conf/certs/server-key.pem"
77 )
78 || die IO::Socket::SSL::errstr();
79 =cut
80  
81 0 my $json_header = HTTP::Headers->new('Content-Type' => 'application/json');
82 0 $self->Log('info', 'OneTool Daemon API listening on ' . $daemon->url);
83 0 ▶ while (my $connection = $daemon->accept)
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $daemon->accept) 0 0 0
84 {
85 0 ▶ while (my $request = $connection->get_request)
Branch: 1 branch
BranchTrueFalseTotal
if (my $request = $connection->get_request) 0 0 0
86 {
87 0 my ($method, $path, $params, $content) = (
88 $request->method, $request->uri->path,
89 $request->uri->query_form_hash,
90 $request->content
91 );
92 0 ▶ if ( (defined $self->{api}->{$path})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'api'}{$path} and $method eq $self->{'api'}{$path}{'method'}) { } 0 0 0
Condition0%0/3
defined $self->{"api"}{$path} and $method eq $self->{"api"}{$path}{"method"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"api"}{$path} and $method eq $self->{"api"}{$path}{"method"}
defined $self->{"api"}{$path} $method eq $self->{"api"}{$path}{"method"}
Truth table0%0/3
A and B
A= defined $self->{"api"}{$path}B= $method eq $self->{"api"}{$path}{"method"}
ABresult
0X0
100
111
93 && ($method eq $self->{api}->{$path}->{method}))
94 {
95 my $resp_content =
96 0 $self->{api}->{$path}->{action}($self, $params, $content);
97 0 my $resp =
98 HTTP::Response->new(200, 'OK', $json_header, $resp_content);
99 0 $connection->send_response($resp);
100 }
101 else
102 {
103 0 $connection->send_error(RC_FORBIDDEN);
104 }
105 }
106 0 $connection->close;
107 0 undef($connection);
108 }
109  
110 0 return (1);
111 }
112  
113 =head2 Log($str_level, $msg)
114  
115 Logs message $msg with loglevel $str_level
116  
117 =cut
118  
119 sub Log
120 2 ▶{
Subroutine Log: called CC=2 SCAR=6.9
Pod documented
121 2 my ($self, $str_level, $msg) = @_;
122  
123 2 ▶ return (undef) if ($str_level !~ /^(?:debug|info|warn|error)$/i);
Branch: 1 branch
BranchTrueFalseTotal
unless $str_level =~ /^(?:debug|info|warn|error)$/i 1 1 2
124  
125 1 my $level = Log::Log4perl::Level::to_priority(uc($str_level));
126 1 $self->{logger}->log($level, $msg);
127  
128 1 return ($msg);
129 }
130  
131 1 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 __PACKAGE__->meta->make_immutable;
133  
134 1 1;
135  
136 =head1 AUTHOR
137  
138 Sebastien Thebert <contact@onetool.pm>
139  
140 =cut
↑ Summary blib/lib/OneTool/App.pm »