]> iEval git - gruntmaster-page.git/commitdiff
Enable caching
authorMarius Gavrilescu <marius@ieval.ro>
Fri, 19 Dec 2014 10:54:40 +0000 (12:54 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Fri, 19 Dec 2014 10:54:40 +0000 (12:54 +0200)
lib/Plack/App/Gruntmaster.pm

index 17d198d74b976fcb95397f5e34214999365756bf..56f0ec11c19912ec2a7f20141bb2b4f604f41a45 100644 (file)
@@ -54,7 +54,7 @@ use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authentica
 
 sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
 
-my $env;
+my ($env, $privacy);
 
 sub db { $env->{'gruntmaster.dbic'} }
 
@@ -73,22 +73,26 @@ sub user    { db->user    ($_{user})    }
 sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
 sub reply    { [200, ['Content-Type' => 'text/plain'], \@_] }
 sub response {
-       my ($template, $title, $params) = @_;
+       my ($template, $title, $params, $maxage) = @_;
        unless ($params) {
                $params = $title;
                $title = 'No title';
        }
        $params->{time} = time;
-       bless {template => $template, title => $title, params => $params}, __PACKAGE__.'::Response'
+       bless {template => $template, title => $title, params => $params, maxage => ($maxage // 1)}, __PACKAGE__.'::Response'
 }
 
-sub forbid   {
-       return if !shift || admin;
+sub forbid {
+       my ($condition) = @_;
+       $privacy = 'private' if $condition;
+       return if !$condition || admin;
        unwind FORBIDDEN, SUB UP
 }
 
 sub dispatch_request{
        $env = $_[PSGI_ENV];
+       $privacy = 'public';
+
        sub (GET) {
                sub (/css/:theme) {
                        my $theme = $_{theme};
@@ -130,9 +134,10 @@ sub dispatch_request{
                        response_filter {
                                my ($r) = @_;
                                return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
-                               return [200, ['Content-Type' => 'application/json'], [encode_json $r->{params}]] if $format eq 'json';
+                               my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
+                               return [200, ['Content-Type' => 'application/json', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
                                my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
-                               [200, ['Content-Type' => 'text/html'], [encode 'UTF-8', $ret]]
+                               [200, ['Content-Type' => 'text/html', @hdrs], [encode 'UTF-8', $ret]]
                        },
                },
 
@@ -140,7 +145,7 @@ sub dispatch_request{
                        response st => 'Standings', {
                                st => [ contest->standings ],
                                problems => [map { $_->problem } contest->contest_problems],
-                       }
+                       }, 10
                },
 
                sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
@@ -166,7 +171,7 @@ sub dispatch_request{
                },
 
                sub (/) { redispatch_to '/index' },
-               sub (/:article) { [200, ['Content-Type' => 'text/html'], [render_article $_{article}, 'en']] }
+               sub (/:article) { [200, ['Content-Type' => 'text/html', 'Cache-Control' => 'public, max-age=60', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
        },
 
        sub (POST) {
@@ -179,6 +184,7 @@ sub dispatch_request{
                        db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}});
                        db->user($_{username})->set_passphrase($_{password});
 
+                       purge '/us/';
                        reply 'Registered successfully';
                },
 
@@ -211,6 +217,7 @@ sub dispatch_request{
                                owner => remote_user->id,
                        });
 
+                       purge '/log/';
                        redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';
                }
        }
This page took 0.031463 seconds and 4 git commands to generate.