Add Cache-Control to static files
[plack-app-gruntmaster.git] / app.psgi
1 #!/usr/bin/perl -w
2 use v5.14;
3
4 use Apache2::Authen::Passphrase qw/pwcheck/;
5 use Apache2::AuthzCaps qw/hascaps/;
6 use Gruntmaster::Data;
7 use Plack::App::Gruntmaster;
8 use Plack::Builder;
9 use Plack::Request;
10
11 $Apache2::AuthzCaps::rootdir = $Apache2::Authen::Passphrase::rootdir;
12 my $word = qr,(\w+),a;
13
14 sub debug {
15 local $Log::Log4perl::caller_depth = $Log::Log4perl::caller_depth + 1;
16 $_[0]->{'psgix.logger'}->({qw/level debug message/ => $_[1]})
17 }
18
19 sub some_auth_required {
20 my $r = Plack::Request->new($_[0]);
21 return 1 if $_[0]->{'gruntmaster.reqadmin'} || $r->path eq '/action/passwd' || $r->path =~ m,/pb/$word/submit$,;
22 0
23 }
24
25 sub admin_required {
26 local $_ = $_[0];
27 return 1 if m,^/pb/$word, && problem_private $1;
28 return 1 if m,^/log/(?:job|src)/$word, && job_private $1;
29 return 1 if m,^/ct/$word/(?:pb|log), && time < contest_start $1;
30 return 1 if m,^/ct/$word/log/src, && time < contest_end $1;
31 0
32 }
33
34 sub require_admin {
35 my $app = $_[0];
36 sub {
37 *__ANON__ = "require_admin_middleware";
38 my $env = $_[0];
39 my $r = Plack::Request->new($env);
40 $env->{'gruntmaster.reqadmin'} = 1 if admin_required $r->path;
41 $app->($env)
42 }
43 }
44
45 sub authenticate {
46 my ($user, $pass, $env) = @_;
47 return unless eval {
48 pwcheck $user, $pass;
49 1
50 };
51
52 return if $env->{'gruntmaster.reqadmin'} && !hascaps $user, 'gmadm';
53 1
54 }
55
56 builder {
57 enable 'ContentLength';
58 enable_if { $_[0]->{PATH_INFO} =~ qr,^/static/,} Header => set => ['Cache-Control', 'public, max-age=604800'];
59 enable 'Static', path => qr,^/static/,;
60 enable 'Log4perl', category => 'plack', conf => 'log.conf';
61 enable \&require_admin;
62 enable_if \&some_auth_required, 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000';
63 Plack::App::Gruntmaster->to_app
64 }
This page took 0.026279 seconds and 5 git commands to generate.