]>
Commit | Line | Data |
---|---|---|
7dc32473 MG |
1 | #!/usr/bin/perl -w |
2 | use v5.14; | |
491e82eb | 3 | no if $] >= 5.017011, warnings => 'experimental::smartmatch'; |
7dc32473 | 4 | |
5a879d9a | 5 | use Gruntmaster::Data; |
7dc32473 | 6 | use Plack::App::Gruntmaster; |
c85bf4a6 | 7 | use Plack::Builder; |
3b69df7a | 8 | use Plack::Util; |
68d5c3ff MG |
9 | use Log::Log4perl; |
10 | ||
594d53ba | 11 | use constant AUTH_TIMEOUT => 5 * 60; |
bbff4a8b | 12 | use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i %l %u %t "%r" %>s %b "%{Referer}i" "%{User-agent}i"'; |
031ffaf5 MG |
13 | |
14 | sub CONTENT_SECURITY_POLICY () { | |
15 | my $csp = <<CSP; | |
16 | default-src 'none' | |
17 | connect-src 'self' | |
18 | form-action 'self' | |
19 | frame-ancestors 'none' | |
bd623b0d | 20 | img-src 'self' https://static.mindcoding.ro |
031ffaf5 | 21 | referrer origin-when-cross-origin |
bd623b0d | 22 | script-src https://static.mindcoding.ro/static/js.js |
d2a977e3 | 23 | style-src https://static.mindcoding.ro/static/css/ |
031ffaf5 MG |
24 | CSP |
25 | chomp $csp; | |
26 | $csp =~ s/\n/; /gr; | |
27 | } | |
c85bf4a6 | 28 | |
be1da3f9 | 29 | my $db; |
5a879d9a | 30 | |
be1da3f9 MG |
31 | sub add_database { |
32 | my $app = $_[0]; | |
33 | sub { | |
34 | my ($env) = @_; | |
35 | $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:'); | |
36 | $env->{'gruntmaster.dbic'} = $db; | |
37 | $app->($env) | |
38 | } | |
39 | } | |
40 | ||
3b69df7a MG |
41 | sub add_headers { |
42 | my $app = $_[0]; | |
43 | sub { | |
44 | my $resp = $app->($_[0]); | |
45 | my $hdrs = Plack::Util::headers($resp->[1]); | |
46 | $hdrs->set('Content-Security-Policy', CONTENT_SECURITY_POLICY); | |
2f64890f | 47 | $hdrs->set('Link', '<https://static.mindcoding.ro/static/slate.css>; rel=stylesheet') if $hdrs->get('Content-Type') =~ m,^text/html,; |
3b69df7a MG |
48 | $hdrs->set('Cache-Control', 'public, max-age=604800') if $_[0]->{PATH_INFO} =~ qr,^/static/,; |
49 | $resp->[1] = $hdrs->headers; | |
50 | $resp; | |
51 | } | |
52 | } | |
53 | ||
28b3c465 | 54 | Log::Log4perl->init_once('log.conf'); |
68d5c3ff | 55 | my $access_logger = Log::Log4perl->get_logger('access'); |
58a0ba09 | 56 | $ENV{DBIC_NULLABLE_KEY_NOWARN} = 1; |
68d5c3ff | 57 | |
7dc32473 | 58 | builder { |
28aeef34 | 59 | enable_if { $_[0]->{PATH_INFO} eq '/ok' } sub { sub{ [200, [], []] }}; |
68d5c3ff | 60 | enable 'AccessLog', format => ACCESSLOG_FORMAT, logger => sub { $access_logger->info(@_) }; |
287424cb | 61 | enable 'ContentLength'; |
3b69df7a | 62 | enable \&add_headers; |
7e3d8247 | 63 | enable 'Static', path => qr,^/static/,; |
68d5c3ff | 64 | enable 'Log4perl', category => 'plack'; |
be1da3f9 | 65 | enable \&add_database; |
8d725691 MG |
66 | enable '+Plack::App::Gruntmaster::Auth', |
67 | dbi_connect => [$ENV{GRUNTMASTER_DSN} // 'dbi:Pg:', '', ''], | |
68 | realm => 'Gruntmaster 6000', | |
69 | mail_from => $ENV{GRUNTMASTER_RESET_FROM}; | |
594d53ba | 70 | Plack::App::Gruntmaster->run_if_script |
7dc32473 | 71 | } |