sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
-my $env;
+my ($env, $privacy);
sub db { $env->{'gruntmaster.dbic'} }
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};
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]]
},
},
response st => 'Standings', {
st => [ contest->standings ],
problems => [map { $_->problem } contest->contest_problems],
- }
+ }, 10
},
sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
},
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) {
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';
},
owner => remote_user->id,
});
+ purge '/log/';
redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';
}
}