]> iEval git - gruntmaster-page.git/blame_incremental - lib/Plack/App/Gruntmaster.pm
Use Content-Type: text/plain for all sources
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
... / ...
CommitLineData
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
5our $VERSION = '5999.000_001';
6
7use Encode qw/encode decode/;
8use File::Slurp qw/read_file/;
9use JSON::MaybeXS qw/encode_json/;
10use PerlX::Maybe;
11use Scope::Upper qw/unwind SUB UP/;
12use Web::Simple;
13
14use Gruntmaster::Data;
15use Plack::App::Gruntmaster::HTML;
16
17use warnings NONFATAL => 'all';
18no warnings 'illegalproto';
19
20##################################################
21
22use constant USER_REGEX => qr/^\w{2,20}$/a;
23
24use 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
40use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
41
42my ($env, $privacy);
43
44sub db { $env->{'gruntmaster.dbic'} }
45
46sub remote_user {
47 my $user = $env->{REMOTE_USER};
48 $user &&= db->user($user);
49 $user
50}
51
52sub admin { remote_user && remote_user->admin }
53sub contest { db->contest ($_{contest}) }
54sub problem { db->problem ($_{problem}) }
55sub job { db->job ($_{job}) }
56sub user { db->user ($_{user}) }
57
58sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
59sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
60sub 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
71sub forbid {
72 my ($condition) = @_;
73 $privacy = 'private' if $condition;
74 return if !$condition || admin;
75 unwind $env->{authcomplex}->unauthorized, SUB UP
76}
77
78sub 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 $vary = 'X-Static';
102 $vary .= ', Authorization' if $privacy eq 'private';
103 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}", Vary => $vary);
104 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
105 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}}, maybe static => $env->{HTTP_X_STATIC};
106 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
107 },
108 },
109
110 sub (/st/:contest) {
111 response st => 'Standings', {
112 st => [ contest->standings ],
113 problems => [
114 map { [$_->id, $_->name] }
115 sort { $a->value <=> $b->value }
116 map { $_->problem } contest->contest_problems],
117 }, 10
118 },
119
120 sub (/ed/:contest) {
121 forbid !contest->is_finished;
122 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
123 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : ();
124 },
125
126 sub (/login) {
127 forbid !remote_user;
128 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
129 },
130
131 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
132
133 sub (/us/) { response us => 'Users', {us => db->user_list} },
134 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 },
135 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
136 forbid $_{private};
137 response log => 'Job list', db->job_list(%_), 5
138 },
139 sub (/pb/ + ?:owner~&:contest~&:private~) {
140 forbid $_{private};
141 forbid contest && contest->is_pending;
142 response pb => 'Problems', db->problem_list(%_)
143 },
144
145 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
146 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}), 60 },
147 sub (/log/:job) {
148 forbid job->private;
149 response log_entry => "Job $_{job}", db->job_entry($_{job}), 10
150 },
151 sub (/pb/:problem + ?contest~) {
152 my (undef, undef, $contest) = @_;
153 $_{contest} = $contest;
154 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
155 forbid problem->private && !contest;
156 if (contest) {
157 return redirect "/pb/$_{problem}" unless contest->is_running;
158 forbid !remote_user;
159 $privacy = 'private';
160 }
161 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id), $_{contest} ? 10 : ();
162 },
163 sub (/sol/:problem) {
164 forbid problem->private;
165 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
166 },
167
168 sub (/) { redispatch_to '/index' },
169 sub (/favicon.ico) { redirect '/static/favicon.ico' },
170 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1, Vary => 'X-Static'], [render_article $_{article}, 'en', maybe static => $env->{HTTP_X_STATIC}]] }
171 },
172
173 sub (POST) {
174 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
175 my (undef, undef, $prog) = @_;
176 forbid !remote_user;
177 my $private = (problem->private && !contest) ? 1 : 0;
178 return reply 'This contest has finished' if contest && contest->is_finished;
179 return reply 'This contest has not yet started' if contest && contest->is_pending;
180 return reply 'This problem is private' if !admin && $private;
181 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
182 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
183 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
184 remote_user->update({lastjob => time});
185
186 my $source = $prog ? read_file $prog->path : $_{source_code};
187 unlink $prog->path if $prog;
188 my $newjob = db->jobs->create({
189 maybe contest => $_{contest},
190 private => $private,
191 date => time,
192 extension => FORMAT_EXTENSION->{$_{prog_format}},
193 format => $_{prog_format},
194 problem => $_{problem},
195 source => $source,
196 owner => remote_user->id,
197 });
198
199 [303, [Location => '/log/' . $newjob->id], []]
200 },
201 }
202}
203
204
2051;
206__END__
This page took 0.022388 seconds and 4 git commands to generate.