Do not handle static files (nginx does this already)
[gruntmaster-page.git] / app.psgi
1 #!/usr/bin/perl -w
2 use v5.14;
3 no if $] >= 5.017011, warnings => 'experimental::smartmatch';
4
5 use Gruntmaster::Data;
6 use Plack::App::Gruntmaster;
7 use Plack::Builder;
8 use Plack::Util;
9 use Log::Log4perl;
10
11 use constant AUTH_TIMEOUT => 5 * 60;
12
13 sub CONTENT_SECURITY_POLICY () {
14 my $csp = <<CSP;
15 default-src 'none'
16 connect-src 'self'
17 form-action 'self'
18 frame-ancestors 'none'
19 img-src 'self' https://static.mindcoding.ro
20 referrer origin-when-cross-origin
21 script-src https://static.mindcoding.ro/static/js.js
22 style-src https://static.mindcoding.ro/static/css/
23 CSP
24 chomp $csp;
25 $csp =~ s/\n/; /gr;
26 }
27
28 my $db;
29
30 sub add_database {
31 my $app = $_[0];
32 sub {
33 my ($env) = @_;
34 $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
35 $env->{'gruntmaster.dbic'} = $db;
36 $app->($env)
37 }
38 }
39
40 sub add_headers {
41 my $app = $_[0];
42 sub {
43 my $resp = $app->($_[0]);
44 my $hdrs = Plack::Util::headers($resp->[1]);
45 $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY);
46 $hdrs->set('Link', '<https://static.mindcoding.ro/static/slate.css>; rel=stylesheet') if $hdrs->get('Content-Type') =~ m,^text/html,;
47 $resp->[1] = $hdrs->headers;
48 $resp;
49 }
50 }
51
52 Log::Log4perl->init_once('log.conf');
53 $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1;
54
55 builder {
56 enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }};
57 enable 'ContentLength';
58 enable \&add_headers;
59 enable 'Log4perl', category => 'plack';
60 enable \&add_database;
61 enable '+Plack::App::Gruntmaster::Auth',
62 dbi_connect => [$ENV{GRUNTMASTER_DSN} // 'dbi:Pg:', '', ''],
63 realm => 'Gruntmaster 6000',
64 mail_from => $ENV{GRUNTMASTER_RESET_FROM};
65 Plack::App::Gruntmaster->run_if_script
66 }
This page took 0.028299 seconds and 5 git commands to generate.