Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Dancer2 sessions under Plack

by fishboy (Initiate)
on Nov 28, 2013 at 04:42 UTC ( #1064730=perlquestion: print w/replies, xml ) Need Help??
fishboy has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to deploy the Dancer2::Tutorial app with Plack::Handler::Apache2 and having problems implementing Sessions using the following setup:

appname: "TestSession" layout: "main" charset: "UTF-8" # session setup session: YAML engines: session: YAML: session_dir: /tmp/dancer-sessions # template engine template: "template_toolkit" engines: template: template_toolkit: start_tag: '[%' end_tag: '%]'

#!/usr/bin/env perl use FindBin; use lib "$FindBin::Bin/../lib"; use TestSession; TestSession->dance; code

package TestSession; use Dancer2; our $VERSION = '0.11'; use DBI; use File::Spec; use File::Slurp; use Template; set 'logger' => 'file'; set 'log' => 'debug'; set 'show_errors' => 1; set 'startup_info' => 1; set 'warnings' => 1; set 'username' => 'admin'; set 'password' => 'password'; set 'layout' => 'main'; my $timestamp = localtime; my $login_status; my $dbh = DBI->connect('DBI:mysql:testsession','test','dr_dfRFgt@');\ my $flash; sub set_flash { my $message = shift; $flash = $message; } sub get_flash { my $msg = $flash; $flash = ""; return $msg; } hook before_template => sub { my $tokens = shift; $tokens->{'session_logged_in'} = session('logged_in'); $tokens->{'css_url'} = request->base . 'css/style.css'; $tokens->{'login_url'} = uri_for('/login'); $tokens->{'logout_url'} = uri_for('/logout'); }; get '/' => sub { my $sql = 'select id, etitle, etext from entries order by id desc +'; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute or die $sth->errstr; template '', { 'session_logged_in' => session('logged_in'), 'msg' => get_flash(), 'add_entry_url' => uri_for('/add'), 'entries' => $sth->fetchall_hashref('id'), }; }; post '/add' => sub { if (not $login_status) { debug "add route attempted with login_status $login_status"; send_error("Not logged in", 401); } debug "login_status $login_status: adding entry on status $login_ +status"; my $sql = 'insert into entries (etitle, etext) values (?, ?)'; my $sth = $dbh->prepare($sql) or die $dbh->errstr; $sth->execute(params->{'etitle'}, params->{'etext'}) or die $sth- +>errstr; set_flash('New entry posted!'); redirect '/'; }; any ['get', 'post'] => '/login' => sub { my $err; if ( request->method() eq "POST" ) { debug "executing login attempt\n"; # process form input if ( params->{'username'} ne setting('username') ) { $err = "Invalid username"; debug "Got Invalid username"; } elsif ( params->{'password'} ne setting('password') ) { $err = "Invalid password"; debug "Got Invalid password"; } else { debug "Login Successful"; session logged_in => true; $login_status = 1; debug "login_status:".session('logged_in'); set_flash('You are logged in.'); redirect '/'; } } # display login form template '', { 'err' => $err, }; }; get '/logout' => sub { debug "login:\t executng LOGOUT\n"; context->destroy_session; $login_status = 0; debug "login_status on LOGOUT:\t$login_status"; set_flash('You are logged out.'); debug "login LOGOUT flash message: ".$flash; redirect '/'; }; start; true;

Apache 2 virtualhosts.conf entry.

<VirtualHost> ServerAdmin DocumentRoot /var/www/retireaboard/RetireAboard ServerName <Directory "/var/www/retireaboard/RetireAboard"> AllowOverride None Order allow,deny Allow from all </Directory> <Location /> SetHandler perl-script PerlResponseHandler Plack::Handler::Apache2 PerlSetVar psgi_app /var/www/retireaboard/RetireAboard/bin/app </Location> SetEnv DANCER_ENVIRONMENT "development" ErrorLog /var/log/httpd/ CustomLog /var/log/httpd/retireaboard_access.log combined </VirtualHost>

httpd error log

[Wed Nov 27 01:15:53 2013] [error] [client] Error while lo +ading /var/www/retireaboard/RetireAboard/bin/ Unable to create + session dir : ./sessions : Permission denied at (eval 135) line 11\n +Compilation failed in require at /var/www/retireaboard/RetireAboard/b +in/ line 6.\nBEGIN failed--compilation aborted at /var/www/reti +reaboard/RetireAboard/bin/ line 6.\n

Using Plack::Handler::Apache2 to deploy the app with Apache2 causes the compile to fail. The app is unable to create a ./session dir despite having full rwx permissions for the appdir.
Exactly the same failure occurs for JSON sessions.
Is there further Plack setup required in the .pm file?

Replies are listed 'Best First'.
Re: Dance2 sessions under Plack
by Anonymous Monk on Nov 28, 2013 at 04:49 UTC
    The error message shows ./sessions but in your configuration you show /tmp/dancer-sessions , so there is your problem :) whatever it is

    Maybe you're reading the wrong documentation? Dancer2 is not Dancer

      Even though config.yml sets the session_dir to /tmp/dancer_sessions, the app still tries to use ./sessions, that's an issue.
      I am reading and using the Dancer2 docs throughout.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1064730]
Approved by kcott
[jedikaiti]: Your Mother++ #♫ You say apod, I say Perl POD ... let's call the whole thing off ♬

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2017-08-18 17:30 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (306 votes). Check out past polls.