2 # Simple counter web application
4 # NOTE: This example uses Plack::Request to illustrate how
5 # Plack::Middleware::Session interface ($env->{'psgix.session'}) could
6 # be wrapped and integrated as part of the request API. See Tatsumaki
7 # (integrated via subclassing Plack::Request) and Dancer::Session::PSGI
8 # how to adapt Plack::Middleware::Session to web frameworks API.
10 # You're not recommended to write a new web application using this style.
14 use Plack::Session::State;
15 use Plack::Session::State::Cookie;
16 use Plack::Session::Store;
17 use Plack::Middleware::Session;
19 my $app = Plack::Middleware::Session->wrap(
22 my $r = Plack::Request->new( $env );
24 return [ 404, [], [] ] if $r->path_info =~ /favicon.ico/;
26 my $session = $r->session;
28 my $id = $session->id;
29 my $counter = $session->get('counter') || 0;
31 $session->set( 'counter' => $counter + 1 );
35 if ( $r->param( 'logout' ) ) {
37 $resp = $r->new_response;
38 $resp->redirect( $r->path_info );
41 $resp = $r->new_response(
43 [ 'Content-Type' => 'text/html' ],
48 <title>Plack::Middleware::Session Example</title>
51 <h1>Session Id: ${id}</h1>
52 <h2>Counter: ${counter}</h2>
54 <a href="/?logout=1">Logout</a>
64 state => Plack::Session::State::Cookie->new,
65 store => Plack::Session::Store->new,