-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathRouter.pm
More file actions
72 lines (47 loc) · 1.44 KB
/
Router.pm
File metadata and controls
72 lines (47 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package WebAPI::DBIC::Router;
=head1 NAME
WebAPI::DBIC::Router - Route URL paths to resources
=head1 DESCRIPTION
This is currently a wrapper for L<Path::Router>.
The intention is to allow support for other routers.
=cut
use Moo;
use Carp qw(croak);
use Path::Router;
use Plack::App::Path::Router;
use namespace::clean -except => [qw(meta)];
use MooX::StrictConstructor;
has router => (
is => 'ro',
default => sub { Path::Router->new },
handles => [ qw(match) ],
);
has extra_routes => (
is => 'ro',
default => sub { [] },
);
sub add_route {
my ($self, %args) = @_;
my $path = delete $args{path};
my $validations = delete $args{validations} || {};
my $defaults = delete $args{defaults} || {};
my $target = delete $args{target} or croak "target not specified";
croak "Unknown params (@{[ sort keys %args ]})" if %args;
$self->router->add_route($path,
validations => $validations,
defaults => $defaults,
target => $target,
);
}
sub to_psgi_app {
my $self = shift;
foreach my $route (@{ $self->extra_routes }) {
$self->router->add_route( @$route );
}
return Plack::App::Path::Router->new( router => $self->router )->to_app; # return Plack app
}
sub uri_for { # called by WebAPI::DBIC::Resource::Role::Router
local $SIG{__DIE__}; # https://github.com/timbunce/WebAPI-DBIC/issues/22
return shift->router->uri_for(@_);
}
1;