lib/Dancer2/Core/Runner.pm

filter: Statementstmt 87.9% 95 / 108 Branchbran 76.6% 23 / 30 Conditioncond 84.6% 11 / 13 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 88.0% 22 / 25 Podpod 0.0% 0 / 7 totaltotal 82.1% 156 / 190 CC 24 SCAR 14.7
CC
24
Cov
85%
print_banner
10.3
_build_config
8.1
psgi_app
7.0
CRAP 4.4
« lib/Dancer2/Core/DSL.pm ↑ Summary lib/Dancer2/Core/Cookie.pm »
1 package Dancer2::Core::Runner;
2 # ABSTRACT: Top-layer class to start a dancer app
3 178 $Dancer2::Core::Runner::VERSION = '2.3.0';
4 178 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 178 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 178 ▶use Module::Runtime 'require_module';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 178 ▶use Dancer2::Core::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 178 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 178 ▶use Dancer2::Core::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 178 ▶use Plack::Builder qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 178 ▶use Ref::Util qw< is_ref is_regexpref >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 # Hashref of configurable items for the runner.
14 # Defaults come from ENV vars. Updated via global triggers
15 # from app configs.
16 178 has config => (
17 is => 'ro',
18 isa => HashRef,
19 lazy => 1,
20 builder => '_build_config',
21 );
22  
23 has mime_type => (
24 is => 'ro',
25 isa => InstanceOf ['Dancer2::Core::MIME'],
26 182 ▶ default => sub { Dancer2::Core::MIME->new(); },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 178 );
28  
29 178 has server => (
30 is => 'ro',
31 isa => InstanceOf['HTTP::Server::PSGI'],
32 lazy => 1,
33 builder => '_build_server',
34 handles => ['run'],
35 );
36  
37 has apps => (
38 is => 'ro',
39 isa => ArrayRef,
40 182 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 178 );
42  
43 has postponed_hooks => (
44 is => 'ro',
45 isa => HashRef,
46 182 ▶ default => sub { +{} },
Subroutine __ANON__: called CC=1 SCAR=0.0
47 178 );
48  
49 has environment => (
50 is => 'ro',
51 isa => Str,
52 required => 1,
53 182 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
54 182 ▶ $ENV{DANCER_ENVIRONMENT} || $ENV{PLACK_ENV} || 'development'
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"DANCER_ENVIRONMENT"} or $ENV{"PLACK_ENV"} or "development" 4 178 182
Condition100%3/3
$ENV{"DANCER_ENVIRONMENT"} or $ENV{"PLACK_ENV"}
l!l&&r!l&&!r
22178
MC/DC100%2/2
$ENV{"DANCER_ENVIRONMENT"} or $ENV{"PLACK_ENV"}
$ENV{"DANCER_ENVIRONMENT"} $ENV{"PLACK_ENV"}
Truth table100%3/3
A or B
A= $ENV{"DANCER_ENVIRONMENT"}B= $ENV{"PLACK_ENV"}
ABresult
1X1
011
000
55 },
56 178 );
57  
58 has host => (
59 is => 'ro',
60 lazy => 1,
61 0 ▶ default => sub { $_[0]->config->{'host'} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 178 );
63  
64 has port => (
65 is => 'ro',
66 lazy => 1,
67 0 ▶ default => sub { $_[0]->config->{'port'} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 178 );
69  
70 has timeout => (
71 is => 'ro',
72 lazy => 1,
73 0 ▶ default => sub { $_[0]->config->{'timeout'} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
74 178 );
75  
76 1 ▶sub _build_server {
Subroutine _build_server: called CC=2 SCAR=7.0
77 1 my $self = shift;
78  
79 1 require_module('HTTP::Server::PSGI');
80 1 my %args = (
81 host => $self->host,
82 port => $self->port,
83 timeout => $self->timeout,
84 );
85  
86 1 ▶ if ( !$self->config->{'no_server_tokens'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->config->{"no_server_tokens"}) 1 0 1
87 1 $args{'server_software'} = "Perl Dancer2 " . Dancer2->VERSION;
88 }
89  
90 1 return HTTP::Server::PSGI->new(%args);
91 }
92  
93 182 ▶sub _build_config {
Subroutine _build_config: called CC=8 SCAR=20.9
94 182 my $self = shift;
95  
96 $ENV{PLACK_ENV}
97 182 ▶ and $ENV{DANCER_APPHANDLER} = 'PSGI';
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"PLACK_ENV"} and $ENV{"DANCER_APPHANDLER"} = "PSGI" 2 180 182
98  
99 return {
100 behind_proxy => 0,
101 apphandler => ( $ENV{DANCER_APPHANDLER} || 'Standalone' ),
102 traces => ( $ENV{DANCER_TRACES} || 0 ),
103 host => ( $ENV{DANCER_SERVER} || '0.0.0.0' ),
104 port => ( $ENV{DANCER_PORT} || '3000' ),
105 no_server_tokens => ( defined $ENV{DANCER_NO_SERVER_TOKENS} ?
106 $ENV{DANCER_NO_SERVER_TOKENS} :
107 0 ),
108 startup_info => ( defined $ENV{DANCER_STARTUP_INFO} ?
109 $ENV{DANCER_STARTUP_INFO} :
110 182 ▶ 1 ),
Branch: 2 branches
BranchTrueFalseTotal
defined $ENV{'DANCER_NO_SERVER_TOKENS'} ? : 2 180 182
defined $ENV{'DANCER_STARTUP_INFO'} ? : 1 181 182
Condition75%6/8
$ENV{'DANCER_APPHANDLER'} || 'Standalone'
l!l
10172
$ENV{'DANCER_TRACES'} || 0
l!l
1181
$ENV{'DANCER_SERVER'} || '0.0.0.0'
l!l
0182
$ENV{'DANCER_PORT'} || '3000'
l!l
0182
MC/DC100%1/1
$ENV{'DANCER_APPHANDLER'} || 'Standalone'
$ENV{'DANCER_APPHANDLER'}
MC/DC100%1/1
$ENV{'DANCER_TRACES'} || 0
$ENV{'DANCER_TRACES'}
MC/DC0%0/1
$ENV{'DANCER_SERVER'} || '0.0.0.0'
$ENV{'DANCER_SERVER'}
MC/DC0%0/1
$ENV{'DANCER_PORT'} || '3000'
$ENV{'DANCER_PORT'}
Truth table100%2/2
A
A= $ENV{'DANCER_APPHANDLER'}
Aresult
11
00
Truth table100%2/2
A
A= $ENV{'DANCER_TRACES'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'DANCER_SERVER'}
Aresult
11
00
Truth table50%1/2
A
A= $ENV{'DANCER_PORT'}
Aresult
11
00
111 };
112 }
113  
114 182 ▶sub BUILD {
Subroutine BUILD: called CC=4 SCAR=13.9
Pod undocumented
115 182 my $self = shift;
116  
117 # Enable traces if set by ENV var.
118 182 ▶ if (my $traces = $self->config->{traces} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $traces = $self->config->{"traces"}) 1 181 182
119 1 require_module('Carp');
120 1 ▶ $Carp::Verbose = $traces ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$traces ? : 1 0 1
121 };
122  
123 # set the global runner object if one doesn't exist yet
124 # this can happen if you create one without going through Dancer2
125 # which doesn't trigger the import that creates it
126 182 ▶ defined $Dancer2::runner
Branch: 1 branch
BranchTrueFalseTotal
defined $Dancer2::runner or $Dancer2::runner = $self 17 165 182
127 or $Dancer2::runner = $self;
128 }
129  
130 251 ▶sub register_application {
Subroutine register_application: called CC=1 SCAR=0.0
Pod undocumented
131 251 my $self = shift;
132 251 my $app = shift;
133  
134 251 push @{ $self->apps }, $app;
135  
136 # add postponed hooks to our psgi app
137 251 $self->add_postponed_hooks( $app->name, $app->postponed_hooks );
138 }
139  
140 251 ▶sub add_postponed_hooks {
Subroutine add_postponed_hooks: called CC=1 SCAR=0.0
Pod undocumented
141 251 my $self = shift;
142 251 my $name = shift;
143 251 my $hooks = shift;
144  
145 # merge postponed hooks
146 251 @{ $self->{'postponed_hooks'}{$name} }{ keys %{$hooks} } = values %{$hooks};
147 }
148  
149 # decide what to start
150 # do we just return a PSGI app
151 # or do we actually start a development standalone server?
152 2 ▶sub start {
Subroutine start: called CC=2 SCAR=7.6
Pod undocumented
153 2 my $self = shift;
154 2 my $app = $self->psgi_app;
155  
156 # we decide whether we return a PSGI coderef
157 # or spin a local development PSGI server
158 2 ▶ $self->config->{'apphandler'} eq 'PSGI'
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{"apphandler"} eq "PSGI" and return $app 2 0 2
159 and return $app;
160  
161 0 $self->start_server($app);
162 }
163  
164 1 ▶sub start_server {
Subroutine start_server: called CC=1 SCAR=0.0
Pod undocumented
165 1 my $self = shift;
166 1 my $app = shift;
167  
168 # does not return
169 1 $self->print_banner;
170 1 $self->server->run($app);
171 }
172  
173 42 ▶sub psgi_app {
Subroutine psgi_app: called CC=7 SCAR=19.5
Pod undocumented
174 42 my ($self, $apps) = @_;
175  
176 42 my @found_apps;
177 42 ▶ foreach my $app_req ( @{ $apps || [] } ) {
Condition100%2/2
$apps || []
l!l
1725
MC/DC100%1/1
$apps || []
$apps
Truth table100%2/2
A
A= $apps
Aresult
11
00
178 30 ▶ if ( is_regexpref($app_req) ) {
Branch: 3 branches
BranchTrueFalseTotal
if (is_regexpref($app_req)) { } 6 24 30
elsif (ref $app_req eq 'Dancer2::Core::App') { } 4 20 24
elsif (not is_ref($app_req)) { } 20 0 20
179 # Asked to find app via regex pattern on its name
180 push @found_apps,
181 6 grep +( $_->name =~ $app_req ), @{ $self->apps };
182 } elsif ( ref $app_req eq 'Dancer2::Core::App' ) {
183 # Given Dancer2 App instance
184 4 push @found_apps, $app_req;
185 } elsif ( !is_ref($app_req) ) {
186 # Strings of the app names
187 push @found_apps,
188 20 grep +( $_->name eq $app_req ), @{ $self->apps };
189 } else {
190 0 croak "Invalid input to psgi_app: $app_req";
191 }
192 }
193  
194 # if specific apps, dispatch to them
195 # otherwise, dispatch over all apps by default
196 42 ▶ $apps = @found_apps ? \@found_apps : $self->apps;
Branch: 1 branch
BranchTrueFalseTotal
@found_apps ? : 17 25 42
197  
198 42 my $dispatcher = Dancer2::Core::Dispatcher->new( apps => $apps );
199  
200 # initialize psgi_apps
201 # (calls ->finish on the apps and create their PSGI apps)
202 # the dispatcher caches that in the attribute
203 # so ->finish isn't actually called again if you run this method
204 42 $dispatcher->apps_psgi;
205  
206 98 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
207 98 my $env = shift;
208  
209 # mark it as an old-style dispatching
210 98 $self->{'internal_dispatch'} = 1;
211  
212 98 my $response = $dispatcher->dispatch($env);
213  
214 # unmark it
215 98 delete $self->{'internal_dispatch'};
216  
217 # cleanup
218 98 delete $self->{'internal_sessions'};
219  
220 98 return $response;
221 42 };
222 }
223  
224 1 ▶sub print_banner {
Subroutine print_banner: called CC=4 SCAR=23.3
Pod undocumented
225 1 my $self = shift;
226 1 my $pid = $$;
227  
228 # we only print the info if we need to
229 1 ▶ $self->config->{'startup_info'} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->config->{"startup_info"} or return 0 1 1
230  
231 # bare minimum
232 0 print STDERR ">> Dancer2 v" . Dancer2->VERSION . " server $pid listening "
233 . 'on http://'
234 . $self->host . ':'
235 . $self->port . "\n";
236  
237 # all loaded plugins
238 0 foreach my $module ( grep { $_ =~ m{^Dancer2/Plugin/} } keys %INC ) {
239 0 $module =~ s{/}{::}g; # change / to ::
240 0 $module =~ s{\.pm$}{}; # remove .pm at the end
241 0 my $version = $module->VERSION;
242  
243 0 ▶ defined $version or $version = 'no version number defined';
Branch: 1 branch
BranchTrueFalseTotal
defined $version or $version = "no version number defined" 0 0 0
244 0 print STDERR ">> $module ($version)\n";
245 }
246 }
247  
248 178 1;
249  
250 __END__
« lib/Dancer2/Core/DSL.pm ↑ Summary lib/Dancer2/Core/Cookie.pm »