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