From: Marius Gavrilescu Date: Sun, 1 Mar 2015 08:02:33 +0000 (+0200) Subject: Connect lazily to database to allow starman --preload-app X-Git-Url: http://git.ieval.ro/?a=commitdiff_plain;h=be1da3f93234c402d5679e6028ae8a2bb915185a;p=plack-app-gruntmaster.git Connect lazily to database to allow starman --preload-app --- 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 }