X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FPlack%2FApp%2FGruntmaster.pm;h=31ea346f8f9d637d05ba29565b2108c6934b79a0;hb=7baee29b2642aabb77a24745dc8be86440c30a4f;hp=77b374894e7a254fa4eb9271b260386c1eb40151;hpb=1f64ef28e31c592b91d5d75850229e01e7f89b84;p=plack-app-gruntmaster.git diff --git a/lib/Plack/App/Gruntmaster.pm b/lib/Plack/App/Gruntmaster.pm index 77b3748..31ea346 100644 --- a/lib/Plack/App/Gruntmaster.pm +++ b/lib/Plack/App/Gruntmaster.pm @@ -7,6 +7,7 @@ our $VERSION = '5999.000_001'; use Encode qw/encode decode/; use File::Slurp qw/read_file/; use JSON::MaybeXS qw/encode_json/; +use List::Util qw/min/; use PerlX::Maybe; use Scope::Upper qw/unwind SUB UP/; use Web::Simple; @@ -22,18 +23,24 @@ no warnings 'illegalproto'; use constant USER_REGEX => qr/^\w{2,20}$/a; use constant FORMAT_EXTENSION => { + BRAINFUCK => 'bf', C => 'c', CPP => 'cpp', + D => 'd', GCCGO => 'go', GOLANG => 'go', GOLFSCRIPT => 'gs', HASKELL => 'hs', - MONO => 'cs', JAVA => 'java', + JULIA => 'jl', + MONO => 'cs', + OCAML => 'ml', PASCAL => 'pas', PERL => 'pl', + PHP => 'php', PYTHON => 'py', RUBY => 'rb', + RUST => 'rs', SBCL => 'l', }; @@ -55,14 +62,14 @@ sub admin { remote_user && remote_user->{admin} } sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] } sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] } sub response { - my ($template, $title, $params, $maxage) = @_; + my ($template, $title, $params, $maxage, $smaxage) = @_; unless ($params) { $params = $title; $title = 'No title'; } $params->{time} = time; $params->{args} = {%_}; - bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response' + bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600), maybe smaxage => $smaxage}, __PACKAGE__.'::Response' } sub forbid { @@ -80,7 +87,7 @@ sub dispatch_request{ sub (/robots.txt) { NOT_FOUND }, sub (/src/:job) { - my $job = job_full $_{job}; + my $job = db->select(jobs => '*', {id => $_{job}})->hash; return NOT_FOUND if !$job; my $isowner = remote_user && remote_user->{id} eq $job->{owner}; my $contest = $job->{contest} && contest_entry $job->{contest}; @@ -97,7 +104,9 @@ sub dispatch_request{ response_filter { my ($r) = @_; return $r if ref $r ne 'Plack::App::Gruntmaster::Response'; - my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}"); + my $smaxage = $r->{smaxage} ? ", s-maxage=$r->{smaxage}" : ''; + my @hdrs = ('Cache-Control' => "$privacy$smaxage, max-age=$r->{maxage}"); + push @hdrs, 'X-Forever' => 1 unless $smaxage; push @hdrs, Vary => 'Authorization' if $privacy eq 'private'; return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json'; my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}; @@ -105,13 +114,16 @@ sub dispatch_request{ }, }, - sub (/st/:contest) { response st => 'Standings', standings($_{contest}), 10 }, + sub (/st/:contest) { + my @pb = map { [$_->{id}, $_->{name}] } sort { $a->{value} <=> $b->{value} } @{problem_list contest => $_{contest}}; + response st => 'Standings', {problems => \@pb, st => standings $_{contest}}, 10 + }, sub (/ed/:contest) { - my $contest = contest_full $_{contest}; - forbid !$contest->{finished}; + my $contest = db->select(contests => '*', {id => $_{contest}})->hash; + forbid time < $contest->{stop}; my $pblist = problem_list contest => $_{contest}, solution => 1; - response ed => 'Editorial of ' . $contest->{name}, {%$pblist, editorial => $contest->{editorial}} + response ed => 'Editorial of ' . $contest->{name}, {pb => $pblist, editorial => $contest->{editorial}} }, sub (/login) { @@ -122,16 +134,23 @@ sub dispatch_request{ sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" }, sub (/us/) { response us => 'Users', {us => user_list} }, - sub (/ct/ + ?:owner~) { response ct => 'Contests', contest_list(%_), 300 }, + sub (/ct/ + ?:owner~) { + my $cts = contest_list(%_); + my $first_event = min + map ({ $_->{start} } grep { !$_->{started} } @$cts), + map ({ $_->{stop} } grep { $_->{started} && !$_->{finished}} @$cts); + response ct => 'Contests', {ct => $cts}, 300, $first_event ? ($first_event - time) : (); + }, sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) { forbid $_{private}; - response log => 'Job list', job_list(%_), 5 + my ($jobs, $pageinfo) = job_list(%_); + response log => 'Job log', {log => $jobs, %$pageinfo}, 5 }, sub (/pb/ + ?:owner~&:contest~&:private~) { forbid $_{private}; my $pending = $_{contest} && !contest_entry($_{contest})->{started}; forbid $pending; - response pb => 'Problems', problem_list %_ + response pb => 'Problems', {pb => problem_list %_} }, sub (/us/:user) { @@ -140,7 +159,10 @@ sub dispatch_request{ }, sub (/ct/:contest) { my $contest = contest_entry $_{contest}; - response ct_entry => $contest->{name}, $contest, 60 + my $smaxage; + $smaxage = $contest->{start} - time if !$contest->{started}; + $smaxage = $contest->{stop} - time if $contest->{started} && !$contest->{finished}; + response ct_entry => $contest->{name}, $contest, 60, $smaxage ? ($smaxage) : () }, sub (/log/:job) { my $job = job_entry $_{job};