[dancer-users] serialiser scope

Maxwell Carey mcarey at ucar.edu
Thu Mar 19 21:59:28 GMT 2015


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 <
ZahirLalani at oliver-marketing.com> 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
> dancer-users at dancer.pm
> http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.preshweb.co.uk/pipermail/dancer-users/attachments/20150319/d1817c34/attachment.html>


More information about the dancer-users mailing list