From: Marius Gavrilescu Date: Fri, 19 Dec 2014 10:54:40 +0000 (+0200) Subject: Enable caching X-Git-Url: http://git.ieval.ro/?p=plack-app-gruntmaster.git;a=commitdiff_plain;h=c039d63e2d631c200f73ae59de2a7d1e66debc71 Enable caching --- diff --git a/lib/Plack/App/Gruntmaster.pm b/lib/Plack/App/Gruntmaster.pm index 17d198d..56f0ec1 100644 --- a/lib/Plack/App/Gruntmaster.pm +++ b/lib/Plack/App/Gruntmaster.pm @@ -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/'; } }