]> iEval git - gruntmaster-page.git/blob - lib/Plack/App/Gruntmaster.pm
1af76d0c14072c2696922b3d892e9034fc636df6
[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_entry($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
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) = @_;
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)}, __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->job_full($_{job});
85 return NOT_FOUND if !$job;
86 my $isowner = remote_user && remote_user->{id} eq $job->{owner};
87 my $contest = $job->{contest} && db->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 @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) {
145 my $user = db->user_entry($_{user});
146 response us_entry => $user->{name}, $user
147 },
148 sub (/ct/:contest) {
149 my $contest = db->contest_entry($_{contest});
150 response ct_entry => $contest->{name}, $contest, 60
151 },
152 sub (/log/:job) {
153 my $job = db->job_entry($_{job});
154 forbid $job->{private};
155 response log_entry => "Job $_{job}", $job, 10
156 },
157 sub (/pb/:problem + ?contest~) {
158 my (undef, undef, $contest) = @_;
159 $_{contest} = $contest;
160 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
161 forbid problem->private && !contest;
162 if (contest) {
163 return redirect "/pb/$_{problem}" unless contest->is_running;
164 forbid !remote_user;
165 $privacy = 'private';
166 }
167 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->{id}), $_{contest} ? 10 : ();
168 },
169 sub (/sol/:problem) {
170 forbid problem->private;
171 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
172 },
173
174 sub (/) { redispatch_to '/index' },
175 sub (/favicon.ico) { redirect '/static/favicon.ico' },
176 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
177 },
178
179 sub (POST) {
180 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
181 my (undef, undef, $prog) = @_;
182 forbid !remote_user;
183 my $private = (problem->private && !contest) ? 1 : 0;
184 return reply 'This contest has finished' if contest && contest->is_finished;
185 return reply 'This contest has not yet started' if contest && contest->is_pending;
186 return reply 'This problem is private' if !admin && $private;
187 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
188 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
189 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
190
191 my $source = $prog ? read_file $prog->path : $_{source_code};
192 unlink $prog->path if $prog;
193 my $id = db->create_job(
194 maybe contest => $_{contest},
195 private => $private,
196 date => time,
197 extension => FORMAT_EXTENSION->{$_{prog_format}},
198 format => $_{prog_format},
199 problem => $_{problem},
200 source => $source,
201 owner => remote_user->{id},
202 );
203
204 [303, [Location => '/log/' . $id], []]
205 },
206 }
207 }
208
209
210 1;
211 __END__
This page took 0.070082 seconds and 4 git commands to generate.