Add editorial header
[plack-app-gruntmaster.git] / app.psgi
CommitLineData
7dc32473
MG
1#!/usr/bin/perl -w
2use v5.14;
491e82eb 3no if $] >= 5.017011, warnings => 'experimental::smartmatch';
7dc32473 4
5a879d9a 5use Gruntmaster::Data;
7dc32473 6use Plack::App::Gruntmaster;
c85bf4a6
MG
7use Plack::Builder;
8use Plack::Request;
3b69df7a 9use Plack::Util;
37bc8c44 10use Digest::SHA qw/sha256/;
68d5c3ff 11use Log::Log4perl;
594d53ba 12use Tie::Hash::Expire;
68d5c3ff 13
594d53ba 14use constant AUTH_TIMEOUT => 5 * 60;
5ab9964f 15use constant ACCESSLOG_FORMAT => 'combined';
1ca1e044 16use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self'; style-src 'self'; img-src 'self'; connect-src 'self',;
c85bf4a6 17
b5885a0b 18our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
5a879d9a 19
10c13642 20tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT};
37bc8c44 21
5a879d9a
MG
22sub authenticate {
23 my ($user, $pass, $env) = @_;
594d53ba
MG
24 my $key = sha256 "$user:$pass";
25 $env->{'gruntmaster.user'} = $user;
26 return 1 if exists $auth{$key};
27 return unless $db->user($user) && $db->user($user)->check_passphrase($pass);
28 $auth{key} = 1;
5a879d9a
MG
29}
30
3b69df7a
MG
31sub add_headers {
32 my $app = $_[0];
33 sub {
34 my $resp = $app->($_[0]);
35 my $hdrs = Plack::Util::headers($resp->[1]);
36 $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY);
37 $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,;
38 $resp->[1] = $hdrs->headers;
39 $resp;
40 }
41}
42
28b3c465 43Log::Log4perl->init_once('log.conf');
68d5c3ff 44my $access_logger = Log::Log4perl->get_logger('access');
58a0ba09 45$ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
68d5c3ff 46
7dc32473 47builder {
28aeef34 48 enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }};
68d5c3ff 49 enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) };
287424cb 50 enable 'ContentLength';
3b69df7a 51 enable \&add_headers;
7e3d8247 52 enable 'Static', path => qr,^/static/,;
68d5c3ff 53 enable 'Log4perl', category => 'plack';
3b69df7a 54 enable_if { shift->{HTTP_AUTHORIZATION} } 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000';
d3200993 55 enable sub { my $app = $_[0]; sub { $_[0]->{'gruntmaster.dbic'} = $db; $app->($_[0]) } };
594d53ba 56 Plack::App::Gruntmaster->run_if_script
7dc32473 57}
This page took 0.020182 seconds and 4 git commands to generate.