Thanks – that makes sense

Z

From: dancer-users [mailto:[email protected]] On Behalf Of Maxwell 
Carey
Sent: 19 March 2015 21:59
To: Perl Dancer users mailing list
Subject: Re: [dancer-users] serialiser scope

Have you read the Dancer advent calendar article about serializers? 
http://advent.perldancer.org/2014/11
You can use Plack::Builder to mount two separate apps together, and they can 
still share sessions:

$ cat MyApp/lib/MyApp.pm
package MyApp;
use Dancer2;

our $VERSION = '0.1';

get '/' => sub {
    session foo => 'bar';
    template 'index';
};

true;

$ cat MyApp/lib/MyApp/API.pm
package MyApp::API;
use Dancer2;

set serializer => 'JSON';

get '/' => sub {
    my $foo = session('foo') // 'fail';
    return { foo => $foo };
};

true;

$ cat MyApp/bin/app.psgi
#!/usr/bin/env perl

use strict;
use warnings;
use FindBin;
use lib "$FindBin::Bin/../lib";

use MyApp;
use MyApp::API;
use Plack::Builder;

builder {
    mount '/'    => MyApp->to_app;
    mount '/api' => MyApp::API->to_app;
};
And the test:

$ cat session_test
#!/usr/bin/env perl

use strict;
use warnings;
use 5.010;

use WWW::Mechanize;

my $mech = WWW::Mechanize->new;

my $base_url = 'http://localhost:5000/';
my $api_url = $base_url . 'api/';

$mech->get($base_url);
$mech->get($api_url);

say $mech->content(raw => 1);

$ ./session_test
{"foo":"bar"}

On Thu, Mar 19, 2015 at 6:51 AM, Zahir Lalani 
<[email protected]<mailto:[email protected]>> 
wrote:
Hi All

A little confused about the serialiser scope. I get that in the config file, it 
will apply to the entire application. I have a need to setup template driven 
routes and ajax routes. So I have a number of route files, and all use the app 
name to link the routes into one application.  Had assumed that the “set 
serialiser “ would be active in the current package only (i.e. the ajax routes 
file) but it seems it applies to the application since all routes are linked 
into the same application.

I have hunted for clues, but in all honesty I am a little confused as to the 
solution. I know creating a separate app would work, but these routes are part 
of the same app and need to be centrally controlled in terms of session 
management etc.

Any advice most appreciated

Z

_______________________________________________
dancer-users mailing list
[email protected]<mailto:[email protected]>
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

_______________________________________________
dancer-users mailing list
[email protected]
http://lists.preshweb.co.uk/mailman/listinfo/dancer-users

Reply via email to