From be1da3f93234c402d5679e6028ae8a2bb915185a Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Sun, 1 Mar 2015 10:02:33 +0200 Subject: [PATCH] Connect lazily to database to allow starman --preload-app --- app.psgi | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/app.psgi b/app.psgi index c61b8bb..85e8847 100644 --- 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 } -- 2.39.2