+use Plack::Builder;
+use Plack::Request;
+use Digest::SHA qw/sha256/;
+use Log::Log4perl;
+
+use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
+use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self' www.google-analytics.com; connect-src 'self',;
+
+$Apache2::AuthzCaps::rootdir = $Apache2::Authen::Passphrase::rootdir;
+my $word = qr,(\w+),a;
+my $number = qr,(\d+),a;
+my $db = Gruntmaster::Data->connect('dbi:Pg:');
+
+sub debug {
+ local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
+ $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
+}
+
+sub some_auth_required {
+ my $r = Plack::Request->new($_[0]);
+ return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path eq '/submit';
+ return 1 if $r->path =~ m,^/pb/$word, && $_[0]->{'gruntmaster.contest'} && time < $db->contest($_[0]->{'gruntmaster.contest'})->stop;
+ ''
+}
+
+sub is_problem_private {
+ my $pb = $_[0];
+ return 1 if $db->problem($pb)->private;
+ my $prv = 0;
+ for my $cp ($db->problem($pb)->contest_problems) {
+ $prv = 1;
+ return '' if $cp->contest->start <= time;
+ }
+
+ $prv
+}
+
+sub is_problem_in_contest {
+ my ($pb, $ct) = @_;
+ return 1 if $db->contest_problems->find($ct, $pb);
+ return '' if defined $ct;
+ for my $cp ($db->problem($pb)->contest_problems) {
+ return 1 if $cp->contest->stop <= time;
+ }
+ ''
+}
+
+sub admin_required {
+ local $_ = $_[0];
+ my $env = $_[1];
+ return $db->contest($env->{'gruntmaster.contest'})->owner->id if $env->{'gruntmaster.contest'} && $db->contest($env->{'gruntmaster.contest'})->start > time;
+ return $db->problem($1)->owner->id if m,^/pb/$word, && is_problem_private $1;
+ return $db->problem($1)->owner->id if m,^/pb/$word, && !is_problem_in_contest $1, $env->{'gruntmaster.contest'};
+ return $db->problem($env->{'gruntmaster.problem'}) if $env->{'gruntmaster.problem'} && is_problem_private $env->{'gruntmaster.problem'};
+ return $db->problem($env->{'gruntmaster.problem'}) if $env->{'gruntmaster.problem'} && !is_problem_in_contest $env->{'gruntmaster.problem'}, $env->{'gruntmaster.contest'};
+ return $db->job ($1)->owner->id if m,^/log/(?:src/)?$number, && $db->job($1)->private;
+ return $db->job ($1)->owner->id if m,^/log/(?:src/)?$number, && is_problem_private $db->job($1)->problem->id;
+ return $db->job ($1)->owner->id if m,^/log/(?:src/)?$number, && $db->job($1)->contest && $db->job($1)->contest->stop > time;
+ ''
+}
+
+sub require_admin {
+ my $app = $_[0];
+ sub {
+ local *__ANON__ = "require_admin_middleware";
+ my $env = $_[0];
+ my $r = Plack::Request->new($env);
+ $env->{'gruntmaster.reqadmin'} = admin_required $r->path, $env;
+ $app->($env)
+ }
+}
+
+sub mangle_request {
+ my $app = $_[0];
+ sub {
+ local *__ANON__ = 'mangle_request_middleware';
+ my $env = $_[0];
+ my ($number, $word) = (qr,(\d+),a, qr,(\w+),a);
+ for ($env->{PATH_INFO}) {
+ $env->{'gruntmaster.page'} = $1 if s,/page/$number$,/,;
+ $env->{'gruntmaster.contest'} = $1 if s,^/ct/$word/,/,;
+ $env->{'gruntmaster.problem'} = $1 if s,^/pb/$word/,/,;
+ $env->{'gruntmaster.user'} = $1 if s,^/us/$word/,/,;
+ $env->{'gruntmaster.page'} //= -1 if m,^/log/$,;
+ }
+ $app->($env);
+ }
+}
+
+my %authen_cache;
+
+sub authenticate {
+ my ($user, $pass, $env) = @_;
+ my $cache_key = sha256 "$user:$pass";
+ my $time = $authen_cache{$cache_key} // 0;
+ if ($time >= time - 300) {
+ return 1;
+ } else {
+ delete $authen_cache{$cache_key};
+ }
+
+ return unless eval {
+ pwcheck $user, $pass;
+ 1
+ };
+ $authen_cache{$cache_key} = time;
+
+ return if $env->{'gruntmaster.reqadmin'} && $env->{'gruntmaster.reqadmin'} ne $user && !hascaps $user, 'gmadm';
+ 1
+}
+
+Log::Log4perl->init('log.conf');
+my $access_logger = Log::Log4perl->get_logger('access');
+$ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;