Autostart/autostop contests via s-maxage
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
7dc32473
MG
5our $VERSION = '5999.000_001';
6
3b69df7a 7use Encode qw/encode decode/;
f34254b8 8use File::Slurp qw/read_file/;
3b69df7a 9use JSON::MaybeXS qw/encode_json/;
6c6fe1b1 10use List::Util qw/min/;
594d53ba
MG
11use PerlX::Maybe;
12use Scope::Upper qw/unwind SUB UP/;
3b69df7a 13use Web::Simple;
594d53ba
MG
14
15use Gruntmaster::Data;
3b69df7a
MG
16use Plack::App::Gruntmaster::HTML;
17
18use warnings NONFATAL => 'all';
19no warnings 'illegalproto';
f34254b8 20
594d53ba
MG
21##################################################
22
594d53ba
MG
23use constant USER_REGEX => qr/^\w{2,20}$/a;
24
594d53ba
MG
25use constant FORMAT_EXTENSION => {
26 C => 'c',
27 CPP => 'cpp',
a5e355d6
MG
28 GCCGO => 'go',
29 GOLANG => 'go',
d39978bf 30 GOLFSCRIPT => 'gs',
a5e355d6 31 HASKELL => 'hs',
594d53ba
MG
32 MONO => 'cs',
33 JAVA => 'java',
34 PASCAL => 'pas',
35 PERL => 'pl',
36 PYTHON => 'py',
c83dca87 37 RUBY => 'rb',
e6d1bcd4 38 SBCL => 'l',
594d53ba
MG
39};
40
3ef32174 41use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
594d53ba 42
c039d63e 43my ($env, $privacy);
594d53ba 44
594d53ba 45sub remote_user {
2a06b02c
MG
46 unless ($env->{'gruntmaster.user'}) {
47 my $user = $env->{REMOTE_USER};
1f64ef28 48 $user &&= user_entry $user;
2a06b02c
MG
49 $env->{'gruntmaster.user'} = $user;
50 }
51 $env->{'gruntmaster.user'}
594d53ba
MG
52}
53
c5343878 54sub admin { remote_user && remote_user->{admin} }
594d53ba 55
54bc2c5c 56sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
69c01de9 57sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
594d53ba 58sub response {
6c6fe1b1 59 my ($template, $title, $params, $maxage, $smaxage) = @_;
594d53ba
MG
60 unless ($params) {
61 $params = $title;
62 $title = 'No title';
63 }
9a4806b3 64 $params->{time} = time;
39e2d01a 65 $params->{args} = {%_};
6c6fe1b1 66 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600), maybe smaxage => $smaxage}, __PACKAGE__.'::Response'
594d53ba 67}
3b69df7a 68
c039d63e
MG
69sub forbid {
70 my ($condition) = @_;
71 $privacy = 'private' if $condition;
72 return if !$condition || admin;
8d725691 73 unwind $env->{authcomplex}->unauthorized, SUB UP
594d53ba 74}
31d70015 75
594d53ba
MG
76sub dispatch_request{
77 $env = $_[PSGI_ENV];
c039d63e
MG
78 $privacy = 'public';
79
594d53ba 80 sub (GET) {
3ef32174 81 sub (/robots.txt) { NOT_FOUND },
3ef32174 82
594d53ba 83 sub (/src/:job) {
25ed3a34 84 my $job = db->select(jobs => '*', {id => $_{job}})->hash;
c5343878
MG
85 return NOT_FOUND if !$job;
86 my $isowner = remote_user && remote_user->{id} eq $job->{owner};
1f64ef28 87 my $contest = $job->{contest} && contest_entry $job->{contest};
02d17b02 88 my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
e547b147
MG
89 forbid !$isowner && $private;
90 my $privacy = $private ? 'private' : 'public';
218d6f7e 91 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
e547b147 92 push @headers, (Vary => 'Authorization') if $private;
02d17b02 93 [200, \@headers, [$job->{source}]]
594d53ba
MG
94 },
95
d3892d73 96 sub (?:format~) {
feaa8f5a 97 my $format = lc ($_{format} // '');
594d53ba
MG
98 response_filter {
99 my ($r) = @_;
100 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
6c6fe1b1
MG
101 my $smaxage = $r->{smaxage} ? ", s-maxage=$r->{smaxage}" : '';
102 my @hdrs = ('Cache-Control' => "$privacy$smaxage, max-age=$r->{maxage}");
103 push @hdrs, 'X-Forever' => 1 unless $smaxage;
928a611f 104 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
69c01de9 105 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
928a611f 106 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
69c01de9 107 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
108 },
109 },
110
25ed3a34 111 sub (/st/:contest) {
cf22c656 112 my @pb = map { [$_->{id}, $_->{name}] } sort { $a->{value} <=> $b->{value} } @{problem_list contest => $_{contest}};
25ed3a34
MG
113 response st => 'Standings', {problems => \@pb, st => standings $_{contest}}, 10
114 },
594d53ba 115
645cfb7d 116 sub (/ed/:contest) {
25ed3a34
MG
117 my $contest = db->select(contests => '*', {id => $_{contest}})->hash;
118 forbid time < $contest->{stop};
1f64ef28 119 my $pblist = problem_list contest => $_{contest}, solution => 1;
25ed3a34 120 response ed => 'Editorial of ' . $contest->{name}, {pb => $pblist, editorial => $contest->{editorial}}
645cfb7d
MG
121 },
122
462db4aa
MG
123 sub (/login) {
124 forbid !remote_user;
2beb67b4 125 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
462db4aa
MG
126 },
127
594d53ba
MG
128 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
129
1f64ef28 130 sub (/us/) { response us => 'Users', {us => user_list} },
6c6fe1b1
MG
131 sub (/ct/ + ?:owner~) {
132 my $cts = contest_list(%_);
133 my $first_event = min
134 map ({ $_->{start} } grep { !$_->{started} } @$cts),
135 map ({ $_->{stop} } grep { $_->{started} && !$_->{finished}} @$cts);
136 response ct => 'Contests', {ct => $cts}, 300, $first_event ? ($first_event - time) : ();
137 },
23e32638 138 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
b8a0fa71 139 forbid $_{private};
25ed3a34 140 my ($jobs, $pageinfo) = job_list(%_);
ea6cce4c 141 response log => 'Job log', {log => $jobs, %$pageinfo}, 5
b8a0fa71 142 },
87ffd88b 143 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71 144 forbid $_{private};
1f64ef28 145 my $pending = $_{contest} && !contest_entry($_{contest})->{started};
a18eef28 146 forbid $pending;
25ed3a34 147 response pb => 'Problems', {pb => problem_list %_}
b8a0fa71 148 },
594d53ba 149
c5343878 150 sub (/us/:user) {
1f64ef28 151 my $user = user_entry $_{user};
c5343878
MG
152 response us_entry => $user->{name}, $user
153 },
154 sub (/ct/:contest) {
1f64ef28 155 my $contest = contest_entry $_{contest};
6c6fe1b1
MG
156 my $smaxage;
157 $smaxage = $contest->{start} - time if !$contest->{started};
158 $smaxage = $contest->{stop} - time if $contest->{started} && !$contest->{finished};
159 response ct_entry => $contest->{name}, $contest, 60, $smaxage ? ($smaxage) : ()
c5343878 160 },
28e89d6c 161 sub (/log/:job) {
1f64ef28 162 my $job = job_entry $_{job};
c5343878
MG
163 forbid $job->{private};
164 response log_entry => "Job $_{job}", $job, 10
28e89d6c 165 },
84ca7535
MG
166 sub (/pb/:problem + ?contest~) {
167 my (undef, undef, $contest) = @_;
168 $_{contest} = $contest;
1f64ef28
MG
169 $contest = $contest && contest_entry $_{contest};
170 return NOT_FOUND if $contest && !contest_has_problem $_{contest}, $_{problem};
171 my $problem = problem_entry $_{problem}, $_{contest};
a18eef28
MG
172 forbid $problem->{private} && !$contest;
173 if ($contest) {
174 return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished};
5b76a57d
MG
175 forbid !remote_user;
176 $privacy = 'private';
177 }
a18eef28 178 response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : ();
594d53ba 179 },
e4d5bdf5 180 sub (/sol/:problem) {
1f64ef28 181 my $problem = problem_entry $_{problem};
a18eef28
MG
182 forbid $problem->{private};
183 response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}};
e4d5bdf5 184 },
594d53ba
MG
185
186 sub (/) { redispatch_to '/index' },
cb0122d7 187 sub (/favicon.ico) { redirect '/static/favicon.ico' },
928a611f 188 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
189 },
190
191 sub (POST) {
08794667
MG
192 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
193 my (undef, undef, $prog) = @_;
594d53ba 194 forbid !remote_user;
1f64ef28 195 my $problem = problem_entry $_{problem};
a18eef28
MG
196 my $private = $problem->{private} ? 1 : 0;
197 if ($_{contest}) {
198 $private = 0;
1f64ef28 199 my $contest = contest_entry $_{contest};
a18eef28
MG
200 return reply 'This contest has not yet started' if !$contest->{started};
201 return reply 'This contest has finished' if $contest->{finished};
202 return reply 'This problem is private' if !admin && $private;
1f64ef28 203 return reply 'This problem does not belong to this contest' unless contest_has_problem $_{contest}, $_{problem};
a18eef28 204 }
08794667 205 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
c5343878 206 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
594d53ba 207
08794667
MG
208 my $source = $prog ? read_file $prog->path : $_{source_code};
209 unlink $prog->path if $prog;
1f64ef28 210 my $id = create_job(
594d53ba 211 maybe contest => $_{contest},
cb6adaff 212 private => $private,
594d53ba
MG
213 date => time,
214 extension => FORMAT_EXTENSION->{$_{prog_format}},
215 format => $_{prog_format},
216 problem => $_{problem},
08794667 217 source => $source,
c5343878
MG
218 owner => remote_user->{id},
219 );
594d53ba 220
c5343878 221 [303, [Location => '/log/' . $id], []]
3c434a02 222 },
594d53ba 223 }
7dc32473
MG
224}
225
594d53ba 226
7dc32473
MG
2271;
228__END__
This page took 0.049132 seconds and 4 git commands to generate.