[dancer-users] Dancer2::Manual: confused as to use of 'appname' keyword to distribute code across packages

James E Keenan jkeen at verizon.net
Thu Jul 14 22:37:51 BST 2016


On 07/12/2016 10:44 AM, Warren Young wrote:
> On Jul 10, 2016, at 11:20 AM, James E Keenan <jkeen at verizon.net> wrote:
>>
>> All the endpoints defined in lib/mywebapp.pm continue to function as expected.  But the endpoint, /api/userdata/:user, defined in lib/mywebapp/api.pm, no longer works.
>
> That’s because your “mount” for the api.pm routes prepends ‘/api’ to them.  When you use the module standalone as you’re attempting to now, that bit is stripped off.  You’ll find that you now have /userdata/$stuff now.
>
> Easy fix: wrap the route definitions in api.pm in a prefix block:
>
>      prefix '/api' => sub {
>          any ['get', 'post'] => '/userdata/:user' => sub {
>              # etc
>          };
>      };
>

Thank you for your reply.  However, when I wrap the endpoint in 
mywebapp::api with 'prefix' per your suggestion, I am still getting 404s 
on both 'http://localhost:5000/api/userdata/dancer_operator' and 
'http://localhost:5000/userdata/dancer_operator'.

Here is the diff of what I am trying:

#####
diff --git a/bin/app.psgi b/bin/app.psgi
index 46d8845..e9ac3da 100755
--- a/bin/app.psgi
+++ b/bin/app.psgi
@@ -6,11 +6,12 @@ use FindBin;
  use lib "$FindBin::Bin/../lib";

  use mywebapp;
-use mywebapp::api;
-use Plack::Builder;
-
-builder {
-    mount '/'       => mywebapp->to_app;
-    mount '/api'    => mywebapp::api->to_app;
-};
+#use mywebapp::api;
+#use Plack::Builder;
+#
+#builder {
+#    mount '/'       => mywebapp->to_app;
+#    mount '/api'    => mywebapp::api->to_app;
+#};
+mywebapp->to_app;

diff --git a/lib/mywebapp.pm b/lib/mywebapp.pm
index 997bdc7..7c940da 100644
--- a/lib/mywebapp.pm
+++ b/lib/mywebapp.pm
@@ -1,6 +1,6 @@
  package mywebapp;
  use v5.10.1;
-use Dancer2;
+use Dancer2 appname => mywebapp;
  use Dancer2::Plugin::Database;
  use Crypt::SaltedHash;
  use Data::Dump;
diff --git a/lib/mywebapp/api.pm b/lib/mywebapp/api.pm
index 3af1adf..88b47f7 100644
--- a/lib/mywebapp/api.pm
+++ b/lib/mywebapp/api.pm
@@ -1,6 +1,6 @@
  package mywebapp::api;
  use v5.10.1;
-use Dancer2;
+use Dancer2 appname => mywebapp;
  use Dancer2::Plugin::Database;
  use Crypt::SaltedHash;
  use Data::Dump;
@@ -13,6 +13,7 @@ set session => 'Simple';
  set views => path( app->location, "templates" );
  set serializer => 'JSON';

+prefix '/api' => sub {
  any ['get', 'post'] => '/userdata/:user' => sub {

      my $user_value = route_parameters->get('user');
@@ -34,6 +35,7 @@ any ['get', 'post'] => '/userdata/:user' => sub {
          }
      }
  };
+};

  start;
######

So I'm still finding that 'appname' is not DWIMming.

Thank you very much.
Jim Keenan


More information about the dancer-users mailing list