blib/lib/Lavoco/Web/App.pm

filter: Statementstmt 21.2% 56 / 264 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 44.1% 15 / 34 Podpod 100% 3 / 3 totaltotal 19.1% 74 / 386 SCAR 69.7
CC
42
Cov
17%
_init
240.0
__ANON__
132.0
_sitemap
72.0
CRAP 1068.3
↑ Summary
1 package Lavoco::Web::App;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Email::Stuffer;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use File::Slurp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Log::AutoDump;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Plack::Handler::FCGI;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 3 $Data::Dumper::Sortkeys = 1;
22  
23 =head1 NAME
24  
25 Lavoco::Web::App - Experimental framework with two constraints: FastCGI and Template::Toolkit.
26  
27 =head1 VERSION
28  
29 Version 0.05
30  
31 =cut
32  
33 3 our $VERSION = '0.05';
34  
35 3 $VERSION = eval $VERSION;
36  
37 =head1 SYNOPSIS
38  
39 Framework to run small web apps, URL dispatching based on a flexible config file, rendering Template::Toolkit templates, running as a FastCGI application.
40  
41 use Lavoco::Web::App;
42
43 my $app = Lavoco::Web::App->new;
44
45 my $action = lc( $ARGV[0] ); # (start|stop|restart)
46
47 $app->$action;
48  
49 =cut
50  
51 =head1 METHODS
52  
53 =head2 Class Methods
54  
55 =head3 new
56  
57 Creates a new instance of the web-app object.
58  
59 =head2 Attributes
60  
61 =cut
62  
63 3 has processes => ( is => 'rw', isa => 'Int', default => 5 );
64 3 has base => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_base' );
65 3 has dev => ( is => 'rw', isa => 'Bool', lazy => 1, builder => '_build_dev' );
66 3 has _pid => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__pid' );
67 3 has _socket => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build__socket' );
68 3 has templates => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_templates' );
69 3 has filename => ( is => 'rw', isa => 'Str', lazy => 1, builder => '_build_filename' );
70 3 has config => ( is => 'rw', isa => 'HashRef' );
71  
72 sub _build_base
73 {
74 0 ▶ return $Bin;
Subroutine _build_base: not called CC=1 SCAR=6.9
75 }
76  
77 sub _build_dev
78 {
79 0 ▶ my $self = shift;
Subroutine _build_dev: not called CC=2 SCAR=17.9
80  
81 0 ▶ return 0 if $self->base =~ m:/live:;
Branch: 1 branch
BranchTrueFalseTotal
if $self->base =~ m[/live] 0 0 0
82  
83 0 return 1;
84 }
85  
86 sub _build__pid
87 {
88 0 ▶ my $self = shift;
Subroutine _build__pid: not called CC=1 SCAR=6.9
89  
90 0 return $self->base . '/app.pid';
91 }
92  
93 sub _build__socket
94 {
95 0 ▶ my $self = shift;
Subroutine _build__socket: not called CC=1 SCAR=6.9
96  
97 0 return $self->base . '/app.sock';
98 }
99  
100 sub _build_templates
101 {
102 0 ▶ my $self = shift;
Subroutine _build_templates: not called CC=1 SCAR=6.9
103  
104 0 return $self->base . '/templates';
105 }
106  
107 sub _build_filename
108 {
109 0 ▶ my $self = shift;
Subroutine _build_filename: not called CC=1 SCAR=6.9
110  
111 0 return $self->base . '/app.json';
112 }
113  
114 =head3 base
115  
116 The base directory of the application, detected using L<FindBin>.
117  
118 =head3 dev
119  
120 A simple boolean flag to indicate whether you're running a development instance of the web-app.
121  
122 It's on by default, and currently turned off if the base directory contains C</live>. Feel free to set it based on your own logic before calling C<start()>.
123  
124 I typically use working directories such as C</home/user/www.example.com/dev> and C</home/user/www.example.com/live>.
125  
126 This flag is useful to disable things like Google Analytics on the dev site.
127  
128 The application object is available to all templates under the name C<app>.
129  
130 e.g. C<[% IF app.dev %] ... [% END %]>
131  
132 =head3 processes
133  
134 Number of FastCGI process to spawn, 5 by default.
135  
136 $app->processes( 10 );
137  
138 =head3 templates
139  
140 The directory containing the TT templates, by default it's C<$app-E<gt>base . '/templates'>.
141  
142 =head3 filename
143  
144 Filename for the config file, default is C<app.json> and only JSON is currently supported.
145  
146 =head3 config
147  
148 The config as a hash-reference.
149  
150 =head2 Instance Methods
151  
152 =head3 start
153  
154 Starts the FastCGI daemon. Performs basic checks of your environment and dies if there's a problem.
155  
156 =cut
157  
158 sub start
159 {
160 0 ▶ my $self = shift;
Subroutine start: not called CC=2 SCAR=17.9
Pod documented
161  
162 0 ▶ if ( -e $self->_pid )
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->_pid) 0 0 0
163 {
164 0 print "PID file " . $self->_pid . " already exists, I think you should kill that first, or specify a new pid file with the -p option\n";
165
166 0 return $self;
167 }
168  
169 0 $self->_init;
170  
171 0 print "Building FastCGI engine...\n";
172
173 0 my $server = Plack::Handler::FCGI->new(
174 nproc => $self->processes,
175 listen => [ $self->_socket ],
176 pid => $self->_pid,
177 detach => 1,
178 );
179
180 0 $server->run( $self->_handler );
181 }
182  
183 sub _init
184 {
185 0 ▶ my ( $self, %args ) = @_;
Subroutine _init: not called CC=15 SCAR=54.8
186  
187 ###############################
188 # make sure there's a log dir #
189 ###############################
190  
191 0 printf( "%-50s", "Checking logs directory");
192  
193 0 my $log_dir = $self->base . '/logs';
194  
195 0 ▶ if ( ! -e $log_dir || ! -d $log_dir )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $log_dir and -d $log_dir) 0 0 0
Condition0%0/3
-e $log_dir and -d $log_dir
!ll&&!rl&&r
000
MC/DC0%0/2
-e $log_dir and -d $log_dir
-e $log_dir -d $log_dir
Truth table0%0/3
A and B
A= -e $log_dirB= -d $log_dir
ABresult
0X0
100
111
196 {
197 0 _print_red( "[ FAIL ]\n" );
198 0 print $log_dir . " does not exist, or it's not a folder.\nExiting...\n";
199 0 exit;
200 }
201  
202 0 _print_green( "[ OK ]\n" );
203  
204 #####################################
205 # make sure there's a templates dir #
206 #####################################
207  
208 0 printf( "%-50s", "Checking templates directory");
209  
210 0 ▶ if ( ! -e $self->templates || ! -d $self->templates )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->templates and -d $self->templates) 0 0 0
Condition0%0/3
-e $self->templates and -d $self->templates
!ll&&!rl&&r
000
MC/DC0%0/2
-e $self->templates and -d $self->templates
-e $self->templates -d $self->templates
Truth table0%0/3
A and B
A= -e $self->templatesB= -d $self->templates
ABresult
0X0
100
111
211 {
212 0 _print_red( "[ FAIL ]\n" );
213 0 print $self->templates . " does not exist, or it's not a folder.\nExiting...\n";
214 0 exit;
215 }
216  
217 0 _print_green( "[ OK ]\n" );
218  
219 ###########################
220 # make sure 404.tt exists #
221 ###########################
222  
223 0 printf( "%-50s", "Checking 404 template");
224  
225 0 my $template_404_file = $self->templates . '/404.tt';
226  
227 0 ▶ if ( ! -e $template_404_file )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $template_404_file) 0 0 0
228 {
229 0 _print_red( "[ FAIL ]\n" );
230 0 print $template_404_file . " does not exist.\nExiting...\n";
231 0 exit;
232 }
233  
234 0 _print_green( "[ OK ]\n" );
235  
236 ########################
237 # load the config file #
238 ########################
239  
240 0 printf( "%-50s", "Checking config");
241  
242 0 ▶ if ( ! -e $self->filename )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->filename) 0 0 0
243 {
244 0 _print_red( "[ FAIL ]\n" );
245 0 print $self->filename . " does not exist.\nExiting...\n";
246 0 exit;
247 }
248  
249 0 my $string = read_file( $self->filename, { binmode => ':utf8' } );
250  
251 0 my $config = undef;
252  
253 0 eval {
254 0 my $json = JSON->new;
255  
256 0 $json->relaxed( 1 );
257  
258 0 $config = $json->decode( $string );
259 };
260  
261 0 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
262 {
263 0 _print_red( "[ FAIL ]\n" );
264 0 print "Config file error...\n" . $@ . "Exiting...\n";
265 0 exit;
266 }
267  
268 ###################################
269 # basic checks on the config file #
270 ###################################
271  
272 0 ▶ if ( ! $config->{ pages } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"pages"}) 0 0 0
273 {
274 0 _print_red( "[ FAIL ]\n" );
275 0 print "'pages' attribute missing at top level.\nExiting...\n";
276 0 exit;
277 }
278  
279 0 ▶ if ( ref $config->{ pages } ne 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config->{"pages"} ne "ARRAY") 0 0 0
280 {
281 0 _print_red( "[ FAIL ]\n" );
282 0 print "'pages' attribute is not a list.\nExiting...\n";
283 0 exit;
284 }
285  
286 0 ▶ if ( scalar @{ $config->{ pages } } == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$config->{"pages"};} == 0) 0 0 0
287 {
288 0 _print_organge( "[ISSUE]\n" );
289 0 print "No 'pages' defined in config, this will result in a 404 for all requests.\n";
290 }
291  
292 0 my %paths = ();
293  
294 0 foreach my $each_page ( @{ $config->{ pages } } )
295 {
296 0 ▶ if ( ! $each_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($each_page->{"path"}) 0 0 0
297 {
298 0 _print_red( "[ FAIL ]\n" );
299 0 print "'path' attribute missing for page..." . ( Dumper $each_page );
300 0 exit;
301 }
302  
303 0 ▶ if ( ! $each_page->{ template } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($each_page->{"template"}) 0 0 0
304 {
305 0 _print_red( "[ FAIL ]\n" );
306 0 print "'template' attribute missing for page..." . ( Dumper $each_page );
307 0 exit;
308 }
309  
310 0 ▶ if ( exists $paths{ $each_page->{ path } } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $paths{$each_page->{"path"}}) 0 0 0
311 {
312 0 _print_red( "[ FAIL ]\n" );
313 0 print "Path '" . $each_page->{ path } . "' found more than once.\nExiting...\n";
314 0 exit;
315 }
316  
317 0 $paths{ $each_page->{ path } } = 1;
318 }
319  
320 0 _print_green( "[ OK ]\n" );
321  
322 0 return $self;
323 }
324  
325 sub _print_green
326 {
327 0 ▶ my $string = shift;
Subroutine _print_green: not called CC=1 SCAR=6.9
328 0 print color 'bold green';
329 0 print $string;
330 0 print color 'reset';
331 }
332  
333 sub _print_orange
334 {
335 0 ▶ my $string = shift;
Subroutine _print_orange: not called CC=1 SCAR=6.9
336 0 print color 'bold orange';
337 0 print $string;
338 0 print color 'reset';
339 }
340  
341 sub _print_red
342 {
343 0 ▶ my $string = shift;
Subroutine _print_red: not called CC=1 SCAR=6.9
344 0 print color 'bold red';
345 0 print $string;
346 0 print color 'reset';
347 }
348  
349 =head3 stop
350  
351 Stops the FastCGI daemon.
352  
353 =cut
354  
355 sub stop
356 {
357 0 ▶ my $self = shift;
Subroutine stop: not called CC=3 SCAR=24.8
Pod documented
358  
359 0 ▶ if ( ! -e $self->_pid )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->_pid) 0 0 0
360 {
361 0 return $self;
362 }
363
364 0 ▶ open( my $fh, "<", $self->_pid ) or die "Cannot open pidfile: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $self->_pid or die "Cannot open pidfile: $!" 0 0 0
365  
366 0 my @pids = <$fh>;
367  
368 0 close $fh;
369  
370 0 chomp( $pids[0] );
371  
372 0 print "Killing pid $pids[0] ...\n";
373  
374 0 kill 15, $pids[0];
375  
376 0 return $self;
377 }
378  
379 =head3 restart
380  
381 Restarts the FastCGI daemon, with a 1 second delay between stopping and starting.
382  
383 =cut
384  
385 sub restart
386 {
387 0 ▶ my $self = shift;
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
388
389 0 $self->stop;
390  
391 0 sleep 1;
392  
393 0 $self->start;
394  
395 0 return $self;
396 }
397  
398 =head1 CONFIGURATION
399  
400 The app should be a simple Perl script in a folder with the following structure:
401  
402 app.pl # see the synopsis
403 app.json # see below
404 app.pid # generated, to control the process
405 app.sock # generated, to accept incoming FastCGI connections
406 logs/
407 templates/
408 404.tt
409  
410 The config file is read for each and every request, this makes adding new pages easy, without the need to restart the application.
411  
412 The config file should be placed in the C<base> directory of your application.
413  
414 See the C<examples> directory for a sample JSON config file, something like the following...
415  
416 {
417 "pages" : [
418 {
419 "path" : "/",
420 "template":"index.tt",
421 ...
422 },
423 ...
424 ]
425 ...
426 "send_alerts_from":"The Example App <no-reply@example.com>",
427 "send_404_alerts_to":"you@example.com",
428 ...
429 }
430  
431 The entire config hash is available in all templates via C<[% app.config %]>, there are only a couple of mandatory/reserved attributes.
432  
433 The mandatory field in the config is C<pages>, an array of pages.
434  
435 Each C<page> should contain a C<path> (for URL matching) and C<template> to render.
436  
437 All other fields are completely up to you, to fit your requirements.
438  
439 When a request is made, a lookup is performed for a page by matching the C<path>, which then results in rendering the associated C<template>.
440  
441 If no page is found, the template C<404.tt> will be rendered, make sure you have this file ready in the templates directory.
442  
443 The C<page> object is available in the rendered template, eg, C<[% page.path %]>
444  
445 It is often useful to have sub-pages and categories, etc. Simply create a C<pages> attribute in a C<page> object as another array of C<page> objects.
446  
447 If a sub-page is matched and selected for a request, an extra key for C<parents> is included in the C<page> object as a list of the parent pages, this is useful for building breadcrumb links.
448  
449 =cut
450  
451 # returns a code-ref for the FCGI handler/server.
452  
453 sub _handler
454 {
455 0 ▶ my $self = shift;
Subroutine _handler: not called CC=1 SCAR=6.9
456  
457 return sub {
458  
459 ##############
460 # initialise #
461 ##############
462  
463 0 ▶ my $req = Plack::Request->new( shift );
Subroutine __ANON__: not called CC=11 SCAR=48.8
464  
465 0 my %stash = (
466 app => $self,
467 req => $req,
468 now => DateTime->now,
469 started => join( '.', gettimeofday ),
470 );
471  
472 0 my $log = Log::AutoDump->new( base_dir => $stash{ app }->base . '/logs', filename => 'app.log' );
473  
474 0 $log->debug("Started");
475  
476 0 my $path = $req->uri->path;
477  
478 0 $log->debug( "Requested path: " . $path );
479  
480 0 $stash{ app }->_reload_config( log => $log );
481  
482 ###############
483 # sitemap xml #
484 ###############
485  
486 0 ▶ if ( $path eq '/sitemap.xml' )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/sitemap.xml") 0 0 0
487 {
488 0 return $stash{ app }->_sitemap( log => $log, req => $req, stash => \%stash );
489 }
490  
491 ##########################################################################
492 # find a matching 'page' from the config that matches the requested path #
493 ##########################################################################
494  
495 # need to do proper recursion here
496  
497 0 foreach my $each_page ( @{ $stash{ app }->{ config }->{ pages } } )
498 {
499 0 ▶ if ( $path eq $each_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $each_page->{"path"}) 0 0 0
500 {
501 0 $stash{ page } = $each_page;
502  
503 0 last;
504 }
505  
506 0 ▶ if ( ref $each_page->{ pages } eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $each_page->{"pages"} eq "ARRAY") 0 0 0
507 {
508 0 foreach my $each_sub_page ( @{ $each_page->{ pages } } )
509 {
510 0 ▶ if ( $path eq $each_sub_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $each_sub_page->{"path"}) 0 0 0
511 {
512 0 $stash{ page } = $each_sub_page;
513  
514 0 $stash{ page }->{ parents } = [];
515
516 0 push @{ $stash{ page }->{ parents } }, $each_page;
517
518 0 last;
519 }
520 }
521 }
522 }
523  
524 0 ▶ $log->debug( "Matching page found in config...", $stash{ page } ) if exists $stash{ page };
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash{"page"} 0 0 0
525  
526 #######
527 # 404 #
528 #######
529
530 0 ▶ if ( ! exists $stash{ page } )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stash{"page"}) 0 0 0
531 {
532 0 return $stash{ app }->_404( log => $log, req => $req, stash => \%stash );
533 }
534  
535 ##############################
536 # responding with a template #
537 ##############################
538  
539 0 my $res = $req->new_response;
540  
541 0 $res->status( 200 );
542  
543 0 my $tt = Template->new( ENCODING => 'UTF-8', INCLUDE_PATH => $stash{ app }->templates );
544  
545 0 $log->debug("Processing template: " . $stash{ app }->templates . "/" . $stash{ page }->{ template } );
546  
547 0 my $body = '';
548  
549 0 ▶ $tt->process( $stash{ page }->{ template }, \%stash, \$body ) or $log->debug( $tt->error );
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($stash{"page"}{"template"}, \%stash, \$body) or $log->debug($tt->error) 0 0 0
550  
551 0 $res->content_type('text/html; charset=utf-8');
552  
553 0 $res->body( encode( "UTF-8", $body ) );
554  
555 #########
556 # stats #
557 #########
558  
559 0 $stash{ took } = join( '.', gettimeofday ) - $stash{ started };
560
561 0 $log->debug( "The stash contains...", \%stash );
562
563 0 $log->debug( "Took " . sprintf("%.5f", $stash{ took } ) . " seconds");
564  
565 #######################################
566 # cleanup (circular references, etc.) #
567 #######################################
568  
569 # need to do deep pages too!
570  
571 0 ▶ delete $stash{ page }->{ parents } if exists $stash{ page };
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash{"page"} 0 0 0
572  
573 0 return $res->finalize;
574 }
575 0 }
576  
577 sub _sitemap
578 {
579 0 ▶ my ( $self, %args ) = @_;
Subroutine _sitemap: not called CC=8 SCAR=42.8
580  
581 0 my $log = $args{ log };
582 0 my $req = $args{ req };
583 0 my $stash = $args{ stash };
584  
585 my $base = ($req->env->{'psgi.url_scheme'} || "http") .
586 0 ▶ "://" . ($req->env->{HTTP_HOST} || (($req->env->{SERVER_NAME} || "") . ":" . ($req->env->{SERVER_PORT} || 80)));
Condition0%0/8
$req->env->{'psgi.url_scheme'} || 'http'
l!l
00
$req->env->{'HTTP_HOST'} || ($req->env->{'SERVER_NAME'} || '') . ':' . ($req->env->{'SERVER_PORT'} || 80)
l!l
00
$req->env->{'SERVER_NAME'} || ''
l!l
00
$req->env->{'SERVER_PORT'} || 80
l!l
00
MC/DC0%0/1
$req->env->{'psgi.url_scheme'} || 'http'
$req->env->{'psgi.url_scheme'}
MC/DC0%0/1
$req->env->{'HTTP_HOST'} || ($req->env->{'SERVER_NAME'} || '') . ':' . ($req->env->{'SERVER_PORT'} || 80)
$req->env->{'HTTP_HOST'}
MC/DC0%0/1
$req->env->{'SERVER_PORT'} || 80
$req->env->{'SERVER_PORT'}
Truth table0%0/2
A
A= $req->env->{'psgi.url_scheme'}
Aresult
11
00
Truth table0%0/2
A
A= $req->env->{'HTTP_HOST'}
Aresult
11
00
Truth table0%0/2
A
A= $req->env->{'SERVER_PORT'}
Aresult
11
00
587  
588 0 my $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
589  
590 0 $sitemap .= "\n";
591  
592 0 $sitemap .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">';
593  
594 0 $sitemap .= "\n";
595  
596 # need to do proper recursion here
597
598 0 foreach my $each_page ( @{ $stash->{ config }->{ pages } } )
599 {
600 0 $sitemap .= "<url><loc>" . $base . $each_page->{ path } . "</loc></url>\n";
601  
602 0 ▶ if ( ref $each_page->{ pages } eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $each_page->{"pages"} eq "ARRAY") 0 0 0
603 {
604 0 foreach my $each_sub_page ( @{ $each_page->{ pages } } )
605 {
606 0 $sitemap .= "<url><loc>" . $base . $each_sub_page->{ path } . "</loc></url>\n";
607 }
608 }
609 }
610
611 0 $sitemap .= "</urlset>\n";
612  
613 0 my $res = $req->new_response;
614  
615 0 $res->status(200);
616  
617 0 $res->content_type('application/xml; charset=utf-8');
618
619 0 $res->body( encode( "UTF-8", $sitemap ) );
620  
621 0 return $res->finalize;
622 }
623  
624 sub _404
625 {
626 0 ▶ my ( $self, %args ) = @_;
Subroutine _404: not called CC=5 SCAR=34.0
627  
628 0 my $log = $args{ log };
629 0 my $req = $args{ req };
630 0 my $stash = $args{ stash };
631  
632 0 $stash->{ page } = { template => '404.tt' };
633  
634 0 ▶ if ( $stash->{ config }->{ send_alerts_from } && $stash->{ config }->{ send_404_alerts_to } )
Branch: 1 branch
BranchTrueFalseTotal
if ($stash->{"config"}{"send_alerts_from"} and $stash->{"config"}{"send_404_alerts_to"}) 0 0 0
Condition0%0/3
$stash->{"config"}{"send_alerts_from"} and $stash->{"config"}{"send_404_alerts_to"}
!ll&&!rl&&r
000
MC/DC0%0/2
$stash->{"config"}{"send_alerts_from"} and $stash->{"config"}{"send_404_alerts_to"}
$stash->{"config"}{"send_alerts_from"} $stash->{"config"}{"send_404_alerts_to"}
Truth table0%0/3
A and B
A= $stash->{"config"}{"send_alerts_from"}B= $stash->{"config"}{"send_404_alerts_to"}
ABresult
0X0
100
111
635 {
636 $stash->{ app }->_send_email(
637 from => $stash->{ config }->{ send_alerts_from },
638 to => $stash->{ config }->{ send_404_alerts_to },
639 0 ▶ subject => "404 - " . $req->uri,
Condition0%0/2
$req->referer || 'None'
l!l
00
MC/DC0%0/1
$req->referer || 'None'
$req->referer
Truth table0%0/2
A
A= $req->referer
Aresult
11
00
640 text_body => "404 - " . $req->uri . "\n\nReferrer: " . ( $req->referer || 'None' ) . "\n\n" . Dumper( $req ) . "\n\n" . Dumper( \%ENV ),
641 );
642 }
643  
644 0 my $res = $req->new_response;
645  
646 0 $res->status( 404 );
647  
648 0 $res->content_type('text/html; charset=utf-8');
649  
650 0 my $tt = Template->new( ENCODING => 'UTF-8', INCLUDE_PATH => $stash->{ app }->templates );
651  
652 0 $log->debug("Processing template: " . $stash->{ app }->templates . "/" . $stash->{ page }->{ template } );
653  
654 0 my $body = '';
655  
656 0 ▶ $tt->process( $stash->{ page }->{ template }, $stash, \$body ) or $log->debug( $tt->error );
Branch: 1 branch
BranchTrueFalseTotal
$tt->process($stash->{"page"}{"template"}, $stash, \$body) or $log->debug($tt->error) 0 0 0
657  
658 0 $res->content_type('text/html; charset=utf-8');
659  
660 0 $res->body( encode( "UTF-8", $body ) );
661  
662 0 return $res->finalize;
663 }
664  
665 sub _reload_config
666 {
667 0 ▶ my ( $self, %args ) = @_;
Subroutine _reload_config: not called CC=2 SCAR=17.9
668  
669 0 my $log = $args{ log };
670  
671 0 $log->debug( "Opening config file: " . $self->filename );
672  
673 0 my $string = read_file( $self->filename, { binmode => ':utf8' } );
674  
675 0 my $config = undef;
676  
677 0 eval {
678 0 my $json = JSON->new;
679  
680 0 $json->relaxed( 1 );
681  
682 0 $self->config( $json->decode( $string ) );
683 };
684  
685 0 ▶ $log->debug( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
686  
687 0 return $self;
688 }
689  
690 sub _send_email
691 {
692 0 ▶ my ( $self, %args ) = @_;
Subroutine _send_email: not called CC=2 SCAR=17.9
693  
694 0 ▶ if ( $args{ to } )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"to"}) 0 0 0
695 {
696 Email::Stuffer->from( $args{ from } )
697 ->to( $args{ to } )
698 ->subject( $args{ subject } )
699 ->text_body( $args{ text_body } )
700 0 ->send;
701 }
702  
703 0 return $self;
704 }
705  
706 =head1 TODO
707  
708 Deep recursion for page/path lookups.
709  
710 Deep recursion for sitemap.
711  
712 Cleanup deeper recursion in pages with parents.
713  
714 Searching, somehow, of some set of templates.
715  
716 =head1 AUTHOR
717  
718 Rob Brown, C<< <rob at intelcompute.com> >>
719  
720 =head1 LICENSE AND COPYRIGHT
721  
722 Copyright 2014 Rob Brown.
723  
724 This program is free software; you can redistribute it and/or modify it
725 under the terms of either: the GNU General Public License as published
726 by the Free Software Foundation; or the Artistic License.
727  
728 See http://dev.perl.org/licenses/ for more information.
729  
730 =cut
731  
732 3 1;
733  
↑ Summary