update example to use Plack::Request->session, add docs about where we store session
[catagits/Web-Session.git] / examples / app.psgi
CommitLineData
bd992981 1#!/usr/bin/perl
2
3use Plack::Session;
4use Plack::Session::State;
5use Plack::Session::State::Cookie;
6use Plack::Session::Store;
7use Plack::Middleware::Session;
8
9my $app = Plack::Middleware::Session->wrap(
10 sub {
11 my $env = shift;
12 my $r = Plack::Request->new( $env );
13
14 return [ 404, [], [] ] if $r->path_info =~ /favicon.ico/;
15
536da026 16 my $session = $r->session;
bd992981 17
18 my $id = $session->id;
19 my $counter = $session->get('counter') || 0;
20
21 $session->set( 'counter' => $counter + 1 );
22
23 my $resp;
24
25 if ( $r->param( 'logout' ) ) {
26 $session->expire;
27 $resp = $r->new_response;
28 $resp->redirect( $r->path_info );
29 }
30 else {
31 $resp = $r->new_response(
32 200,
33 [ 'Content-Type' => 'text/html' ],
34 [
35 qq{
36 <html>
37 <head>
38 <title>Plack::Middleware::Session Example</title>
39 </head>
40 <body>
41 <h1>Session Id: ${id}</h1>
42 <h2>Counter: ${counter}</h2>
43 <hr/>
44 <a href="/?logout=1">Logout</a>
45 </body>
46 </html>
47 }
48 ]
49 );
50 }
51
52 $resp->finalize;
53 },
54 state => Plack::Session::State::Cookie->new,
55 store => Plack::Session::Store->new,
56);