]> iEval git - plack-app-gruntmaster.git/blobdiff - app.psgi
Connect lazily to database to allow starman --preload-app
[plack-app-gruntmaster.git] / app.psgi
index 7dce99b972bbfd7fe9448ca2ac9172fff6ba5102..85e8847a44e8b7420e8cefca46ec862075f06cfe 100644 (file)
--- a/app.psgi
+++ b/app.psgi
@@ -12,10 +12,24 @@ use Log::Log4perl;
 use Tie::Hash::Expire;
 
 use constant AUTH_TIMEOUT => 5 * 60;
-use constant ACCESSLOG_FORMAT => '%{X-Forwarded-For}i|%h %u "%r" %>s %b "%{Referer}i" "%{User-agent}i"';
-use constant CONTENT_SECURITY_POLICY => q,default-src 'none'; script-src 'self' www.google-analytics.com; style-src 'self'; img-src 'self'; connect-src 'self'; frame-src free.timeanddate.com,;
+use constant ACCESSLOG_FORMAT => 'combined';
 
-our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
+sub CONTENT_SECURITY_POLICY () {
+       my $csp = <<CSP;
+default-src 'none'
+connect-src 'self'
+form-action 'self'
+frame-ancestors 'none'
+img-src 'self'
+referrer origin-when-cross-origin
+script-src 'self'
+style-src 'self'
+CSP
+       chomp $csp;
+       $csp =~ s/\n/; /gr;
+}
+
+my $db;
 
 tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT};
 
@@ -28,6 +42,16 @@ sub authenticate {
        $auth{key} = 1;
 }
 
+sub add_database {
+       my $app = $_[0];
+       sub {
+               my ($env) = @_;
+               $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
+               $env->{'gruntmaster.dbic'} = $db;
+               $app->($env)
+       }
+}
+
 sub add_headers {
        my $app = $_[0];
        sub {
@@ -51,7 +75,7 @@ builder {
        enable \&add_headers;
        enable 'Static', path => qr,^/static/,;
        enable 'Log4perl', category => 'plack';
+       enable \&add_database;
        enable_if { shift->{HTTP_AUTHORIZATION} } 'Auth::Basic', authenticator => \&authenticate, realm => 'Gruntmaster 6000';
-       enable sub { my $app = $_[0]; sub { $_[0]->{'gruntmaster.dbic'} = $db; $app->($_[0]) } };
        Plack::App::Gruntmaster->run_if_script
 }
This page took 0.021941 seconds and 4 git commands to generate.