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