]> iEval git - plack-app-gruntmaster.git/commitdiff
Connect lazily to database to allow starman --preload-app
authorMarius Gavrilescu <marius@ieval.ro>
Sun, 1 Mar 2015 08:02:33 +0000 (10:02 +0200)
committerMarius Gavrilescu <marius@ieval.ro>
Sun, 1 Mar 2015 08:02:33 +0000 (10:02 +0200)
app.psgi

index c61b8bbbb4c8c78dde09bbd09e2eecb69ebde8da..85e8847a44e8b7420e8cefca46ec862075f06cfe 100644 (file)
--- a/app.psgi
+++ b/app.psgi
@@ -29,7 +29,7 @@ CSP
        $csp =~ s/\n/; /gr;
 }
 
-our $db //= Gruntmaster::Data->connect($ENV{GRUNTMASTER_DSN} // 'dbi:Pg:');
+my $db;
 
 tie my %auth, 'Tie::Hash::Expire', {expire_seconds => AUTH_TIMEOUT};
 
@@ -42,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 {
@@ -65,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.022904 seconds and 4 git commands to generate.