]> iEval git - gruntmaster-page.git/blobdiff - lib/Gruntmaster/Page/Base.pm
Add Cache-Control to most requests
[gruntmaster-page.git] / lib / Gruntmaster / Page / Base.pm
index bbb75f214aa58d252943af63f8aba643c0fe3866..cf7d2291587809652e8636c03d692692161ed4ae 100644 (file)
@@ -4,7 +4,6 @@ use 5.014000;
 use strict;
 use warnings;
 
-use Encode qw/encode/;
 use File::Slurp qw/read_file/;
 use HTML::Template::Compiled;
 
@@ -13,6 +12,9 @@ use HTML::Template::Compiled;
 use POSIX ();
 use Gruntmaster::Data ();
 use List::Util ();
+use LWP::UserAgent;
+
+my $ua = LWP::UserAgent->new;
 
 sub import {
        my $caller = caller;
@@ -27,7 +29,13 @@ sub import {
        *{"${caller}::TITLE"} = sub () { $title };
        *{"${caller}::debug"} = sub {
                local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
-               $_[0]->({qw/level debug message/ => $_[1]})
+               $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
+       };
+       *{"${caller}::reply"} = sub { [200, ['Content-Type' => 'text/plain', 'Cache-Control' => 'no-cache'], [ @_ ] ] };
+       *{"${caller}::purge"} = sub {
+               return unless $ENV{PURGE_HOST};
+               my $req = HTTP::Request->new(PURGE => "http://$ENV{PURGE_HOST}$_[0]");
+               $ua->request($req)
        };
 }
 
@@ -75,8 +83,8 @@ my %orig_header_templates = (
 
 <div class="container-fluid">
 
-<div id="subtitle">TITLE_GOES_HERE</div>
-
+<h1 id="title">TITLE_GOES_HERE</h1>
+<div id="result"></div>
 HTML
 );
 
@@ -92,7 +100,8 @@ HTML
 );
 
 sub patch_templates {
-       my $root = $ENV{GRUNTMASTER_TEMPLATE_ROOT} or return %{$_[0]};
+       my $root = 'tmpl';
+       return %{$_[0]} unless -d $root;
        my ($templates, $name) = @_;
        my %out = %$templates;
        for (<$root/$name.*>) {
@@ -103,19 +112,15 @@ sub patch_templates {
        %out
 }
 
-sub reload_templates (){ $ENV{GRUNTMASTER_RELOAD_TEMPLATES} }
-
 my %header_templates = patch_templates \%orig_header_templates, 'header';
 my %footer_templates = patch_templates \%orig_footer_templates, 'footer';
 
 sub header{
   my ($language, $title) = @_;
-  %header_templates = patch_templates \%orig_header_templates, 'header' if reload_templates;
   $header_templates{$language} =~ s/TITLE_GOES_HERE/$title/ger;
 }
 
 sub footer{
-  %footer_templates = patch_templates \%orig_footer_templates, 'footer' if reload_templates;
   $footer_templates{$_[0]};
 }
 
@@ -136,15 +141,19 @@ my %templates;
 sub generate{
        my ($self, $lang, @args) = @_;
 
-       $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } if !exists $templates{$self} or reload_templates;
+       $templates{$self} = { cook_templates $self->TEMPLATES, $self->NAME => $self->TITLE } unless exists $templates{$self};
 
        my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$self}{$lang}, default_escape => 'HTML',);
        $self->_generate($htc, $lang, @args);
-       [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language'], [ encode 'UTF-8' => $htc->output ] ]
+       my $out = $htc->output;
+       utf8::downgrade($out);
+       [200, ['Content-Type' => 'text/html', 'Content-Language' => $_[1], 'Vary' => 'Accept-Language', 'X-Forever' => 1, 'Cache-Control' => 'max-age=' . $self->max_age], [ $out ] ]
 }
 
 sub _generate {}
 
+sub max_age { 60 }
+
 sub variants {
        [ map { [ $_, 1, 'text/html', undef, undef, $_, undef ]} keys $_[0]->TEMPLATES ]
 }
This page took 0.024222 seconds and 4 git commands to generate.