Cache remote_user per-request
[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 unless ($env->{'gruntmaster.user'}) {
48 my $user = $env->{REMOTE_USER};
49 $user &&= db->user_entry($user);
50 $env->{'gruntmaster.user'} = $user;
51 }
52 $env->{'gruntmaster.user'}
53 }
54
55 sub admin { remote_user && remote_user->{admin} }
56 sub contest { db->contest ($_{contest}) }
57 sub problem { db->problem ($_{problem}) }
58
59 sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
60 sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
61 sub response {
62 my ($template, $title, $params, $maxage) = @_;
63 unless ($params) {
64 $params = $title;
65 $title = 'No title';
66 }
67 $params->{time} = time;
68 $params->{args} = {%_};
69 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response'
70 }
71
72 sub forbid {
73 my ($condition) = @_;
74 $privacy = 'private' if $condition;
75 return if !$condition || admin;
76 unwind $env->{authcomplex}->unauthorized, SUB UP
77 }
78
79 sub dispatch_request{
80 $env = $_[PSGI_ENV];
81 $privacy = 'public';
82
83 sub (GET) {
84 sub (/robots.txt) { NOT_FOUND },
85
86 sub (/src/:job) {
87 my $job = db->job_full($_{job});
88 return NOT_FOUND if !$job;
89 my $isowner = remote_user && remote_user->{id} eq $job->{owner};
90 my $contest = $job->{contest} && db->contest_entry($job->{contest});
91 my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
92 forbid !$isowner && $private;
93 my $privacy = $private ? 'private' : 'public';
94 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
95 push @headers, (Vary => 'Authorization') if $private;
96 [200, \@headers, [$job->{source}]]
97 },
98
99 sub (?:format~) {
100 my $format = lc ($_{format} // '');
101 response_filter {
102 my ($r) = @_;
103 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
104 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
105 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
106 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
107 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
108 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
109 },
110 },
111
112 sub (/st/:contest) {
113 response st => 'Standings', {
114 st => [ contest->standings ],
115 problems => [
116 map { [$_->id, $_->name] }
117 sort { $a->value <=> $b->value }
118 map { $_->problem } contest->contest_problems],
119 }, 10
120 },
121
122 sub (/ed/:contest) {
123 forbid !contest->is_finished;
124 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
125 response ed => 'Editorial of ' . contest->name, {%$pblist, editorial => contest->editorial}, contest->is_finished(time - 86400) ? 60 : ();
126 },
127
128 sub (/login) {
129 forbid !remote_user;
130 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
131 },
132
133 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
134
135 sub (/us/) { response us => 'Users', {us => db->user_list} },
136 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 },
137 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
138 forbid $_{private};
139 response log => 'Job list', db->job_list(%_), 5
140 },
141 sub (/pb/ + ?:owner~&:contest~&:private~) {
142 forbid $_{private};
143 forbid contest && contest->is_pending;
144 response pb => 'Problems', db->problem_list(%_)
145 },
146
147 sub (/us/:user) {
148 my $user = db->user_entry($_{user});
149 response us_entry => $user->{name}, $user
150 },
151 sub (/ct/:contest) {
152 my $contest = db->contest_entry($_{contest});
153 response ct_entry => $contest->{name}, $contest, 60
154 },
155 sub (/log/:job) {
156 my $job = db->job_entry($_{job});
157 forbid $job->{private};
158 response log_entry => "Job $_{job}", $job, 10
159 },
160 sub (/pb/:problem + ?contest~) {
161 my (undef, undef, $contest) = @_;
162 $_{contest} = $contest;
163 return NOT_FOUND if contest && !db->contest_problems->find($_{contest}, $_{problem});
164 forbid problem->private && !contest;
165 if (contest) {
166 return redirect "/pb/$_{problem}" unless contest->is_running;
167 forbid !remote_user;
168 $privacy = 'private';
169 }
170 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->{id}), $_{contest} ? 10 : ();
171 },
172 sub (/sol/:problem) {
173 forbid problem->private;
174 response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
175 },
176
177 sub (/) { redispatch_to '/index' },
178 sub (/favicon.ico) { redirect '/static/favicon.ico' },
179 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
180 },
181
182 sub (POST) {
183 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
184 my (undef, undef, $prog) = @_;
185 forbid !remote_user;
186 my $private = (problem->private && !contest) ? 1 : 0;
187 return reply 'This contest has finished' if contest && contest->is_finished;
188 return reply 'This contest has not yet started' if contest && contest->is_pending;
189 return reply 'This problem is private' if !admin && $private;
190 return reply 'This problem does not belong to this contest' if contest && !db->contest_problems->find($_{contest}, $_{problem});
191 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
192 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
193
194 my $source = $prog ? read_file $prog->path : $_{source_code};
195 unlink $prog->path if $prog;
196 my $id = db->create_job(
197 maybe contest => $_{contest},
198 private => $private,
199 date => time,
200 extension => FORMAT_EXTENSION->{$_{prog_format}},
201 format => $_{prog_format},
202 problem => $_{problem},
203 source => $source,
204 owner => remote_user->{id},
205 );
206
207 [303, [Location => '/log/' . $id], []]
208 },
209 }
210 }
211
212
213 1;
214 __END__
This page took 0.037016 seconds and 4 git commands to generate.