http://qs1969.pair.com?node_id=1064730

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:
config.yml

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: '%]'

app.pl

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

TestSession.pm 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 'show_entries.tt', { '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 'login.tt', { '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 96.0.255.146:80> ServerAdmin webmaster@dataship.com DocumentRoot /var/www/retireaboard/RetireAboard ServerName retireaboard.com <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 +.pl </Location> SetEnv DANCER_ENVIRONMENT "development" ErrorLog /var/log/httpd/www.retireaboard.com.log CustomLog /var/log/httpd/retireaboard_access.log combined </VirtualHost>

httpd error log

[Wed Nov 27 01:15:53 2013] [error] [client 24.86.10.67] Error while lo +ading /var/www/retireaboard/RetireAboard/bin/app.pl: 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/app.pl line 6.\nBEGIN failed--compilation aborted at /var/www/reti +reaboard/RetireAboard/bin/app.pl 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

      Correct:
      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.