[dancer-users] Dancer2::Manual: confused as to use of 'appname' keyword to distribute code across packages
Amelia Ireland
aireland at lbl.gov
Fri Jul 15 04:35:29 BST 2016
I didn't read the first email properly -- I think what you want to do is
this:
in WebApp.pm:
package WebApp;
use Dancer2 appname => 'test';
# plus the other dependencies
hook before => sub {
if (!session('user') && request->dispatch_path !~ m{^/login}) {
forward '/login', { requested_path => request->dispatch_path };
}
};
get '/' => sub { template 'index'; };
get '/index' => sub { redirect '/' };
# etc.
1;
in WebApp::API.pm:
package WebApp::API;
use Dancer2 appname => 'test';
# handles everything under /api
prefix '/api' => sub {
any ['get', 'post'] => '/userdata/:user' => sub {
my $user_value = route_parameters->get('user');
my $user = database->quick_select('users', { username =>
$user_value });
# etc.
};
};
1;
in app.psgi:
use WebApp;
use WebApp::API;
builder {
mount '/' => WebApp->to_app # note that WebApp::API->to_app also
works here
};
To make life even easier for yourself, you can make a wrapper to hold your
various route modules:
package WebAppWrap;
use Dancer2 appname => 'test';
use WebApp;
use WebApp::API;
use WebApp::JSON;
use WebApp::Complex::Package; # each of these modules has appname 'test'
# etc.
in app.psgi:
use WebAppWrap;
builder {
mount '/' => WebAppWrap->to_app # pulls in all loaded modules with
appname 'test'
};
On 14 July 2016 at 18:53, James E Keenan <jkeen at verizon.net> wrote:
> On 07/14/2016 06:11 PM, Amelia Ireland wrote:
>
>> Have you checked http://localhost:5000/api/api/userdata/dancer_operator?
>>
>> Another 404 Not Found.
>
>
>
> _______________________________________________
> 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/20160714/ddd7e1ca/attachment.html>
More information about the dancer-users
mailing list