blib/lib/Lavoco/Web/App.pm

filter: Statementstmt 21.5% 56 / 260 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.3% 74 / 382 SCAR 69.7
CC
42
Cov
17%
_init
240.0
__ANON__
132.0
_sitemap
72.0
CRAP 1061.0
↑ 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.04
30  
31 =cut
32  
33 3 our $VERSION = '0.04';
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 $config = decode_json $string;
255 };
256  
257 0 ▶ if ( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
258 {
259 0 _print_red( "[ FAIL ]\n" );
260 0 print "Config file error...\n" . $@ . "Exiting...\n";
261 0 exit;
262 }
263  
264 ###################################
265 # basic checks on the config file #
266 ###################################
267  
268 0 ▶ if ( ! $config->{ pages } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"pages"}) 0 0 0
269 {
270 0 _print_red( "[ FAIL ]\n" );
271 0 print "'pages' attribute missing at top level.\nExiting...\n";
272 0 exit;
273 }
274  
275 0 ▶ if ( ref $config->{ pages } ne 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $config->{"pages"} ne "ARRAY") 0 0 0
276 {
277 0 _print_red( "[ FAIL ]\n" );
278 0 print "'pages' attribute is not a list.\nExiting...\n";
279 0 exit;
280 }
281  
282 0 ▶ if ( scalar @{ $config->{ pages } } == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$config->{"pages"};} == 0) 0 0 0
283 {
284 0 _print_organge( "[ISSUE]\n" );
285 0 print "No 'pages' defined in config, this will result in a 404 for all requests.\n";
286 }
287  
288 0 my %paths = ();
289  
290 0 foreach my $each_page ( @{ $config->{ pages } } )
291 {
292 0 ▶ if ( ! $each_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($each_page->{"path"}) 0 0 0
293 {
294 0 _print_red( "[ FAIL ]\n" );
295 0 print "'path' attribute missing for page..." . ( Dumper $each_page );
296 0 exit;
297 }
298  
299 0 ▶ if ( ! $each_page->{ template } )
Branch: 1 branch
BranchTrueFalseTotal
unless ($each_page->{"template"}) 0 0 0
300 {
301 0 _print_red( "[ FAIL ]\n" );
302 0 print "'template' attribute missing for page..." . ( Dumper $each_page );
303 0 exit;
304 }
305  
306 0 ▶ if ( exists $paths{ $each_page->{ path } } )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $paths{$each_page->{"path"}}) 0 0 0
307 {
308 0 _print_red( "[ FAIL ]\n" );
309 0 print "Path '" . $each_page->{ path } . "' found more than once.\nExiting...\n";
310 0 exit;
311 }
312  
313 0 $paths{ $each_page->{ path } } = 1;
314 }
315  
316 0 _print_green( "[ OK ]\n" );
317  
318 0 return $self;
319 }
320  
321 sub _print_green
322 {
323 0 ▶ my $string = shift;
Subroutine _print_green: not called CC=1 SCAR=6.9
324 0 print color 'bold green';
325 0 print $string;
326 0 print color 'reset';
327 }
328  
329 sub _print_orange
330 {
331 0 ▶ my $string = shift;
Subroutine _print_orange: not called CC=1 SCAR=6.9
332 0 print color 'bold orange';
333 0 print $string;
334 0 print color 'reset';
335 }
336  
337 sub _print_red
338 {
339 0 ▶ my $string = shift;
Subroutine _print_red: not called CC=1 SCAR=6.9
340 0 print color 'bold red';
341 0 print $string;
342 0 print color 'reset';
343 }
344  
345 =head3 stop
346  
347 Stops the FastCGI daemon.
348  
349 =cut
350  
351 sub stop
352 {
353 0 ▶ my $self = shift;
Subroutine stop: not called CC=3 SCAR=24.8
Pod documented
354  
355 0 ▶ if ( ! -e $self->_pid )
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->_pid) 0 0 0
356 {
357 0 return $self;
358 }
359
360 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
361  
362 0 my @pids = <$fh>;
363  
364 0 close $fh;
365  
366 0 chomp( $pids[0] );
367  
368 0 print "Killing pid $pids[0] ...\n";
369  
370 0 kill 15, $pids[0];
371  
372 0 return $self;
373 }
374  
375 =head3 restart
376  
377 Restarts the FastCGI daemon, with a 1 second delay between stopping and starting.
378  
379 =cut
380  
381 sub restart
382 {
383 0 ▶ my $self = shift;
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
384
385 0 $self->stop;
386  
387 0 sleep 1;
388  
389 0 $self->start;
390  
391 0 return $self;
392 }
393  
394 =head1 CONFIGURATION
395  
396 The app should be a simple Perl script in a folder with the following structure:
397  
398 app.pl # see the synopsis
399 app.json # see below
400 app.pid # generated, to control the process
401 app.sock # generated, to accept incoming FastCGI connections
402 logs/
403 templates/
404 404.tt
405  
406 The config file is read for each and every request, this makes adding new pages easy, without the need to restart the application.
407  
408 The config file should be placed in the C<base> directory of your application.
409  
410 See the C<examples> directory for a sample JSON config file, something like the following...
411  
412 {
413 "pages" : [
414 {
415 "path" : "/",
416 "template":"index.tt",
417 ...
418 },
419 ...
420 ]
421 ...
422 "send_alerts_from":"The Example App <no-reply@example.com>",
423 "send_404_alerts_to":"you@example.com",
424 ...
425 }
426  
427 The entire config hash is available in all templates via C<[% app.config %]>, there are only a couple of mandatory/reserved attributes.
428  
429 The mandatory field in the config is C<pages>, an array of pages.
430  
431 Each C<page> should contain a C<path> (for URL matching) and C<template> to render.
432  
433 All other fields are completely up to you, to fit your requirements.
434  
435 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>.
436  
437 If no page is found, the template C<404.tt> will be rendered, make sure you have this file ready in the templates directory.
438  
439 The C<page> object is available in the rendered template, eg, C<[% page.path %]>
440  
441 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.
442  
443 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.
444  
445 =cut
446  
447 # returns a code-ref for the FCGI handler/server.
448  
449 sub _handler
450 {
451 0 ▶ my $self = shift;
Subroutine _handler: not called CC=1 SCAR=6.9
452  
453 return sub {
454  
455 ##############
456 # initialise #
457 ##############
458  
459 0 ▶ my $req = Plack::Request->new( shift );
Subroutine __ANON__: not called CC=11 SCAR=48.8
460  
461 0 my %stash = (
462 app => $self,
463 req => $req,
464 now => DateTime->now,
465 started => join( '.', gettimeofday ),
466 );
467  
468 0 my $log = Log::AutoDump->new( base_dir => $stash{ app }->base . '/logs', filename => 'app.log' );
469  
470 0 $log->debug("Started");
471  
472 0 my $path = $req->uri->path;
473  
474 0 $log->debug( "Requested path: " . $path );
475  
476 0 $stash{ app }->_reload_config( log => $log );
477  
478 ###############
479 # sitemap xml #
480 ###############
481  
482 0 ▶ if ( $path eq '/sitemap.xml' )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq "/sitemap.xml") 0 0 0
483 {
484 0 return $stash{ app }->_sitemap( log => $log, req => $req, stash => \%stash );
485 }
486  
487 ##########################################################################
488 # find a matching 'page' from the config that matches the requested path #
489 ##########################################################################
490  
491 # need to do proper recursion here
492  
493 0 foreach my $each_page ( @{ $stash{ app }->{ config }->{ pages } } )
494 {
495 0 ▶ if ( $path eq $each_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $each_page->{"path"}) 0 0 0
496 {
497 0 $stash{ page } = $each_page;
498  
499 0 last;
500 }
501  
502 0 ▶ if ( ref $each_page->{ pages } eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $each_page->{"pages"} eq "ARRAY") 0 0 0
503 {
504 0 foreach my $each_sub_page ( @{ $each_page->{ pages } } )
505 {
506 0 ▶ if ( $path eq $each_sub_page->{ path } )
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq $each_sub_page->{"path"}) 0 0 0
507 {
508 0 $stash{ page } = $each_sub_page;
509  
510 0 $stash{ page }->{ parents } = [];
511
512 0 push @{ $stash{ page }->{ parents } }, $each_page;
513
514 0 last;
515 }
516 }
517 }
518 }
519  
520 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
521  
522 #######
523 # 404 #
524 #######
525
526 0 ▶ if ( ! exists $stash{ page } )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $stash{"page"}) 0 0 0
527 {
528 0 return $stash{ app }->_404( log => $log, req => $req, stash => \%stash );
529 }
530  
531 ##############################
532 # responding with a template #
533 ##############################
534  
535 0 my $res = $req->new_response;
536  
537 0 $res->status( 200 );
538  
539 0 my $tt = Template->new( ENCODING => 'UTF-8', INCLUDE_PATH => $stash{ app }->templates );
540  
541 0 $log->debug("Processing template: " . $stash{ app }->templates . "/" . $stash{ page }->{ template } );
542  
543 0 my $body = '';
544  
545 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
546  
547 0 $res->content_type('text/html; charset=utf-8');
548  
549 0 $res->body( encode( "UTF-8", $body ) );
550  
551 #########
552 # stats #
553 #########
554  
555 0 $stash{ took } = join( '.', gettimeofday ) - $stash{ started };
556
557 0 $log->debug( "The stash contains...", \%stash );
558
559 0 $log->debug( "Took " . sprintf("%.5f", $stash{ took } ) . " seconds");
560  
561 #######################################
562 # cleanup (circular references, etc.) #
563 #######################################
564  
565 # need to do deep pages too!
566  
567 0 ▶ delete $stash{ page }->{ parents } if exists $stash{ page };
Branch: 1 branch
BranchTrueFalseTotal
if exists $stash{"page"} 0 0 0
568  
569 0 return $res->finalize;
570 }
571 0 }
572  
573 sub _sitemap
574 {
575 0 ▶ my ( $self, %args ) = @_;
Subroutine _sitemap: not called CC=8 SCAR=42.8
576  
577 0 my $log = $args{ log };
578 0 my $req = $args{ req };
579 0 my $stash = $args{ stash };
580  
581 my $base = ($req->env->{'psgi.url_scheme'} || "http") .
582 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
583  
584 0 my $sitemap = '<?xml version="1.0" encoding="UTF-8"?>';
585  
586 0 $sitemap .= "\n";
587  
588 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">';
589  
590 0 $sitemap .= "\n";
591  
592 # need to do proper recursion here
593
594 0 foreach my $each_page ( @{ $stash->{ config }->{ pages } } )
595 {
596 0 $sitemap .= "<url><loc>" . $base . $each_page->{ path } . "</loc></url>\n";
597  
598 0 ▶ if ( ref $each_page->{ pages } eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $each_page->{"pages"} eq "ARRAY") 0 0 0
599 {
600 0 foreach my $each_sub_page ( @{ $each_page->{ pages } } )
601 {
602 0 $sitemap .= "<url><loc>" . $base . $each_sub_page->{ path } . "</loc></url>\n";
603 }
604 }
605 }
606
607 0 $sitemap .= "</urlset>\n";
608  
609 0 my $res = $req->new_response;
610  
611 0 $res->status(200);
612  
613 0 $res->content_type('application/xml; charset=utf-8');
614
615 0 $res->body( encode( "UTF-8", $sitemap ) );
616  
617 0 return $res->finalize;
618 }
619  
620 sub _404
621 {
622 0 ▶ my ( $self, %args ) = @_;
Subroutine _404: not called CC=5 SCAR=34.0
623  
624 0 my $log = $args{ log };
625 0 my $req = $args{ req };
626 0 my $stash = $args{ stash };
627  
628 0 $stash->{ page } = { template => '404.tt' };
629  
630 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
631 {
632 $stash->{ app }->_send_email(
633 from => $stash->{ config }->{ send_alerts_from },
634 to => $stash->{ config }->{ send_404_alerts_to },
635 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
636 text_body => "404 - " . $req->uri . "\n\nReferrer: " . ( $req->referer || 'None' ) . "\n\n" . Dumper( $req ) . "\n\n" . Dumper( \%ENV ),
637 );
638 }
639  
640 0 my $res = $req->new_response;
641  
642 0 $res->status( 404 );
643  
644 0 $res->content_type('text/html; charset=utf-8');
645  
646 0 my $tt = Template->new( ENCODING => 'UTF-8', INCLUDE_PATH => $stash->{ app }->templates );
647  
648 0 $log->debug("Processing template: " . $stash->{ app }->templates . "/" . $stash->{ page }->{ template } );
649  
650 0 my $body = '';
651  
652 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
653  
654 0 $res->content_type('text/html; charset=utf-8');
655  
656 0 $res->body( encode( "UTF-8", $body ) );
657  
658 0 return $res->finalize;
659 }
660  
661 sub _reload_config
662 {
663 0 ▶ my ( $self, %args ) = @_;
Subroutine _reload_config: not called CC=2 SCAR=17.9
664  
665 0 my $log = $args{ log };
666  
667 0 $log->debug( "Opening config file: " . $self->filename );
668  
669 0 my $string = read_file( $self->filename, { binmode => ':utf8' } );
670  
671 0 my $config = undef;
672  
673 0 eval {
674 0 $self->config( decode_json $string );
675 };
676  
677 0 ▶ $log->debug( $@ ) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
678  
679 0 return $self;
680 }
681  
682 sub _send_email
683 {
684 0 ▶ my ( $self, %args ) = @_;
Subroutine _send_email: not called CC=2 SCAR=17.9
685  
686 0 ▶ if ( $args{ to } )
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"to"}) 0 0 0
687 {
688 Email::Stuffer->from( $args{ from } )
689 ->to( $args{ to } )
690 ->subject( $args{ subject } )
691 ->text_body( $args{ text_body } )
692 0 ->send;
693 }
694  
695 0 return $self;
696 }
697  
698 =head1 TODO
699  
700 Deep recursion for page/path lookups.
701  
702 Deep recursion for sitemap.
703  
704 Cleanup deeper recursion in pages with parents.
705  
706 Searching, somehow, of some set of templates.
707  
708 =head1 AUTHOR
709  
710 Rob Brown, C<< <rob at intelcompute.com> >>
711  
712 =head1 LICENSE AND COPYRIGHT
713  
714 Copyright 2014 Rob Brown.
715  
716 This program is free software; you can redistribute it and/or modify it
717 under the terms of either: the GNU General Public License as published
718 by the Free Software Foundation; or the Artistic License.
719  
720 See http://dev.perl.org/licenses/ for more information.
721  
722 =cut
723  
724 3 1;
725  
↑ Summary