]> iEval git - plack-app-gruntmaster.git/blame_incremental - app.psgi
Merge branch 'master' into newmc
[plack-app-gruntmaster.git] / app.psgi
... / ...
CommitLineData
1#!/usr/bin/perl -w
2use v5.14;
3no if $] >= 5.017011, warnings => 'experimental::smartmatch';
4
5use Gruntmaster::Data;
6use Plack::App::Gruntmaster;
7use Plack::Builder;
8use Plack::Request;
9use Plack::Util;
10use Digest::SHA qw/sha256/;
11use Log::Log4perl;
12use Tie::Hash::Expire;
13
14use constant AUTH_TIMEOUT => 5 * 60;
15use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
16
17sub CONTENT_SECURITY_POLICY () {
18 my $csp = <<CSP;
19default-src 'none'
20connect-src 'self'
21form-action 'self'
22frame-ancestors 'none'
23img-src 'self' https://static.mindcoding.ro https://www.google-analytics.com/collect
24referrer origin-when-cross-origin
25script-src 'self' https://static.mindcoding.ro/js.js https://www.google-analytics.com/analytics.js
26style-src 'self' https://static.mindcoding.ro/css/
27CSP
28 chomp $csp;
29 $csp =~ s/\n/; /gr;
30}
31
32our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
33
34tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT};
35
36sub authenticate {
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);
42 $auth{key} = 1;
43}
44
45sub add_headers {
46 my $app = $_[0];
47 sub {
48 my $resp = $app->($_[0]);
49 my $hdrs = Plack::Util::headers($resp->[1]);
50 $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY);
51 $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,;
52 $resp->[1] = $hdrs->headers;
53 $resp;
54 }
55}
56
57Log::Log4perl->init_once('log.conf');
58my $access_logger = Log::Log4perl->get_logger('access');
59$ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
60
61builder {
62 enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }};
63 enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) };
64 enable 'ContentLength';
65 enable \&add_headers;
66 enable 'Static', path => qr,^/static/,;
67 enable 'Log4perl', category => 'plack';
68 enable_if { shift->{HTTP_AUTHORIZATION} } 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000';
69 enable sub { my $app = $_[0]; sub { $_[0]->{'gruntmaster.dbic'} = $db; $app->($_[0]) } };
70 Plack::App::Gruntmaster->run_if_script
71}
This page took 0.02564 seconds and 4 git commands to generate.