Update for the new Gruntmaster::Data
[plack-app-gruntmaster.git] / app.psgi
1 #!/usr/bin/perl
2 use v5.14;
3 use warnings;
4
5 use Gruntmaster::Data;
6 use Plack::App::Gruntmaster;
7 use Plack::Builder;
8 use Plack::Util;
9 use Log::Log4perl;
10
11 sub CONTENT_SECURITY_POLICY () {
12 my $csp = <<CSP;
13 default-src 'none'
14 connect-src 'self'
15 form-action 'self'
16 frame-ancestors 'none'
17 img-src 'self'
18 referrer origin-when-cross-origin
19 script-src 'self'
20 style-src 'self'
21 CSP
22 chomp $csp;
23 $csp =~ s/\n/; /gr;
24 }
25
26 my $dbinit;
27
28 sub add_database {
29 my $app = $_[0];
30 sub {
31 Gruntmaster::Data::init $ENV{GRUNTMASTER_DSN} // 'dbi:Pg:' unless $dbinit;
32 $dbinit = 1;
33 $app->(@_)
34 }
35 }
36
37 sub add_headers {
38 my $app = $_[0];
39 sub {
40 my $resp = $app->($_[0]);
41 my $hdrs = Plack::Util::headers($resp->[1]);
42 $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY);
43 $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,;
44 $resp->[1] = $hdrs->headers;
45 $resp;
46 }
47 }
48
49 Log::Log4perl->init_once('log.conf');
50 $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
51
52 builder {
53 enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }};
54 enable 'ContentLength';
55 enable \&add_headers;
56 enable 'Static', path => qr,^/static/,;
57 enable 'Log4perl', category => 'plack';
58 enable \&add_database;
59 enable '+Plack::App::Gruntmaster::Auth',
60 dbi_connect => [$ENV{GRUNTMASTER_DSN} // 'dbi:Pg:', '', ''],
61 realm => 'Gruntmaster 6000',
62 mail_from => $ENV{GRUNTMASTER_RESET_FROM};
63 Plack::App::Gruntmaster->run_if_script
64 }
This page took 0.02546 seconds and 4 git commands to generate.