Kelp/Module/Symbiosis.pm

filter: Statementstmt 92.3% 72 / 78 Branchbran 67.8% 19 / 28 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 88.8% 16 / 18 Podpod 100% 3 / 3 totaltotal 82.8% 116 / 140 SCAR 29.6
CC
18
Cov
84%
run
6.1
__ANON__
6.0
build
5.1
CRAP 19.3
↑ Summary Kelp/Module/Symbiosis/_Util.pm »
1 package Kelp::Module::Symbiosis;
2 6 $Kelp::Module::Symbiosis::VERSION = '1.14';
3 6 ▶use Kelp::Base qw(Kelp::Module);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Plack::App::URLMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Scalar::Util qw(blessed refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Plack::Middleware::Conditional;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 ▶use Kelp::Module::Symbiosis::_Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 6 ▶attr -mounted => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
12 5 ▶attr -loaded => sub { {} };
Subroutine __ANON__: called CC=1 SCAR=0.0
13 6 ▶attr -middleware => sub { [] };
Subroutine __ANON__: called CC=1 SCAR=0.0
14 6 attr reverse_proxy => 0;
15  
16 sub mount
17 {
18 16 ▶ my ($self, $path, $app) = @_;
Subroutine mount: called CC=5 SCAR=16.3
Pod documented
19 16 my $mounted = $self->mounted;
20  
21 16 ▶ if (!ref $app && $app) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $app and $app) 2 14 16
Condition66%2/3
not ref $app and $app
!ll&&!rl&&r
1402
MC/DC50%1/2
not ref $app and $app
not ref $app $app
Truth table66%2/3
not(A) and B
A= not ref $appB= $app
ABresult
0X0
100
111
22 2 my $loaded = $self->loaded;
23 croak "Symbiosis: cannot mount $app, because no such name was loaded"
24 2 ▶ unless $loaded->{$app};
Branch: 1 branch
BranchTrueFalseTotal
unless $loaded->{$app} 0 2 2
25 2 $app = $loaded->{$app};
26 }
27  
28 carp "Symbiosis: overriding mounting point $path"
29 16 ▶ if exists $mounted->{$path};
Branch: 1 branch
BranchTrueFalseTotal
if exists $mounted->{$path} 0 16 16
30 16 $mounted->{$path} = $app;
31 16 return scalar keys %{$mounted};
32 }
33  
34 sub _link
35 {
36 8 ▶ my ($self, $name, $app, $mount) = @_;
Subroutine _link: called CC=3 SCAR=11.0
37 8 my $loaded = $self->loaded;
38  
39 warn "Symbiosis: overriding module name $name"
40 8 ▶ if exists $loaded->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $loaded->{$name} 0 8 8
41 8 $loaded->{$name} = $app;
42  
43 8 ▶ if ($mount) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mount) 1 7 8
44 1 $self->mount($mount, $app);
45 }
46 8 return scalar keys %{$loaded};
47 }
48  
49 sub run
50 {
51 20 ▶ my ($self) = shift;
Subroutine run: called CC=6 SCAR=18.1
Pod documented
52 20 my $psgi_apps = Plack::App::URLMap->new;
53 20 my %addrs; # apps keyed by refaddr
54  
55 20 my $error = "Symbiosis: cannot start the ecosystem because";
56 20 ▶ while (my ($path, $app) = each %{$self->mounted}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->mounted;}) 52 20 72
57 52 ▶ if (blessed $app) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($app)) { } 49 3 52
elsif (ref $app eq 'CODE') { } 3 0 3
58 49 ▶ croak "$error application mounted under $path cannot run()"
Branch: 1 branch
BranchTrueFalseTotal
unless $app->can("run") 0 49 49
59 unless $app->can("run");
60  
61 # cache the ran application so that it won't be ran twice
62 49 my $addr = refaddr $app;
63 49 ▶ my $ran = $addrs{$addr} //= $app->run(@_);
Condition66%2/3
$addrs{$addr} //= $app->run(@_)
l!l&&r!l&&!r
2470
MC/DC0%0/2
$addrs{$addr} //= $app->run(@_)
$addrs{$addr} $app->run(@_)
Truth table66%2/3
A //= B
A= $addrs{$addr}B= $app->run(@_)
ABresult
1X1
011
000
64  
65 49 $psgi_apps->map($path, $ran);
66 }
67 elsif (ref $app eq 'CODE') {
68 3 $psgi_apps->map($path, $app);
69 }
70 else {
71 0 croak "$error mount point $path is neither an object nor a coderef";
72 }
73 }
74  
75 20 my $wrapped = Kelp::Module::Symbiosis::_Util::wrap($self, $psgi_apps->to_app);
76 20 return $self->_reverse_proxy_wrap($wrapped);
77 }
78  
79 sub _reverse_proxy_wrap
80 {
81 20 ▶ my ($self, $app) = @_;
Subroutine _reverse_proxy_wrap: called CC=2 SCAR=12.2
82 20 ▶ return $app unless $self->reverse_proxy;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->reverse_proxy 20 0 20
83  
84 0 my $mw_class = Plack::Util::load_class('ReverseProxy', 'Plack::Middleware');
85 return Plack::Middleware::Conditional->wrap(
86 $app,
87 0 ▶ condition => sub { !$_[0]{REMOTE_ADDR} || $_[0]{REMOTE_ADDR} =~ m{127\.0\.0\.1} },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
not $_[0]{"REMOTE_ADDR"} or $_[0]{"REMOTE_ADDR"} =~ /127\.0\.0\.1/ 0 0 0
88 0 ▶ builder => sub { $mw_class->wrap($_[0]) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 );
90 }
91  
92 sub build
93 {
94 6 ▶ my ($self, %args) = @_;
Subroutine build: called CC=5 SCAR=16.4
Pod documented
95 $args{mount} //= '/'
96 6 ▶ unless exists $args{mount};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{"mount"} 1 5 6
Condition50%1/2
$args{"mount"} //= "/"
l!l
01
MC/DC0%0/1
$args{"mount"} //= "/"
$args{"mount"}
Truth table50%1/2
A
A= $args{"mount"}
Aresult
11
00
97  
98 6 ▶ if ($args{mount}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"mount"}) 2 4 6
99 2 $self->mount($args{mount}, $self->app);
100 }
101  
102 6 ▶ if ($args{reverse_proxy}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"reverse_proxy"}) 0 6 6
103 0 $self->reverse_proxy(1);
104 }
105  
106 6 Kelp::Module::Symbiosis::_Util::load_middleware($self, %args);
107  
108 $self->register(
109 symbiosis => $self,
110 20 ▶ run_all => sub { shift->symbiosis->run(@_); },
Subroutine __ANON__: called CC=1 SCAR=0.0
111 6 );
112  
113 }
114  
115 6 1;
116 __END__
117  
118 =head1 NAME
119  
120 Kelp::Module::Symbiosis - Manage an entire ecosystem of Plack organisms under Kelp
121  
122 =head1 SYNOPSIS
123  
124 # in configuration file
125 modules => [qw/Symbiosis SomeSymbioticModule/],
126 modules_init => {
127 Symbiosis => {
128 mount => '/kelp', # a path to mount Kelp main instance
129 },
130 SomeSymbioticModule => {
131 mount => '/elsewhere', # a path to mount SomeSymbioticModule
132 },
133 },
134  
135 # in kelp application - can be skipped if all mount paths are specified in config above
136 my $symbiosis = $kelp->symbiosis;
137 $symbiosis->mount('/app-path' => $kelp);
138 $symbiosis->mount('/other-path' => $kelp->module_method);
139 $symbiosis->mount('/other-path' => 'module_name'); # alternative - finds a module by name
140  
141 # in psgi script
142 my $app = KelpApp->new();
143 $app->run_all; # instead of run
144  
145 =head1 DESCRIPTION
146  
147 This module is an attempt to standardize the way many standalone Plack applications should be ran alongside the Kelp framework. The intended use is to introduce new "organisms" into symbiotic interaction by creating Kelp modules that are then attached onto Kelp. Then, the added method I<run_all> should be invoked in place of Kelp's I<run>, which will construct a L<Plack::App::URLMap> and return it as an application.
148  
149 =head2 Why not just use Plack::Builder in a .psgi script?
150  
151 One reason is not to put too much logic into .psgi script. It my opinion a framework should be capable enough not to make adding an additional application feel like a hack. This is of course subjective.
152  
153 The main functional reason to use this module is the ability to access the Kelp application instance inside another Plack application. If that application is configurable, it can be configured to call Kelp methods. This way, Kelp can become a glue for multiple standalone Plack applications, the central point of a Plack mixture:
154  
155 # in Symbiont's Kelp module (extends Kelp::Module::Symbiosis::Base)
156  
157 sub psgi {
158 my ($self) = @_;
159  
160 my $app = Some::Plack::App->new(
161 on_something => sub {
162 my $kelp = $self->app; # we can access Kelp!
163 $kelp->something_happened;
164 },
165 );
166  
167 return $app->to_app;
168 }
169  
170 # in Kelp application class
171  
172 sub something_happened {
173 ... # handle another app's signal
174 }
175  
176 =head2 What can be mounted?
177  
178 The sole requirement for a module to be mounted into Symbiosis is its ability to I<run()>, returning the psgi application. A module also needs to be a blessed reference, of course. Fun fact: Symbiosis module itself meets that requirements, so one symbiotic app can be mounted inside another.
179  
180 It can also be just a plain psgi app, which happens to be a code reference.
181  
182 Whichever it is, it should be a psgi application ready to be ran by the server, wrapped in all the needed middlewares. This is made easier with L<Kelp::Module::Symbiosis::Base>, which allows you to add symbionts in the configuration for Kelp along with the middlewares. Because of this, this should be a preferred way of defining symbionts.
183  
184 For very simple use cases, this will work though:
185  
186 # in application build method
187 my $some_app = SomePlackApp->new->to_app;
188 $self->symbiosis->mount('/path', $some_app);
189  
190 =head1 METHODS
191  
192 =head2 mount
193  
194 sig: mount($self, $path, $app)
195  
196 Adds a new $app to the ecosystem under $path. I<$app> can be:
197  
198 =over
199  
200 =item
201  
202 A blessed reference - will try to call run on it
203  
204 =item
205  
206 A code reference - will try calling it
207  
208 =item
209  
210 A string - will try finding a symbiotic module with that name and mounting it. See L<Kelp::Module::Symbiosis::Base/name>
211  
212 =back
213  
214 =head2 run
215  
216 Constructs and returns a new L<Plack::App::URLMap> with all the mounted modules and Kelp itself.
217  
218 Note: it will not run mounted object twice. This means that it is safe to mount something in two paths at once, and it will just be an alias to the same application.
219  
220 =head2 mounted
221  
222 sig: mounted($self)
223  
224 Returns a hashref containing a list of mounted modules, keyed by their specified mount paths.
225  
226 =head2 loaded
227  
228 sig: loaded($self)
229  
230 I<new in 1.10>
231  
232 Returns a hashref containing a list of loaded modules, keyed by their names.
233  
234 A module is loaded once it is added to Kelp configuration. This can be used to access a module that does not introduce new methods to Kelp.
235  
236 =head1 METHODS INTRODUCED TO KELP
237  
238 =head2 symbiosis
239  
240 Returns an instance of this class.
241  
242 =head2 run_all
243  
244 Shortcut method, same as C<< $kelp->symbiosis->run() >>.
245  
246 =head1 CONFIGURATION
247  
248 # Symbiosis MUST be specified as the first one
249 modules => [qw/Symbiosis Module::Some/],
250 modules_init => {
251 Symbiosis => {
252 mount => '/kelp',
253 },
254 'Module::Some' => {
255 mount => '/some',
256 ...
257 },
258 }
259  
260 Symbiosis should be the first of the symbiotic modules specified in your Kelp configuration. Failure to meet this requirement will cause your application to crash immediately.
261  
262 =head2 mount
263  
264 I<new in 1.10>
265  
266 A path to mount the Kelp instance, which defaults to I<'/'>. Specify a string if you wish a to use different path. Specify an I<undef> or empty string to avoid mounting at all - you will have to run something like C<< $kelp->symbiosis->mount($mount_path, $kelp); >> in Kelp's I<build> method.
267  
268 =head2 reverse_proxy
269  
270 I<new in 1.11>
271  
272 A boolean flag (I<1/0>) which enables reverse proxy for all the Plack apps at once. Requires L<Plack::Middleware::ReverseProxy> to be installed.
273  
274 =head2 middleware, middleware_init
275  
276 I<new in 1.12>
277  
278 Middleware specs for the entire ecosystem. Every application mounted in Symbiosis will be wrapped in these middleware. They are configured exactly the same as middlewares in Kelp. Regular Kelp middleware will be used just for the Kelp application, so if you want to wrap all symbionts at once, this is the place to do it.
279  
280 =head1 CAVEATS
281  
282 Routes specified in symbiosis will be matched before routes in Kelp. Once you mount something under I</api> for example, you will no longer be able to specify Kelp route for anything under I</api>.
283  
284 =head1 SEE ALSO
285  
286 =over 2
287  
288 =item * L<Kelp::Module::Symbiosis::Base>, a base for symbiotic modules
289  
290 =item * L<Kelp::Module::WebSocket::AnyEvent>, a reference symbiotic module
291  
292 =item * L<Plack::App::URLMap>, Plack URL mapper application
293  
294 =back
295  
296 =head1 AUTHOR
297  
298 Bartosz Jarzyna, E<lt>bbrtj.pro@gmail.comE<gt>
299  
300 =head1 COPYRIGHT AND LICENSE
301  
302 Copyright (C) 2020 - 2022 by Bartosz Jarzyna
303  
304 This library is free software; you can redistribute it and/or modify
305 it under the same terms as Perl itself.
306  
307  
308 =cut
309  
↑ Summary Kelp/Module/Symbiosis/_Util.pm »