Remove any dependency on DBIC
[plack-app-gruntmaster.git] / lib / Plack / App / Gruntmaster.pm
CommitLineData
7dc32473
MG
1package Plack::App::Gruntmaster;
2
3use 5.014000;
4use strict;
7dc32473
MG
5our $VERSION = '5999.000_001';
6
3b69df7a 7use Encode qw/encode decode/;
f34254b8 8use File::Slurp qw/read_file/;
3b69df7a 9use JSON::MaybeXS qw/encode_json/;
594d53ba
MG
10use PerlX::Maybe;
11use Scope::Upper qw/unwind SUB UP/;
3b69df7a 12use Web::Simple;
594d53ba
MG
13
14use Gruntmaster::Data;
3b69df7a
MG
15use Plack::App::Gruntmaster::HTML;
16
17use warnings NONFATAL => 'all';
18no warnings 'illegalproto';
f34254b8 19
594d53ba
MG
20##################################################
21
594d53ba
MG
22use constant USER_REGEX => qr/^\w{2,20}$/a;
23
594d53ba
MG
24use constant FORMAT_EXTENSION => {
25 C => 'c',
26 CPP => 'cpp',
a5e355d6
MG
27 GCCGO => 'go',
28 GOLANG => 'go',
d39978bf 29 GOLFSCRIPT => 'gs',
a5e355d6 30 HASKELL => 'hs',
594d53ba
MG
31 MONO => 'cs',
32 JAVA => 'java',
33 PASCAL => 'pas',
34 PERL => 'pl',
35 PYTHON => 'py',
c83dca87 36 RUBY => 'rb',
e6d1bcd4 37 SBCL => 'l',
594d53ba
MG
38};
39
3ef32174 40use constant NOT_FOUND => [404, ['X-Forever' => 1, 'Content-Type' => 'text/plain'], ['Not found']];
594d53ba 41
c039d63e 42my ($env, $privacy);
594d53ba
MG
43
44sub db { $env->{'gruntmaster.dbic'} }
45
46sub remote_user {
2a06b02c
MG
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'}
594d53ba
MG
53}
54
c5343878 55sub admin { remote_user && remote_user->{admin} }
594d53ba 56
54bc2c5c 57sub redirect { [301, ['X-Forever' => 1, 'Cache-Control' => 'public, max-age=86400', 'Location' => $_[0]], []] }
69c01de9 58sub reply { [200, ['Content-Type' => 'text/plain; charset=utf-8'], \@_] }
594d53ba 59sub response {
c039d63e 60 my ($template, $title, $params, $maxage) = @_;
594d53ba
MG
61 unless ($params) {
62 $params = $title;
63 $title = 'No title';
64 }
9a4806b3 65 $params->{time} = time;
39e2d01a 66 $params->{args} = {%_};
54bc2c5c 67 bless {template => $template, title => $title, params => $params, maxage => ($maxage // 3600)}, __PACKAGE__.'::Response'
594d53ba 68}
3b69df7a 69
c039d63e
MG
70sub forbid {
71 my ($condition) = @_;
72 $privacy = 'private' if $condition;
73 return if !$condition || admin;
8d725691 74 unwind $env->{authcomplex}->unauthorized, SUB UP
594d53ba 75}
31d70015 76
594d53ba
MG
77sub dispatch_request{
78 $env = $_[PSGI_ENV];
c039d63e
MG
79 $privacy = 'public';
80
594d53ba 81 sub (GET) {
3ef32174 82 sub (/robots.txt) { NOT_FOUND },
3ef32174 83
594d53ba 84 sub (/src/:job) {
02d17b02 85 my $job = db->job_full($_{job});
c5343878
MG
86 return NOT_FOUND if !$job;
87 my $isowner = remote_user && remote_user->{id} eq $job->{owner};
02d17b02
MG
88 my $contest = $job->{contest} && db->contest_entry($job->{contest});
89 my $private = $job->{private} || $contest && ($contest->{started} && !$contest->{finished});
e547b147
MG
90 forbid !$isowner && $private;
91 my $privacy = $private ? 'private' : 'public';
218d6f7e 92 my @headers = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=604800", 'Content-Type' => 'text/plain');
e547b147 93 push @headers, (Vary => 'Authorization') if $private;
02d17b02 94 [200, \@headers, [$job->{source}]]
594d53ba
MG
95 },
96
d3892d73 97 sub (?:format~) {
feaa8f5a 98 my $format = lc ($_{format} // '');
594d53ba
MG
99 response_filter {
100 my ($r) = @_;
101 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
928a611f
MG
102 my @hdrs = ('X-Forever' => 1, 'Cache-Control' => "$privacy, max-age=$r->{maxage}");
103 push @hdrs, Vary => 'Authorization' if $privacy eq 'private';
69c01de9 104 return [200, ['Content-Type' => 'application/json; charset=utf-8', @hdrs], [encode_json $r->{params}]] if $format eq 'json';
928a611f 105 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
69c01de9 106 [200, ['Content-Type' => 'text/html; charset=utf-8', @hdrs], [encode 'UTF-8', $ret]]
594d53ba
MG
107 },
108 },
109
a18eef28 110 sub (/st/:contest) { response st => 'Standings', db->standings($_{contest}), 10 },
594d53ba 111
645cfb7d 112 sub (/ed/:contest) {
a18eef28
MG
113 my $contest = db->contest_full($_{contest});
114 forbid !$contest->{finished};
42243f04 115 my $pblist = db->problem_list(contest => $_{contest}, solution => 1);
a18eef28 116 response ed => 'Editorial of ' . $contest->{name}, {%$pblist, editorial => $contest->{editorial}}
645cfb7d
MG
117 },
118
462db4aa
MG
119 sub (/login) {
120 forbid !remote_user;
2beb67b4 121 [200, ['Content-Type' => 'text/plain; charset=UTF-8', 'Cache-Control' => 'private, max-age=300', Vary => 'Authorization'], [$env->{REMOTE_USER}]]
462db4aa
MG
122 },
123
594d53ba
MG
124 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
125
126 sub (/us/) { response us => 'Users', {us => db->user_list} },
54bc2c5c 127 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_), 300 },
23e32638 128 sub (/log/ + ?:contest~&:owner~&:page~&:problem~&:private~&:result~) {
b8a0fa71 129 forbid $_{private};
7e158d3c 130 response log => 'Job list', db->job_list(%_), 5
b8a0fa71 131 },
87ffd88b 132 sub (/pb/ + ?:owner~&:contest~&:private~) {
b8a0fa71 133 forbid $_{private};
a18eef28
MG
134 my $pending = $_{contest} && !db->contest_entry($_{contest})->{started};
135 forbid $pending;
ee38c972 136 response pb => 'Problems', db->problem_list(%_)
b8a0fa71 137 },
594d53ba 138
c5343878
MG
139 sub (/us/:user) {
140 my $user = db->user_entry($_{user});
141 response us_entry => $user->{name}, $user
142 },
143 sub (/ct/:contest) {
144 my $contest = db->contest_entry($_{contest});
145 response ct_entry => $contest->{name}, $contest, 60
146 },
28e89d6c 147 sub (/log/:job) {
c5343878
MG
148 my $job = db->job_entry($_{job});
149 forbid $job->{private};
150 response log_entry => "Job $_{job}", $job, 10
28e89d6c 151 },
84ca7535
MG
152 sub (/pb/:problem + ?contest~) {
153 my (undef, undef, $contest) = @_;
154 $_{contest} = $contest;
a18eef28
MG
155 $contest = $contest && db->contest_entry($_{contest});
156 return NOT_FOUND if $contest && !db->contest_has_problem($_{contest}, $_{problem});
157 my $problem = db->problem_entry($_{problem}, $_{contest});
158 forbid $problem->{private} && !$contest;
159 if ($contest) {
160 return redirect "/pb/$_{problem}" if !$contest->{started} || $contest->{finished};
5b76a57d
MG
161 forbid !remote_user;
162 $privacy = 'private';
163 }
a18eef28 164 response pb_entry => $problem->{name}, $problem, $_{contest} ? 10 : ();
594d53ba 165 },
e4d5bdf5 166 sub (/sol/:problem) {
a18eef28
MG
167 my $problem = db->problem_entry($_{problem});
168 forbid $problem->{private};
169 response sol => 'Solution of ' . $problem->{name}, {solution => $problem->{solution}};
e4d5bdf5 170 },
594d53ba
MG
171
172 sub (/) { redispatch_to '/index' },
cb0122d7 173 sub (/favicon.ico) { redirect '/static/favicon.ico' },
928a611f 174 sub (/:article) { [200, ['Content-Type' => 'text/html; charset=utf-8', 'Cache-Control' => 'public, max-age=3600', 'X-Forever' => 1], [render_article $_{article}, 'en']] }
594d53ba
MG
175 },
176
177 sub (POST) {
08794667
MG
178 sub (/action/submit + %:problem=&:contest~&:prog_format=&:source_code~ + *prog~) {
179 my (undef, undef, $prog) = @_;
594d53ba 180 forbid !remote_user;
a18eef28
MG
181 my $problem = db->problem_entry($_{problem});
182 my $private = $problem->{private} ? 1 : 0;
183 if ($_{contest}) {
184 $private = 0;
185 my $contest = db->contest_entry($_{contest});
186 return reply 'This contest has not yet started' if !$contest->{started};
187 return reply 'This contest has finished' if $contest->{finished};
188 return reply 'This problem is private' if !admin && $private;
189 return reply 'This problem does not belong to this contest' unless db->contest_has_problem($_{contest}, $_{problem});
190 }
08794667 191 return reply 'Maximum source size is 10KB' if ($prog ? $prog->size : length $_{source_code}) > 10 * 1024;
c5343878 192 return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->{lastjob} + 30;
594d53ba 193
08794667
MG
194 my $source = $prog ? read_file $prog->path : $_{source_code};
195 unlink $prog->path if $prog;
c5343878 196 my $id = db->create_job(
594d53ba 197 maybe contest => $_{contest},
cb6adaff 198 private => $private,
594d53ba
MG
199 date => time,
200 extension => FORMAT_EXTENSION->{$_{prog_format}},
201 format => $_{prog_format},
202 problem => $_{problem},
08794667 203 source => $source,
c5343878
MG
204 owner => remote_user->{id},
205 );
594d53ba 206
c5343878 207 [303, [Location => '/log/' . $id], []]
3c434a02 208 },
594d53ba 209 }
7dc32473
MG
210}
211
594d53ba 212
7dc32473
MG
2131;
214__END__
This page took 0.046725 seconds and 4 git commands to generate.