Microservice.pm

filter: Statementstmt 39.8% 59 / 148 Branchbran 0.0% 0 / 26 Conditioncond 5.5% 1 / 18 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 45.7% 16 / 35 Podpod 0.0% 0 / 5 totaltotal 31.2% 76 / 243 CC 21 SCAR 31.9
CC
21
Cov
31%
__ANON__
110.0
_build_router
20.0
__ANON__
12.0
CRAP 24.2
« Microservice/Time.pm ↑ Summary MicroserviceReq.pm »
1 package Async::Microservice;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.09';
9  
10 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 requires qw(get_routes service_name);
12  
13 2 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Path::Class qw(dir file);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use MooseX::Types::Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Path::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use Async::MicroserviceReq;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 has 'api_version' => (
23 is => 'ro',
24 isa => 'Int',
25 default => 1,
26 );
27 has 'api_prefix' => (
28 is => 'ro',
29 isa => 'Str',
30 lazy => 1,
31 0 ▶ default => sub { return '/v' . $_[0]->api_version; },
Subroutine __ANON__: not called CC=1 SCAR=6.9
32 2 );
33 2 has 'jsonp' => (
34 is => 'rw',
35 isa => 'Str',
36 default => '',
37 );
38 has 'static_dir' => (
39 is => 'ro',
40 isa => 'Path::Class::Dir',
41 required => 1,
42 coerce => 1,
43 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
44 0 my $static_dir = $ENV{STATIC_DIR};
45 0 ▶ die 'static dir "'
Branch: 1 branch
BranchTrueFalseTotal
unless $static_dir and -d $static_dir 0 0 0
Condition0%0/3
$static_dir and -d $static_dir
!ll&&!rl&&r
000
MC/DC0%0/2
$static_dir and -d $static_dir
$static_dir -d $static_dir
Truth table0%0/3
A and B
A= $static_dirB= -d $static_dir
ABresult
0X0
100
111
46 . $static_dir
47 . '" not found (check $ENV{STATIC_DIR})'
48 if !$static_dir || !-d $static_dir;
49 0 return $static_dir;
50 },
51 2 lazy => 1,
52 );
53 2 has 'static_path' => ( is => 'ro', isa => 'Str', default => 'static' );
54 has 'using_frontend_proxy' => (
55 is => 'ro',
56 isa => 'Bool',
57 3 ▶ default => sub { $ENV{USING_FRONTEND_PROXY} // 0 },
Subroutine __ANON__: called CC=2 SCAR=7.6
Condition50%1/2
$ENV{"USING_FRONTEND_PROXY"} // 0
l!l
03
MC/DC0%0/1
$ENV{"USING_FRONTEND_PROXY"} // 0
$ENV{"USING_FRONTEND_PROXY"}
Truth table50%1/2
A
A= $ENV{"USING_FRONTEND_PROXY"}
Aresult
11
00
58 2 );
59  
60 2 has 'router' => (
61 is => 'ro',
62 isa => 'Path::Router',
63 lazy => 1,
64 builder => '_build_router'
65 );
66 3 ▶has 'start_time' => ( is => 'ro', default => sub { time() } );
Subroutine __ANON__: called CC=1 SCAR=0.0
67 2 has 'req_count' => ( is => 'rw', default => 0 );
68 2 has 'pending_req' => ( is => 'rw', default => 0 );
69  
70 2 has 'request_timeout' => ( is => 'ro', isa => 'Num', default => 300 );
71 2 has 'max_concurrent_requests' => (
72 is => 'ro',
73 isa => 'Int',
74 default => 1000,
75 );
76  
77 2 has 'file_placeholder' => ( is => 'ro', default => 'ASYNC-SERVICE-NAME' );
78  
79 0 ▶sub _build_router {
Subroutine _build_router: not called CC=4 SCAR=30.0
80 0 my ($self) = @_;
81  
82 0 my $get_static_path = $self->static_path . '/:filename';
83 0 my $router = Path::Router->new;
84 0 my @default_routes = (
85 '' => { defaults => { GET => 'GET_root_index', }, },
86 $get_static_path => { defaults => { GET => 'GET_static', }, },
87 'edit' => { defaults => { GET => 'GET_root_edit', }, },
88 'hcheck' => { defaults => { GET => 'GET_hcheck', }, },
89 );
90 0 my @routes = ( $self->get_routes() );
91 0 ▶ while (@routes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@routes) 0 0 0
92 0 my ( $path, $opts ) = splice( @routes, 0, 2 );
93 0 $router->add_route( $path, %$opts );
94 }
95  
96 # skip default routes if they are already defined in get_routes
97 0 ▶ while (@default_routes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@default_routes) 0 0 0
98 0 my ( $path, $opts ) = splice( @default_routes, 0, 2 );
99 0 ▶ next if $router->match($path);
Branch: 1 branch
BranchTrueFalseTotal
if $router->match($path) 0 0 0
100 0 $router->add_route( $path, %$opts );
101 }
102  
103 0 return $router;
104 }
105  
106 0 ▶sub plack_handler {
Subroutine plack_handler: not called CC=2 SCAR=17.9
Pod undocumented
107 0 my ( $self, $env ) = @_;
108  
109 0 $self->req_count( $self->req_count + 1 );
110  
111 0 my $plack_req = Plack::Request->new($env);
112 my $this_req = Async::MicroserviceReq->new(
113 method => $plack_req->method,
114 headers => $plack_req->headers,
115 content => $plack_req->content,
116 path => $plack_req->path_info,
117 params => $plack_req->parameters,
118 static_dir => $self->static_dir,
119 jsonp => $self->jsonp,
120 using_frontend_proxy => $self->using_frontend_proxy,
121 server_host => $env->{SERVER_NAME},
122 server_port => $env->{SERVER_PORT},
123 server_scheme => $env->{'psgi.url_scheme'} || 'http',
124 pending_ref => \$self->{pending_req},
125 0 ▶ request_timeout => $self->request_timeout,
Condition0%0/2
$env->{'psgi.url_scheme'} || 'http'
l!l
00
MC/DC0%0/1
$env->{'psgi.url_scheme'} || 'http'
$env->{'psgi.url_scheme'}
Truth table0%0/2
A
A= $env->{'psgi.url_scheme'}
Aresult
11
00
126 );
127  
128 # set process name and last requested path for debug/troubleshooting
129 0 $0 = sprintf( "%s %s (pending_req: %d)",
130 $self->service_name, $this_req->path, $self->pending_req );
131  
132 0 ▶ my $plack_handler_sub = sub {
Subroutine __ANON__: not called CC=10 SCAR=47.0
133 0 my ($plack_respond) = @_;
134 0 $this_req->plack_respond($plack_respond);
135  
136 # limit number of pending requests
137 0 ▶ if ( $self->pending_req > $self->max_concurrent_requests ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->pending_req > $self->max_concurrent_requests) 0 0 0
138 0 $log->errorf(
139 'too many concurrent requests (%d), rejecting request for %s',
140 $self->pending_req, $this_req->path
141 );
142 0 return $this_req->respond( 429, [], 'too many requests' );
143 }
144  
145 # API prefix
146 0 my $sub_path_info;
147 0 my $api_prefix = quotemeta( $self->api_prefix );
148 0 ▶ if ( $this_req->path =~ qr{^$api_prefix(/.*)$} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_req->path =~ qr"^$api_prefix(/.*)$") 0 0 0
149 0 $sub_path_info = $1;
150 }
151  
152 # without configured API prefix redirect to the API root
153 0 ▶ return $this_req->redirect( $self->api_prefix . '/' )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sub_path_info 0 0 0
154 unless defined $sub_path_info;
155  
156 0 ▶ if ( my $match = $self->router->match($sub_path_info) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $match = $self->router->match($sub_path_info)) 0 0 0
157 0 my $func = $match->{mapping}->{ $this_req->method };
158 0 ▶ if ( $func && ( my $misc_fn = $self->can($func) ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($func and my $misc_fn = $self->can($func)) { } 0 0 0
Condition0%0/3
$func and my $misc_fn = $self->can($func)
!ll&&!rl&&r
000
MC/DC0%0/2
$func and my $misc_fn = $self->can($func)
$func my $misc_fn = $self->can($func)
Truth table0%0/3
A and B
A= $funcB= my $misc_fn = $self->can($func)
ABresult
0X0
100
111
159 0 %{ $this_req->params } =
160 0 ( %{ $this_req->params }, %{ $match->{mapping} } );
161 0 my $resp = $misc_fn->( $self, $this_req, $match );
162 0 ▶ if ( blessed($resp) && $resp->isa('Future') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($resp) and $resp->isa('Future')) { } 0 0 0
elsif (ref $resp eq 'ARRAY') { } 0 0 0
Condition0%0/3
builtin::blessed($resp) and $resp->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($resp) and $resp->isa("Future")
builtin::blessed($resp) $resp->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($resp)B= $resp->isa("Future")
ABresult
0X0
100
111
163 0 $resp->retain;
164 $resp->on_done(
165 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
166 0 my ($resp_data) = @_;
167 0 ▶ if ( ref($resp_data) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $resp_data eq 'ARRAY') { } 0 0 0
168 0 $this_req->respond(@$resp_data);
169 }
170 else {
171 0 $this_req->respond( 200, [], $resp_data );
172 }
173 }
174 0 );
175 $resp->on_fail(
176 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
177 0 my ($err_msg) = @_;
178 0 ▶ $err_msg ||= 'unknown';
Condition0%0/2
$err_msg ||= "unknown"
l!l
00
MC/DC0%0/1
$err_msg ||= "unknown"
$err_msg
Truth table0%0/2
A
A= $err_msg
Aresult
11
00
179 0 $log->errorf( 'exception while calling "%s": %s',
180 $plack_req->path_info, $err_msg );
181 0 $this_req->respond( 503, [],
182 'internal server error calling '
183 . $func . ': '
184 . $err_msg );
185 }
186 0 );
187 $resp->on_cancel(
188 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 $this_req->respond( 429, [],
190 'request for ' . $func . ' canceled' );
191 }
192 0 );
193 0 return $resp;
194 }
195 elsif ( ref($resp) eq 'ARRAY' ) {
196 0 $this_req->respond(@$resp);
197 }
198 0 return;
199 }
200 else {
201 # Route exists but method not supported
202 0 my @allowed = grep { defined $match->{mapping}->{$_} }
203 0 keys %{ $match->{mapping} };
204 0 return $this_req->respond(
205 405,
206 [ "Allow" => join( ", ", sort @allowed ) ],
207 'method not allowed'
208 );
209 }
210 }
211 0 return $this_req->respond( 404, [],
212 'path ' . $sub_path_info . ' not found' );
213 0 };
214  
215 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
216 0 my $respond = shift;
217 0 ▶ my $response = try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 $plack_handler_sub->($respond);
219 }
220 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
221 0 $this_req->respond( 503, [], 'internal server error: ' . $_ );
222 0 };
223 0 ▶ if ( blessed($response) && $response->isa('Future') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($response) and $response->isa('Future')) { } 0 0 0
Condition0%0/3
builtin::blessed($response) and $response->isa("Future")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($response) and $response->isa("Future")
builtin::blessed($response) $response->isa("Future")
Truth table0%0/3
A and B
A= builtin::blessed($response)B= $response->isa("Future")
ABresult
0X0
100
111
224 0 ▶ $response->on_done( sub { $this_req->clear_plack_respond } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 }
226 else {
227 0 $this_req->clear_plack_respond;
228 }
229 0 return $response;
230 0 };
231 }
232  
233 0 ▶sub _update_openapi_html {
Subroutine _update_openapi_html: not called CC=1 SCAR=6.9
234 0 my ( $self, $content ) = @_;
235 0 my $service_name = $self->service_name;
236 0 my $service_name_placeholder = $self->file_placeholder;
237 0 $content =~ s/$service_name_placeholder/$service_name/g;
238 0 return $content;
239 }
240  
241 0 ▶sub GET_root_index {
Subroutine GET_root_index: not called CC=1 SCAR=6.9
Pod undocumented
242 0 my ( $self, $this_req ) = @_;
243 return $this_req->static_ft( 'index.html',
244 0 ▶ sub { $self->_update_openapi_html(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
245 }
246  
247 0 ▶sub GET_static {
Subroutine GET_static: not called CC=1 SCAR=6.9
Pod undocumented
248 0 my ( $self, $this_req ) = @_;
249 0 my $filename = $this_req->params->{filename};
250 0 return $this_req->static_ft($filename);
251 }
252  
253 0 ▶sub GET_root_edit {
Subroutine GET_root_edit: not called CC=1 SCAR=6.9
Pod undocumented
254 0 my ( $self, $this_req ) = @_;
255 return $this_req->static_ft( 'edit.html',
256 0 ▶ sub { $self->_update_openapi_html(@_) } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
257 }
258  
259 0 ▶sub GET_hcheck {
Subroutine GET_hcheck: not called CC=1 SCAR=6.9
Pod undocumented
260 0 my ( $self, $this_req ) = @_;
261 0 return $this_req->text_plain(
262 'Service-Name: ' . $self->service_name,
263 "API-Version: " . $self->api_version,
264 'Uptime: ' . ( time() - $self->start_time ),
265 'Request-Count: ' . $self->req_count,
266 'Pending-Requests: ' . $self->pending_req,
267 );
268 }
269  
270 2 ▶no Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 2 1;
273  
274 __END__
« Microservice/Time.pm ↑ Summary MicroserviceReq.pm »