]> iEval git - gruntmaster-page.git/blobdiff - lib/Plack/App/Gruntmaster.pm
Remove archive standings
[gruntmaster-page.git] / lib / Plack / App / Gruntmaster.pm
index 3c989ac10fe533058e1b3d12bcf219d68d7ffcb3..4a89edab6ef3f34a3bbd3f48d023f2d53eada852 100644 (file)
@@ -4,59 +4,32 @@ use 5.014000;
 use strict;
 our $VERSION = '5999.000_001';
 
-use Apache2::Authen::Passphrase qw/pwcheck pwset/;
 use CSS::Minifier::XS;
+use Encode qw/encode decode/;
 use File::Slurp qw/read_file/;
 use JavaScript::Minifier::XS;
-
-use HTML::Template::Compiled;
+use JSON::MaybeXS qw/encode_json/;
 use PerlX::Maybe;
 use Scope::Upper qw/unwind SUB UP/;
+use Web::Simple;
 
 use Gruntmaster::Data;
-use Web::Simple;
-no warnings FATAL => 'all';
-use warnings;
-no warnings::illegalproto;
+use Plack::App::Gruntmaster::HTML;
+
+use warnings NONFATAL => 'all';
+no warnings 'illegalproto';
 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
 
 ##################################################
 
-sub read_templates {
-       my $name = shift;
-
-       my %tmpl = map { m/\.(.+)$/; $1 => scalar read_file $_ } <tmpl/$name.*>;
-       my %arti = map { m/\.(.+)$/; $1 => scalar read_file $_ } <a/$name.*>;
-       return %tmpl ? %tmpl : %arti
-}
-
-my %header_templates = read_templates 'header';
-my %footer_templates = read_templates 'footer';
-my %templates;
-
-sub render {
-       my ($tmpl, $title, %params) = @_;
-       unless ($templates{$tmpl}) {
-               $templates{$tmpl} = { read_templates $tmpl };
-               for my $lang (keys $templates{$tmpl}) {
-                       my $header = $header_templates{$lang} =~ s/TITLE_GOES_HERE/$title/rg;
-                       $templates{$tmpl}{$lang} = $header . $templates{$tmpl}{$lang};
-               }
-               $templates{$tmpl}{$_} .= $footer_templates{$_} for keys $templates{$tmpl};
-
-       }
-
-       my $htc = HTML::Template::Compiled->new(scalarref => \$templates{$tmpl}{en}, default_escape => 'HTML', use_perl => 1);
-       $htc->param(%params);
-       [200, ['Content-Type' => 'text/html'], [$htc->output]]
-}
-
 use constant USER_REGEX => qr/^\w{2,20}$/a;
 
 use constant CONTENT_TYPES => +{
        c => 'text/x-csrc',
        cpp => 'text/x-c++src',
        cs => 'text/x-csharp', # Used by GNOME. Not in mime.types.
+       go => 'text/plain', # ?
+       hs => 'text/x-haskell',
        java => 'text/x-java',
        pas => 'text/x-pascal',
        pl => 'text/x-perl',
@@ -66,6 +39,9 @@ use constant CONTENT_TYPES => +{
 use constant FORMAT_EXTENSION => {
        C => 'c',
        CPP => 'cpp',
+       GCCGO => 'go',
+       GOLANG => 'go',
+       HASKELL => 'hs',
        MONO => 'cs',
        JAVA => 'java',
        PASCAL => 'pas',
@@ -74,7 +50,9 @@ use constant FORMAT_EXTENSION => {
 };
 
 use constant NOT_FOUND => [404, ['Content-Type' => 'text/plain'], ['Not found']];
-use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => ' Basic realm="Gruntmaster 6000"'], ['Forbidden']];
+use constant FORBIDDEN => [401, ['Content-Type' => 'text/plain', 'WWW-Authenticate' => 'Basic realm="Gruntmaster 6000"'], ['Forbidden']];
+
+sub development() { ($ENV{PLACK_ENV} // 'development') eq 'development' }
 
 my $env;
 
@@ -86,7 +64,7 @@ sub remote_user {
        $user
 }
 
-sub admin   { remote_user && remote_user->isadmin }
+sub admin   { remote_user && remote_user->admin }
 sub contest { db->contest ($_{contest}) }
 sub problem { db->problem ($_{problem}) }
 sub job     { db->job     ($_{job})     }
@@ -100,8 +78,10 @@ sub response {
                $params = $title;
                $title = 'No title';
        }
+       $params->{time} = time;
        bless {template => $template, title => $title, params => $params}, __PACKAGE__.'::Response'
 }
+
 sub forbid   {
        return if !shift || admin;
        unwind FORBIDDEN, SUB UP
@@ -116,14 +96,14 @@ sub dispatch_request{
                        my $css = read_file "css/themes/$theme.css";
                        $css .= read_file $_ for <css/*.css>;
                        my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'text/css');
-                       [200, \@headers, [CSS::Minifier::XS::minify $css]]
+                       [200, \@headers, [development ? $css : CSS::Minifier::XS::minify $css]]
                },
 
                sub (/js.js) {
                        my $js;
                        $js .= read_file $_ for <js/*.js>;
                        my @headers = ('X-Forever' => 1, 'Cache-Control' => 'public, max-age=604800', 'Content-Type' => 'application/javascript');
-                       [200, \@headers, [JavaScript::Minifier::XS::minify $js]]
+                       [200, \@headers, [development ? $js : JavaScript::Minifier::XS::minify $js]]
                },
 
                sub (/src/:job) {
@@ -149,19 +129,16 @@ sub dispatch_request{
                        response_filter {
                                my ($r) = @_;
                                return $r if ref $r ne 'Plack::App::Gruntmaster::Response';
-                               return [200, ['Content-Type' => 'application/json', 'X-Forever' => 1], [encode_json $r->{params}]] if $env->{HTTP_ACCEPT} =~ m,^\s*application/json\s*$,g;
-                               render $r->{template}, $r->{title}, %{$r->{params}}
+                               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;
+                               my $ret = render $r->{template}, 'en', title => $r->{title}, %{$r->{params}};
+                               [200, ['Content-Type' => 'text/html'], [encode 'UTF-8', $ret]]
                        },
                },
 
-               sub (/st/) {
-                       response st => 'Standings', { st => [db->standings] }
-               },
-
                sub (/st/:contest) {
                        response st => 'Standings', {
-                               st => [ db->standings($_{contest}) ],
-                               problems => [map { $_->problem } contest->contest_problems]
+                               st => [ contest->standings ],
+                               problems => [map { $_->problem } contest->contest_problems],
                        }
                },
 
@@ -169,24 +146,26 @@ sub dispatch_request{
 
                sub (/us/)                                        { response us => 'Users', {us => db->user_list} },
                sub (/ct/  + ?:owner~)                            { response ct => 'Contests', db->contest_list(%_) },
-               sub (/log/ + ?:contest~&:owner~&:page~&:problem~) { response log => 'Job list', {log => db->job_list(%_)} },
-               sub (/pb/  + ?:owner~&:contest~)                  { response pb => 'Problems', db->problem_list(%_) },
+               sub (/log/ + ?:contest~&:owner~&:page~&:problem~) { response log => 'Job list', {%{db->job_list(%_)}, maybe contest => $_{contest}} },
+               sub (/pb/  + ?:owner~&:contest~)                  { response pb => 'Problems', {%{db->problem_list(%_)}, maybe contest => $_{contest}} },
 
                sub (/us/:user)    { response us_entry => user->name, db->user_entry($_{user}) },
                sub (/ct/:contest) { response ct_entry => contest->name, db->contest_entry($_{contest}) },
                sub (/log/:job)    { response log_entry => "Job  $_{job}", db->job_entry($_{job}) },
-               sub (/pb/:problem + ?:contest~) {
+               sub (/pb/:problem + ?contest~) {
+                       my (undef, undef, $contest) = @_;
+                       $_{contest} = $contest;
                        return NOT_FOUND if !contest && !problem->is_in_archive || contest && !db->contest_problems->find($_{contest}, $_{problem});
                        forbid problem->is_private;
                        response pb_entry => problem->name, db->problem_entry($_{problem}, $_{contest}, remote_user && remote_user->id);
                },
+               sub (/sol/:problem) {
+                       forbid !problem->is_in_archive;
+                       response sol => 'Solution of ' . problem->name, {solution => db->problem($_{problem})->solution};
+               },
 
                sub (/) { redispatch_to '/index' },
-
-               sub (/:article) {
-                       my $title = read_file "a/$_{article}.en.title";
-                       response $_{article} => $title, {};
-               }
+               sub (/:article) { [200, ['Content-Type' => 'text/html'], [render_article $_{article}, 'en']] }
        },
 
        sub (POST) {
@@ -210,16 +189,16 @@ sub dispatch_request{
                        reply 'Password changed successfully';
                },
 
-               sub (/action/submit + %:problem=&:contest~&prog_format=&private~ + *source_code=) {
+               sub (/action/submit + %:problem=&:contest~&:prog_format=&:private~&:source_code~ + *:prog~) {
                        forbid !remote_user;
-                       return reply 'This contest has finished' if contest->is_finished;
-                       return reply 'This contest has not yet started' if !admin && contest->is_pending;
-                       return reply 'Maximum source size is 10KB' if $_{source_code}->size > 25 * 1024;
+                       return reply 'This contest has finished' if contest && contest->is_finished;
+                       return reply 'This contest has not yet started' if !admin && contest && contest->is_pending;
+                       return reply 'Maximum source size is 10KB' if ($_{prog} ? $_{prog}->size : length $_{source_code}) > 10 * 1024;
                        return reply 'You must wait 30 seconds between jobs' if !admin && time <= remote_user->lastjob + 30;
                        remote_user->update({lastjob => time});
 
-                       my $prog = read_file $_{source_code}->path;
-                       unlink $_{source_code}->path;
+                       my $prog = $_{prog} ? read_file $_{prog}->path : $_{source_code};
+                       unlink $_{prog}->path if $_{prog};
                        db->jobs->create({
                                maybe contest => $_{contest},
                                maybe private => $_{private},
This page took 0.029275 seconds and 4 git commands to generate.