blib/lib/Clustericious/App.pm

filter: Statementstmt 90.9% 110 / 121 Branchbran 68.1% 15 / 22 Conditioncond 62.5% 5 / 8 MC/DCmcdc 20.0% 1 / 5 Subroutinesub 92.8% 26 / 28 Podpod 100% 4 / 4 totaltotal 85.6% 161 / 188 CC 15 SCAR 11.5
CC
15
Cov
86%
startup
7.4
config
6.1
sanity_check
6.0
CRAP 3.1
« blib/lib/Clustericious/Controller.pm ↑ Summary blib/lib/auto/share/dist/Clustericious/tmpl/1.08/app/lib/APPCLASS.pm »
1 package Clustericious::App;
2  
3 45 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 45 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 45 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 45 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 45 ▶use MojoX::Log::Log4perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 45 ▶use Mojo::UserAgent;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 45 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 45 ▶use Clustericious::Log;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 45 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 45 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 45 ▶use Mojo::Base 'Mojolicious';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 45 ▶use File::Glob qw( bsd_glob );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 45 ▶use File::Path qw( mkpath );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 45 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 45 ▶use Clustericious;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 45 ▶use Clustericious::Controller;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 45 ▶use Clustericious::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 45 ▶use Clustericious::Commands;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 45 ▶use Path::Class::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # ABSTRACT: Clustericious app base class
24 45 our $VERSION = '1.29'; # VERSION
25  
26  
27 8 ▶has commands => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
28 8 my $commands = Clustericious::Commands->new(app => shift);
29 8 weaken $commands->{app};
30 8 return $commands;
31 45 };
32  
33  
34 49 ▶sub startup {
Subroutine startup: called CC=7 SCAR=20.0
Pod documented
35 49 my $self = shift;
36  
37 49 $self->init_logging();
38 49 $self->plugins->namespaces(['Clustericious::Plugin','Mojolicious::Plugin']);
39 49 $self->controller_class('Clustericious::Controller');
40 49 $self->renderer->classes(['Clustericious::App']);
41  
42 # this is questionable
43 49 my $home = $self->home;
44 49 $self->renderer->paths([ Path::Class::Dir->new($home)->subdir('templates')->stringify ]);
45  
46 49 $self->plugin('CommonRoutes');
47 49 $self->plugin('AutodataHandler');
48 49 $self->plugin('ClustericiousHelpers');
49  
50 49 @{ $self->static->paths } = (
51 Clustericious
52 ->_dist_dir
53 ->subdir(qw( www 1.08 ))
54 ->stringify
55 );
56  
57 49 my $config = $self->config;
58 49 my $auth_plugin;
59 49 ▶ if(my $auth_config = $config->plug_auth(default => '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auth_config = $config->plug_auth('default', '')) { } 7 42 49
60 7 $self->log->info("Loading auth plugin plug_auth");
61 7 my $name = 'plug_auth';
62 7 ▶ if(ref($auth_config) && $auth_config->{plugin})
Branch: 1 branch
BranchTrueFalseTotal
if (ref $auth_config and $auth_config->{"plugin"}) 1 6 7
Condition66%2/3
ref $auth_config and $auth_config->{"plugin"}
!ll&&!rl&&r
061
MC/DC50%1/2
ref $auth_config and $auth_config->{"plugin"}
ref $auth_config $auth_config->{"plugin"}
Truth table66%2/3
A and B
A= ref $auth_configB= $auth_config->{"plugin"}
ABresult
0X0
100
111
63 1 { $name = $auth_config->{plugin} }
64 7 $auth_plugin = $self->plugin($name, plug_auth => $auth_config);
65 } else {
66 42 $self->log->info("No auth configured");
67 }
68  
69 49 ▶ $self->startup_route_builder($auth_plugin) if $self->can('startup_route_builder');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("startup_route_builder") 34 15 49
70  
71 19 ▶ my $url = $config->url(default => '') or do {
Branch: 1 branch
BranchTrueFalseTotal
my $url = $config->url("default", "") or do { $self->log->warn("Configuration file should contain 'url'.") } 19 0 19
72 0 $self->log->warn("Configuration file should contain 'url'.");
73 };
74  
75 8 ▶ $self->hook( before_dispatch => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
76 8 ▶ Log::Log4perl::MDC->put(remote_ip => shift->tx->remote_address || 'unknown');
Condition50%1/2
(shift())->tx->remote_address || 'unknown'
l!l
80
MC/DC0%0/1
(shift())->tx->remote_address || 'unknown'
(shift())->tx->remote_address
Truth table50%1/2
A
A= (shift())->tx->remote_address
Aresult
11
00
77 19 });
78  
79 19 ▶ if ( my $cors_allowed_origins = $config->cors_allowed_origins( default => '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cors_allowed_origins = $config->cors_allowed_origins("default", "")) 0 19 19
80 $self->hook(
81 0 ▶ after_dispatch => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my $c = shift;
83 0 $c->res->headers->add( 'Access-Control-Allow-Origin' => $cors_allowed_origins );
84 0 $c->res->headers->add( 'Access-Control-Allow-Headers' => 'Authorization' );
85 }
86 0 );
87 }
88  
89 }
90  
91  
92 49 ▶sub init_logging {
Subroutine init_logging: called CC=2 SCAR=7.0
Pod documented
93 49 my $self = shift;
94 49 ▶ croak "cannot be called as class method" unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 49 49
95 49 my $logger = Clustericious::Log->init_logging($self);
96 49 $self->log( $logger );
97 }
98  
99  
100 sub _my_dist_data
101 40 ▶{
Subroutine _my_dist_data: called CC=1 SCAR=0.0
102 40 my $dir = bsd_glob '~/.local/share/Perl/dist/Clustericious';
103 40 mkpath $dir, 0, 0700;
104 40 $dir;
105 }
106  
107 sub config
108 171 ▶{
Subroutine config: called CC=6 SCAR=18.0
Pod documented
109 171 my($self, $what) = @_;
110  
111 171 ▶ unless($self->{_config})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_config"}) 49 122 171
112 {
113 49 my $config = $self->{_config} = eval { Clustericious::Config->new(ref $self) };
114 49 ▶ if(my $error = $@)
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $@) 1 48 49
115 {
116 1 $self->log->error("error loading config $error");
117 1 $config = $self->{_config} = Clustericious::Config->new({ clustericious_config_error => $error });
118 }
119  
120 49 ▶ $config->{url} //= Clustericious->_default_url(ref $self);
Condition66%2/3
$config->{"url"} //= "Clustericious"->_default_url(ref $self)
l!l&&r!l&&!r
28210
MC/DC0%0/2
$config->{"url"} //= "Clustericious"->_default_url(ref $self)
$config->{"url"} "Clustericious"->_default_url(ref $self)
Truth table66%2/3
A //= B
A= $config->{"url"}B= "Clustericious"->_default_url(ref $self)
ABresult
1X1
011
000
121
122 49 ▶ if(grep { $_ eq 'hypnotoad' } $config->start_mode(default => [ 'hypnotoad' ]) )
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq "hypnotoad";} $config->start_mode("default", ["hypnotoad"])) 49 0 49
123 {
124 my $hypnotoad = $config->hypnotoad(
125 40 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
126 40 my $url = Mojo::URL->new($config->{url});
127 {
128 40 pid_file => File::Spec->catfile( _my_dist_data(), 'hypnotoad-' . $url->port . '-' . $url->host . '.pid' ),
129 listen => [
130 $url->to_string,
131 ],
132 };
133 }
134 49 );
135 }
136 }
137  
138 171 my $config = $self->{_config};
139  
140 # Mojo uses $app->config('what');
141 # Clustericious usually uses $app->config->what;
142 171 ▶ $what ? $config->{$what} : $config;
Branch: 1 branch
BranchTrueFalseTotal
$what ? : 0 171 171
143 }
144  
145  
146 sub sanity_check
147 0 ▶{
Subroutine sanity_check: not called CC=2 SCAR=17.9
Pod documented
148 0 my($self) = @_;
149 0 my $sane = 1;
150
151 0 ▶ if(my $error = $self->config->clustericious_config_error(default => ''))
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->config->clustericious_config_error("default", "")) 0 0 0
152 {
153 0 say "error loading configuration: $error";
154 0 $sane = 0;
155 }
156
157 0 $sane;
158 }
159  
160 45 1;
161  
162 =pod
163  
164 =encoding UTF-8
165  
166 =head1 NAME
167  
168 Clustericious::App - Clustericious app base class
169  
170 =head1 VERSION
171  
172 version 1.29
173  
174 =head1 SYNOPSIS
175  
176 use Mojo::Base 'Clustericious::App';
177  
178 =head1 DESCRIPTION
179  
180 This class is the base class for all Clustericious applications. It
181 inherits everything from L<Mojolicious> and adds a few Clustericious
182 specific methods documented here.
183  
184 =head1 SUPER CLASS
185  
186 L<Mojolicious>
187  
188 =head1 ATTRIBUTES
189  
190 =head2 commands
191  
192 An instance of L<Clustericious::Commands> for use with this application.
193  
194 =head1 METHODS
195  
196 =head2 startup
197  
198 $app->startup;
199  
200 Adds the autodata_handler plugin, common routes,
201 and sets up logging for the client using log::log4perl.
202  
203 =head2 init_logging
204  
205 $app->init_logging;
206  
207 Initializing logging using ~/etc/log4perl.conf
208  
209 =head2 config
210  
211 my $config = $app->config;
212  
213 Returns the config (an instance of L<Clustericious::Config>) for the application.
214  
215 =head2 sanity_check
216  
217 my $ok = $app->sanity_check;
218  
219 This method is executed after C<startup>, but before the application
220 actually starts with the L<start|Clustericious::Command::start> command.
221 If it returns 1 then the configuration is considered sane and the
222 application will start. If it returns 0 then the configuration has
223 problems and start will be aborted with an appropriate message to the user
224 attempting start.
225  
226 By default this just checks that the application's configuration file
227 (usually located in ~/etc/MyApp.conf) is correctly formatted as either
228 YAML or JSON.
229  
230 You can override this in your application, but don't forget to call
231 the base class's version of sanity_check before making your own checks.
232  
233 =head1 SEE ALSO
234  
235 L<Clustericious>
236  
237 =head1 AUTHOR
238  
239 Original author: Brian Duggan
240  
241 Current maintainer: Graham Ollis E<lt>plicease@cpan.orgE<gt>
242  
243 Contributors:
244  
245 Curt Tilmes
246  
247 Yanick Champoux
248  
249 =head1 COPYRIGHT AND LICENSE
250  
251 This software is copyright (c) 2013 by NASA GSFC.
252  
253 This is free software; you can redistribute it and/or modify it under
254 the same terms as the Perl 5 programming language system itself.
255  
256 =cut
257  
258 __DATA__
« blib/lib/Clustericious/Controller.pm ↑ Summary blib/lib/auto/share/dist/Clustericious/tmpl/1.08/app/lib/APPCLASS.pm »