8f2b548cc184bad032d8de7d8012b6345d894a30
[catagits/Web-Session.git] / examples / app.psgi
1 #!/usr/bin/perl
2
3 use Plack::Session;
4 use Plack::Session::State;
5 use Plack::Session::State::Cookie;
6 use Plack::Session::Store;
7 use Plack::Middleware::Session;
8
9 my $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
16         my $session = $env->{'plack.session'};
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 );