]> iEval git - gruntmaster-page.git/blob - lib/Plack/App/Gruntmaster.pm
4872da563fd8506f262c248cf21fa41a88c1a25f
[gruntmaster-page.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 $isowner = remote_user && remote_user->id eq job->rawowner;
88 my $private = job->private || job->problem->private || job->contest && job->contest->is_running;
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 @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
102 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
103 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
104 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
105 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
106 },
107 },
108
109 sub (/st/:contest) {
110 response st => 'Standings', {
111 st => [ contest->standings ],
112 problems => [
113 map { [$_->id, $_->name] }
114 sort { $a->value <=> $b->value }
115 map { $_->problem } contest->contest_problems],
116 }, 10
117 },
118
119 sub (/ed/:contest) {
120 forbid !contest->is_finished;
121 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
122 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : ();
123 },
124
125 sub (/login) {
126 forbid !remote_user;
127 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
128 },
129
130 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
131
132 sub (/us/) { response us => 'Users', {us => db->user_list} },
133 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 },
134 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
135 forbid $_{private};
136 response log => 'Job list', db->job_list(%_), 5
137 },
138 sub (/pb/ + ?:owner~&:contest~&:private~) {
139 forbid $_{private};
140 forbid contest && contest->is_pending;
141 response pb => 'Problems', db->problem_list(%_)
142 },
143
144 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
145 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 },
146 sub (/log/:job) {
147 forbid job->private;
148 response log_entry => "Job $_{job}", db->job_entry($_{job}), 10
149 },
150 sub (/pb/:problem + ?contest~) {
151 my (undef, undef, $contest) = @_;
152 $_{contest} = $contest;
153 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
154 forbid problem->private && !contest;
155 if (contest) {
156 return redirect "/pb/$_{problem}" unless contest->is_running;
157 forbid !remote_user;
158 $privacy = 'private';
159 }
160 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : ();
161 },
162 sub (/sol/:problem) {
163 forbid problem->private;
164 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
165 },
166
167 sub (/) { redispatch_to '/index' },
168 sub (/favicon.ico) { redirect '/static/favicon.ico' },
169 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
170 },
171
172 sub (POST) {
173 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
174 my (undef, undef, $prog) = @_;
175 forbid !remote_user;
176 my $private = (problem->private && !contest) ? 1 : 0;
177 return reply 'This contest has finished' if contest && contest->is_finished;
178 return reply 'This contest has not yet started' if contest && contest->is_pending;
179 return reply 'This problem is private' if !admin && $private;
180 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
181 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
182 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
183 remote_user->update({lastjob => time});
184
185 my $source = $prog ? read_file $prog->path : $_{source_code};
186 unlink $prog->path if $prog;
187 my $newjob = db->jobs->create({
188 maybe contest => $_{contest},
189 private => $private,
190 date => time,
191 extension => FORMAT_EXTENSION->{$_{prog_format}},
192 format => $_{prog_format},
193 problem => $_{problem},
194 source => $source,
195 owner => remote_user->id,
196 });
197
198 [303, [Location => '/log/' . $newjob->id], []]
199 },
200 }
201 }
202
203
204 1;
205 __END__
This page took 0.069331 seconds and 3 git commands to generate.