3 no if $] >= 5.017011, warnings
=> 'experimental::smartmatch';
6 use Plack
::App
::Gruntmaster
;
10 use Digest
::SHA qw
/sha256/;
12 use Tie
::Hash
::Expire
;
14 use constant AUTH_TIMEOUT
=> 5 * 60;
15 use constant ACCESSLOG_FORMAT
=> 'combined';
17 sub CONTENT_SECURITY_POLICY
() {
22 frame-ancestors 'none'
24 referrer origin-when-cross-origin
34 tie
my %auth, 'Tie::Hash::Expire', {expire_seconds
=> AUTH_TIMEOUT
};
37 my ($user, $pass, $env) = @_;
38 my $key = sha256
"$user:$pass";
39 $env->{'gruntmaster.user'} = $user;
40 return 1 if exists $auth{$key};
41 return unless $db->user($user) && $db->user($user)->check_passphrase($pass);
49 $db //= Gruntmaster
::Data
->connect($ENV{GRUNTMASTER_DSN
} // 'dbi:Pg:');
50 $env->{'gruntmaster.dbic'} = $db;
58 my $resp = $app->($_[0]);
59 my $hdrs = Plack
::Util
::headers
($resp->[1]);
60 $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY
);
61 $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO
} =~ qr
,^/static/,;
62 $resp->[1] = $hdrs->headers;
67 Log
::Log4perl
->init_once('log.conf');
68 my $access_logger = Log
::Log4perl
->get_logger('access');
69 $ENV{DBIC_NULLABLE_KEY_NOWARN
} = 1;
72 enable_if
{ $_[0]->{PATH_INFO
} eq '/ok' } sub { sub{ [200, [], []] }};
73 enable
'AccessLog', format
=> ACCESSLOG_FORMAT
, logger
=> sub { $access_logger->info(@_) };
74 enable
'ContentLength';
76 enable
'Static', path
=> qr
,^/static/,;
77 enable
'Log4perl', category
=> 'plack';
78 enable \
&add_database
;
79 enable_if
{ shift->{HTTP_AUTHORIZATION
} } 'Auth::Basic', authenticator
=> \
&authenticate
, realm
=> 'Gruntmaster 6000';
80 Plack
::App
::Gruntmaster
->run_if_script