blib/lib/Thunderhorse/App.pm

filter: Statementstmt 98.6% 151 / 153 Branchbran 75.0% 18 / 24 Conditioncond 75.0% 6 / 8 MC/DCmcdc 60.0% 3 / 5 Subroutinesub 100% 49 / 49 Podpod 87.5% 14 / 16 totaltotal 94.5% 241 / 255 CC 23 SCAR 7.5
CC
23
Cov
95%
run
5.5
pagi
4.0
render_error
4.0
CRAP 2.1
« blib/lib/Thunderhorse/AppController.pm ↑ Summary bin/thunderhorse »
1 package Thunderhorse::App;
2 24 $Thunderhorse::App::VERSION = '0.107';
3 24 ▶use v5.40;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 24 ▶use Mooish::Base -standard;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 24 ▶use Thunderhorse qw(pagi_loop);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 24 ▶use Thunderhorse::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use Gears qw(load_component get_component_name);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use Thunderhorse::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use Thunderhorse::Router;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Thunderhorse::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Thunderhorse::AppController;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use Path::Tiny (); # path attribute exists in this package
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 24 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 24 ▶use IO::Async::Loop;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 24 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 24 ▶use PAGI::Utils qw(handle_lifespan);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 24 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 24 extends 'Gears::App';
22  
23 has param 'path' => (
24 coerce => (InstanceOf ['Path::Tiny'])
25 ->plus_coercions(Str, q{ Path::Tiny::path($_) }),
26 41 ▶ default => sub { $FindBin::Bin },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 24 );
28  
29 has param 'env' => (
30 isa => Enum ['production', 'development', 'test'],
31 42 ▶ default => sub { $ENV{PAGI_ENV} // 'production' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{"PAGI_ENV"} // "production"
l!l
384
MC/DC100%1/1
$ENV{"PAGI_ENV"} // "production"
$ENV{"PAGI_ENV"}
Truth table100%2/2
A
A= $ENV{"PAGI_ENV"}
Aresult
11
00
32 24 );
33  
34 has param 'initial_config' => (
35 isa => Str | HashRef,
36 34 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
37 24 );
38  
39 has field 'loop' => (
40 isa => InstanceOf ['IO::Async::Loop'],
41 42 ▶ default => sub { IO::Async::Loop->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
42 24 );
43  
44 # the base app controller
45 24 has field 'controller' => (
46 isa => InstanceOf ['Thunderhorse::Controller'],
47 lazy => '_build_app_controller',
48 );
49  
50 has extended 'router' => (
51 reader => '_router',
52 isa => InstanceOf ['Thunderhorse::Router'],
53 42 ▶ default => sub { Thunderhorse::Router->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
54 24 );
55  
56 24 has extended 'config' => (
57 builder => 1,
58 );
59  
60 has field 'modules' => (
61 isa => ArrayRef,
62 42 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 24 );
64  
65 has field 'extra_methods' => (
66 isa => HashRef,
67 42 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 {
69 42 controller => {},
70 }
71 },
72 24 );
73  
74 has field 'extra_middleware' => (
75 isa => ArrayRef,
76 42 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
77 24 );
78  
79 has field 'extra_hooks' => (
80 isa => HashRef,
81 42 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
82 {
83 42 startup => [],
84 shutdown => [],
85 error => [],
86 }
87 },
88 24 );
89  
90 #############################
91 ### BOOTSTRAPPING SECTION ###
92 #############################
93  
94 42 ▶sub BUILD ($self, $)
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
95 {
96 42 $self->late_configure;
97 }
98  
99 42 ▶sub _build_config ($self)
Subroutine _build_config: called CC=3 SCAR=11.1
100 {
101 42 my $conf = Thunderhorse::Config->new;
102 42 foreach my $reader ($conf->readers->@*) {
103 42 ▶ next unless $reader isa 'Gears::Config::Reader::PerlScript';
Branch: 1 branch
BranchTrueFalseTotal
unless $reader isa "Gears::Config::Reader::PerlScript" 0 42 42
104  
105 # make app available from .pl config files
106 42 $reader->declared_vars->%* = (
107 $reader->declared_vars->%*,
108 app => $self,
109 );
110 }
111  
112 42 return $conf;
113 }
114  
115 40 ▶sub _build_app_controller ($self)
Subroutine _build_app_controller: called CC=1 SCAR=0.0
116 {
117 40 return $self->_build_controller('Thunderhorse::AppController');
118 }
119  
120 106 ▶sub _build_context ($self, @pagi)
Subroutine _build_context: called CC=1 SCAR=0.0
121 {
122 106 return Thunderhorse::Context->new(
123 app => $self,
124 pagi => \@pagi,
125 );
126 }
127  
128 13 ▶sub is_production ($self)
Subroutine is_production: called CC=1 SCAR=0.0
Pod documented
129 {
130 13 return $self->env eq 'production';
131 }
132  
133 129 ▶sub router ($self)
Subroutine router: called CC=1 SCAR=0.0
Pod documented
134 {
135 129 my $router = $self->_router;
136 129 $router->set_controller($self->controller);
137 129 return $router;
138 }
139  
140 42 ▶sub configure ($self)
Subroutine configure: called CC=3 SCAR=11.0
Pod documented
141 {
142 42 my $config = $self->config;
143  
144 42 my $preconf = $self->initial_config;
145 42 ▶ if (!ref $preconf) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $preconf) { } 5 37 42
146 5 $config->load_from_files($self->path->child($preconf), $self->env);
147 }
148 else {
149 37 $config->add(var => $preconf);
150 }
151  
152 # TODO: module dependencies and ordering
153 42 my %modules = $config->get('modules', {})->%*;
154 42 foreach my $module_name (sort keys %modules) {
155 9 $self->load_module($module_name, $modules{$module_name});
156 }
157 }
158  
159 42 ▶sub late_configure ($self)
Subroutine late_configure: called CC=2 SCAR=6.9
Pod undocumented
160 {
161 42 foreach my $controller ($self->config->get('controllers', [])->@*) {
162 6 $self->load_controller($controller);
163 }
164 }
165  
166 16 ▶sub load_module ($self, $module_class, $args = {})
Subroutine load_module: called CC=2 SCAR=6.9
Pod documented
167 16 {
168 16 my $module = load_component(get_component_name($module_class, 'Thunderhorse::Module'))
169 ->new(app => $self, config => $args);
170  
171 16 push $self->modules->@*, $module;
172 16 return $self;
173 }
174  
175 ###################################
176 ### PAGI IMPLEMENTATION SECTION ###
177 ###################################
178  
179 108 ▶async sub pagi ($self, $scope, $receive, $send)
Subroutine pagi: called CC=4 SCAR=13.9
Pod documented
180 108 {
181 108 my $scope_type = $scope->{type};
182  
183 return await handle_lifespan(
184 $scope, $receive, $send,
185 2 ▶ startup => sub { $self->_on_startup(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
186 2 ▶ shutdown => sub { $self->_on_shutdown(@_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
187 108 ▶ ) if $scope_type eq 'lifespan';
Branch: 1 branch
BranchTrueFalseTotal
if $scope_type eq "lifespan" 2 106 108
188  
189 # TODO: is this needed?
190 106 ▶ die 'Unsupported scope type'
Branch: 1 branch
BranchTrueFalseTotal
unless $scope_type =~ /^(http|sse|websocket)$/u 0 106 106
191 unless $scope_type =~ m/^(http|sse|websocket)$/;
192  
193 # copy scope since we are modifying it
194 106 $scope = {$scope->%*};
195 106 my $ctx = $scope->{thunderhorse} = $self->_build_context($scope, $receive, $send);
196 106 weaken $scope->{thunderhorse};
197  
198 # query router for matches
199 106 my $req = $ctx->req;
200 my $matches = $self->_router->match(
201 $req->path,
202 106 (lc join '.', grep { defined } $scope_type, $req->method),
203 );
204  
205 # run location handlers' nested structure
206 106 await pagi_loop($ctx, $matches->@*);
207  
208 # 404 is possible even if we had matches, as long as no handler consumed
209 # the context
210 99 ▶ if (!$ctx->is_consumed) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->is_consumed) 4 95 99
211 4 await $self->render_error(undef, $ctx, 404);
212 4 await $ctx->send_res;
213 }
214  
215 99 return;
216 24 }
217  
218 110 ▶sub run ($self)
Subroutine run: called CC=5 SCAR=17.0
Pod documented
219 {
220 # do not turn into PAGI application if run by a thunderhorse script
221 110 ▶ return $self if $ENV{THUNDERHORSE_SCRIPT};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"THUNDERHORSE_SCRIPT"} 2 108 110
222  
223 108 ▶ my $pagi = sub (@args) {
Subroutine __ANON__: called CC=1 SCAR=0.0
224 108 return $self->pagi(@args);
225 108 };
226  
227 108 foreach my $mw ($self->extra_middleware->@*) {
228 11 ▶ if (ref $mw eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $mw eq 'CODE') { } 0 11 11
elsif ($mw isa 'PAGI::Middleware') { } 11 0 11
229 0 $pagi = $mw->($pagi);
230 }
231 elsif ($mw isa 'PAGI::Middleware') {
232 11 $pagi = $mw->wrap($pagi);
233 }
234 else {
235 0 Gears::X::Thunderhorse->raise('bad middleware, not CODE or PAGI::Middleware');
236 }
237 }
238  
239 108 return $pagi;
240 }
241  
242 19 ▶async sub render_error ($self, $controller, $ctx, $code, $message = undef)
Subroutine render_error: called CC=4 SCAR=13.9
Pod documented
243 19 {
244 19 ▶ $message = defined $message && !$self->is_production ? $message : status_message($code);
Branch: 1 branch
BranchTrueFalseTotal
defined $message && !$self->is_production ? : 13 6 19
Condition100%3/3
defined $message and not $self->is_production
!ll&&!rl&&r
3313
MC/DC100%2/2
defined $message and not $self->is_production
defined $message not $self->is_production
Truth table100%3/3
A and not(B)
A= defined $messageB= not $self->is_production
ABresult
0X0
100
111
245 19 $ctx->empty_res->status($code)->text($message);
246 24 }
247  
248 54 ▶async sub render_response ($self, $controller, $ctx, $result)
Subroutine render_response: called CC=1 SCAR=0.0
Pod documented
249 54 {
250 54 $ctx->res->status_try(200)->html($result);
251 24 }
252  
253 #########################
254 ### EXTENSION METHODS ###
255 #########################
256  
257 12 ▶sub add_method ($self, $for, $name, $code)
Subroutine add_method: called CC=3 SCAR=11.3
Pod documented
258 {
259 12 my $area = $self->extra_methods->{$for};
260 12 ▶ Gears::X::Thunderhorse->raise("bad area '$for' for symbol '$name'")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $area 0 12 12
261 unless defined $area;
262  
263 Gears::X::Thunderhorse->raise("symbol '$name' already exists in area '$for'")
264 12 ▶ if exists $area->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $area->{$name} 0 12 12
265  
266 12 $area->{$name} = $code;
267 12 return $self;
268 }
269  
270 11 ▶sub add_middleware ($self, $mw)
Subroutine add_middleware: called CC=1 SCAR=0.0
Pod documented
271 {
272 11 push $self->extra_middleware->@*, $mw;
273 11 return $self;
274 }
275  
276 10 ▶sub add_hook ($self, $hook, $handler)
Subroutine add_hook: called CC=1 SCAR=0.0
Pod documented
277 {
278 10 push $self->extra_hooks->{$hook}->@*, $handler;
279 10 return $self;
280 }
281  
282 #####################
283 ### HOOKS SECTION ###
284 #####################
285  
286 23 ▶sub _fire_hooks ($self, $hook, @args)
Subroutine _fire_hooks: called CC=2 SCAR=6.9
287 {
288 23 foreach my $handler ($self->extra_hooks->{$hook}->@*) {
289 4 $handler->(@args);
290 }
291 }
292  
293 2 ▶sub _on_startup ($self, @args)
Subroutine _on_startup: called CC=1 SCAR=0.0
294 {
295 2 $self->_fire_hooks(startup => @args);
296 2 return $self->on_startup(@args);
297 }
298  
299 1 ▶async sub on_startup ($self, $state)
Subroutine on_startup: called CC=1 SCAR=0.0
Pod documented
300 1 {
301 24 }
302  
303 2 ▶sub _on_shutdown ($self, @args)
Subroutine _on_shutdown: called CC=1 SCAR=0.0
304 {
305 2 $self->_fire_hooks(shutdown => @args);
306 2 return $self->on_shutdown(@args);
307 }
308  
309 1 ▶async sub on_shutdown ($self, $state)
Subroutine on_shutdown: called CC=1 SCAR=0.0
Pod documented
310 1 {
311 24 }
312  
313 16 ▶async sub on_error ($self, $controller, $ctx, $error)
Subroutine on_error: called CC=3 SCAR=11.3
Pod documented
314 16 {
315 16 ▶ my $code = $error isa 'Gears::X::HTTP' ? $error->code : 500;
Branch: 1 branch
BranchTrueFalseTotal
$error isa 'Gears::X::HTTP' ? : 4 12 16
316 16 ▶ await +($controller // $self->controller)->render_error($ctx, $code, $error);
Condition33%1/3
$controller // $self->controller
l!l&&r!l&&!r
1600
MC/DC0%0/2
$controller // $self->controller
$controller $self->controller
Truth table33%1/3
A // B
A= $controllerB= $self->controller
ABresult
1X1
011
000
317 }
318  
319 __END__
« blib/lib/Thunderhorse/AppController.pm ↑ Summary bin/thunderhorse »