Skip to content

Commit 39eb7a7

Browse files
committed
JSON serialization for cookies.
1 parent df81d32 commit 39eb7a7

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

app.psgi

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use Config::JFDI;
1818
use FindBin;
1919
use lib "$FindBin::RealBin/lib";
2020
use File::Path ();
21+
use JSON qw( encode_json decode_json );
2122
use MetaCPAN::Web;
2223
use Plack::Builder;
2324
use Plack::App::File;
@@ -29,6 +30,7 @@ use Plack::Middleware::MCLess;
2930
use Plack::Middleware::ReverseProxy;
3031
use Plack::Middleware::Session::Cookie;
3132
use Plack::Middleware::ServerStatus::Lite;
33+
use Plack::Session::Store::File;
3234

3335
# explicitly call ->to_app on every Plack::App::* for performance
3436
my $app = Plack::App::URLMap->new;
@@ -63,6 +65,9 @@ my $app = Plack::App::URLMap->new;
6365

6466
die 'cookie_secret not configured' unless $config->get->{cookie_secret};
6567

68+
my $storage_path = "$path/var/tmp/cookies";
69+
maybe_make_path($storage_path);
70+
6671
# Add session cookie here only
6772
$core_app = Plack::Middleware::Session::Cookie->wrap(
6873
$core_app,
@@ -71,6 +76,11 @@ my $app = Plack::App::URLMap->new;
7176
secure => ( ( $ENV{PLACK_ENV} || q[] ) ne 'development' ),
7277
httponly => 1,
7378
secret => $config->get->{cookie_secret},
79+
store => Plack::Session::Store::File->new(
80+
dir => $storage_path,
81+
serializer => sub { encode_json(@_) },
82+
deserializer => sub { decode_json(@_) },
83+
),
7484
);
7585

7686
$app->map( q[/] => $core_app );
@@ -80,10 +90,8 @@ $app = $app->to_app;
8090

8191
unless ( $ENV{HARNESS_ACTIVE} ) {
8292
my $scoreboard = "$FindBin::RealBin/var/tmp/scoreboard";
83-
unless ( -d $scoreboard ) {
84-
File::Path::make_path($scoreboard)
85-
or die "Can't make_path $scoreboard: $!";
86-
}
93+
maybe_make_path($scoreboard);
94+
8795
$app = Plack::Middleware::ServerStatus::Lite->wrap(
8896
$app,
8997
path => '/server-status',
@@ -175,6 +183,14 @@ if ( !$ENV{PLACK_ENV} || $ENV{PLACK_ENV} ne 'development' ) {
175183
};
176184
}
177185

186+
sub maybe_make_path {
187+
my $path = shift;
188+
189+
return if -d $path;
190+
191+
File::Path::make_path($path) or die "Can't make_path $path: $!";
192+
}
193+
178194
$app = Plack::Middleware::ReverseProxy->wrap($app);
179195

180196
return $app;

0 commit comments

Comments
 (0)