Add GOLANG, GCCGO, HASKELL
[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 CSS::Minifier::XS;
8 use Encode qw/encode decode/;
9 use File::Slurp qw/read_file/;
10 use JavaScript::Minifier::XS;
11 use JSON::MaybeXS qw/encode_json/;
12 use PerlX::Maybe;
13 use Scope::Upper qw/unwind SUB UP/;
14 use Web::Simple;
15
16 use Gruntmaster::Data;
17 use Plack::App::Gruntmaster::HTML;
18
19 use warnings NONFATAL => 'all';
20 no warnings 'illegalproto';
21 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
22
23 ##################################################
24
25 use constant USER_REGEX => qr/^\w{2,20}$/a;
26
27 use constant CONTENT_TYPES => +{
28 c => 'text/x-csrc',
29 cpp => 'text/x-c++src',
30 cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
31 go => 'text/plain', # ?
32 hs => 'text/x-haskell',
33 java => 'text/x-java',
34 pas => 'text/x-pascal',
35 pl => 'text/x-perl',
36 py => 'text/x-python',
37 };
38
39 use constant FORMAT_EXTENSION => {
40 C => 'c',
41 CPP => 'cpp',
42 GCCGO => 'go',
43 GOLANG => 'go',
44 HASKELL => 'hs',
45 MONO => 'cs',
46 JAVA => 'java',
47 PASCAL => 'pas',
48 PERL => 'pl',
49 PYTHON => 'py',
50 };
51
52 use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']];
53 use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
54
55 my $env;
56
57 sub db { $env->{'gruntmaster.dbic'} }
58
59 sub remote_user {
60 my $user = $env->{'gruntmaster.user'};
61 $user &&= db->user($user);
62 $user
63 }
64
65 sub admin { remote_user && remote_user->admin }
66 sub contest { db->contest ($_{contest}) }
67 sub problem { db->problem ($_{problem}) }
68 sub job { db->job ($_{job}) }
69 sub user { db->user ($_{user}) }
70
71 sub redirect { [301, ['X-Forever' => 1, 'Location' => $_[0]], []] }
72 sub reply { [200, ['Content-Type' => 'text/plain'], \@_] }
73 sub response {
74 my ($template, $title, $params) = @_;
75 unless ($params) {
76 $params = $title;
77 $title = 'No title';
78 }
79 bless {template => $template, title => $title, params => $params}, __PACKAGE__.'::Response'
80 }
81
82 sub forbid {
83 return if !shift || admin;
84 unwind FORBIDDEN, SUB UP
85 }
86
87 sub dispatch_request{
88 $env = $_[PSGI_ENV];
89 sub (GET) {
90 sub (/css/:theme) {
91 my $theme = $_{theme};
92 return NOT_FOUND unless -e "css/themes/$theme.css";
93 my $css = read_file "css/themes/$theme.css";
94 $css .= read_file $_ for <css/*.css>;
95 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
96 [200, \@headers, [CSS::Minifier::XS::minify $css]]
97 },
98
99 sub (/js.js) {
100 my $js;
101 $js .= read_file $_ for <js/*.js>;
102 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
103 [200, \@headers, [JavaScript::Minifier::XS::minify $js]]
104 },
105
106 sub (/src/:job) {
107 return NOT_FOUND if !job;
108 forbid job->private || job->problem->private || job->contest && job->contest->private;
109 my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => CONTENT_TYPES->{job->format});
110 [200, \@headers, [job->source]]
111 },
112
113 sub (?:contest=) {
114 return NOT_FOUND if !contest;
115 forbid contest->is_pending;
116 response_filter { return shift }
117 },
118
119 sub (?:problem=) {
120 return NOT_FOUND if !problem;
121 forbid problem->is_private;
122 response_filter { return shift }
123 },
124
125 sub () {
126 response_filter {
127 my ($r) = @_;
128 return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
129 return [200, ['Content-Type' => 'application/json', 'X-Forever' => 1], [encode 'UTF-8', encode_json $r->{params}]] if $env->{HTTP_ACCEPT} =~ m,^\s*application/json\s*$,g;
130 my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
131 [200, ['Content-Type' => 'text/html'], [encode 'UTF-8', $ret]]
132 },
133 },
134
135 sub (/st/) {
136 response st => 'Standings', { st => [db->standings] }
137 },
138
139 sub (/st/:contest) {
140 response st => 'Standings', {
141 st => [ db->standings($_{contest}) ],
142 problems => [map { $_->problem } contest->contest_problems]
143 }
144 },
145
146 sub (/ct/:contest/log/st) { redirect "/st/$_{contest}" },
147
148 sub (/us/) { response us => 'Users', {us => db->user_list} },
149 sub (/ct/ + ?:owner~) { response ct => 'Contests', db->contest_list(%_) },
150 sub (/log/ + ?:contest~&:owner~&:page~&:problem~) { response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest}} },
151 sub (/pb/ + ?:owner~&:contest~) { response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}} },
152
153 sub (/us/:user) { response us_entry => user->name, db->user_entry($_{user}) },
154 sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
155 sub (/log/:job) { response log_entry => "Job $_{job}", db->job_entry($_{job}) },
156 sub (/pb/:problem + ?:contest~) {
157 return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
158 forbid problem->is_private;
159 response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id);
160 },
161
162 sub (/) { redispatch_to '/index' },
163 sub (/:article) { [200, ['Content-Type' => 'text/html'], [render_article $_{article}, 'en']] }
164 },
165
166 sub (POST) {
167 sub (/action/register + %:username=&:password=&:confirm_password=&:name=&:email=&:phone=&:town=&:university=&:level=) {
168 return reply 'Parameter too long' if grep { length > 200 } values %_;
169 return reply 'Bad username. Allowed characters are letters, digits and underscores, and the username must be between 2 and 20 characters long.' unless $_{username} =~ USER_REGEX;
170 return reply 'Username already in use' if db->user($_{username});
171 return reply 'The two passwords do not match' unless $_{password} eq $_{confirm_password};
172
173 db->users->create({id => $_{username}, name => $_{name}, email => $_{email}, phone => $_{phone}, town => $_{town}, university => $_{university}, level => $_{level}});
174 db->user($_{username})->set_passphrase($_{password});
175
176 reply 'Registered successfully';
177 },
178
179 sub (/action/passwd + %:password=&:new_password=&:confirm_new_password=) {
180 forbid !remote_user;
181 return reply 'Incorrect password' unless remote_user->check_passphrase($_{password});
182 return reply 'The two passwords do not match' unless $_{new_password} eq $_{confirm_new_password};
183 remote_user->set_passphrase($_{new_password});
184 reply 'Password changed successfully';
185 },
186
187 sub (/action/submit + %:problem=&:contest~&:prog_format=&:private~&:source_code~ + *:prog~) {
188 forbid !remote_user;
189 return reply 'This contest has finished' if contest && contest->is_finished;
190 return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
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 remote_user->update({lastjob => time});
194
195 my $prog = $_{prog} ? read_file $_{prog}->path : $_{source_code};
196 unlink $_{prog}->path if $_{prog};
197 db->jobs->create({
198 maybe contest => $_{contest},
199 maybe private => $_{private},
200 date => time,
201 extension => FORMAT_EXTENSION->{$_{prog_format}},
202 format => $_{prog_format},
203 problem => $_{problem},
204 source => $prog,
205 owner => remote_user->id,
206 });
207
208 redirect $_{contest} ? "/log/?contest=$_{contest}" : '/log/';
209 }
210 }
211 }
212
213
214 1;
215 __END__
This page took 0.040329 seconds and 4 git commands to generate.