line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Nephia::Plugin::Dispatch; |
2
|
2
|
|
|
2
|
|
6730
|
use 5.008005; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
88
|
|
3
|
2
|
|
|
2
|
|
13
|
use strict; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
64
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
14
|
|
|
2
|
|
|
|
|
63
|
|
5
|
2
|
|
|
2
|
|
1012
|
use parent 'Nephia::Plugin'; |
|
2
|
|
|
|
|
300
|
|
|
2
|
|
|
|
|
12
|
|
6
|
2
|
|
|
2
|
|
15421
|
use Router::Simple; |
|
2
|
|
|
|
|
20407
|
|
|
2
|
|
|
|
|
2187
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = "0.03"; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub exports { |
11
|
10
|
|
|
10
|
1
|
54805
|
qw/get post put del path_param/; |
12
|
|
|
|
|
|
|
} |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
1
|
|
|
1
|
0
|
20
|
my ($class, %opts) = @_; |
16
|
1
|
|
|
|
|
10
|
my $self = $class->SUPER::new(%opts); |
17
|
1
|
|
|
|
|
81
|
my $app = $self->app; |
18
|
1
|
|
|
|
|
12
|
$app->action_chain->after('Core', Dispatch => $self->can('dispatch')); |
19
|
1
|
|
|
|
|
234
|
$app->{router} = Router::Simple->new; |
20
|
1
|
|
|
|
|
6
|
return $self; |
21
|
|
|
|
|
|
|
} |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
sub dispatch { |
24
|
10
|
|
|
10
|
0
|
16
|
my ($app, $context) = @_; |
25
|
10
|
|
|
|
|
106
|
my $router = $app->{router}; |
26
|
10
|
|
|
|
|
34
|
my $req = $context->get('req'); |
27
|
10
|
|
|
|
|
67
|
my $env = $req->env; |
28
|
10
|
50
|
|
|
|
66
|
my $res = $app->dsl('res_404') ? $app->dsl('res_404')->() : [404, [], ['not found']]; |
29
|
10
|
50
|
|
|
|
117
|
if (my $p = $router->match($env) ) { |
30
|
10
|
|
|
|
|
1157
|
my $action = delete $p->{action}; |
31
|
10
|
|
|
|
|
37
|
$context->set(path_param => $p); |
32
|
10
|
|
|
|
|
68
|
$res = $action->($app, $context); |
33
|
|
|
|
|
|
|
} |
34
|
10
|
|
|
|
|
116
|
$context->set(res => $res); |
35
|
10
|
|
|
|
|
62
|
return $context; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub path_param { |
39
|
20
|
|
|
20
|
1
|
136
|
my ($self, $context) = @_; |
40
|
|
|
|
|
|
|
return sub (;$) { |
41
|
2
|
|
|
2
|
|
11
|
my $path_param = $context->get('path_param'); |
42
|
2
|
50
|
|
|
|
25
|
$_[0] ? $path_param->{$_[0]} : $path_param; |
43
|
20
|
|
|
|
|
252
|
}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub path { |
47
|
80
|
|
|
80
|
0
|
119
|
my ($self, $context, $method) = @_; |
48
|
80
|
|
|
|
|
196
|
my $router = $self->app->{router}; |
49
|
|
|
|
|
|
|
return sub ($;@) { |
50
|
60
|
|
|
60
|
|
5191
|
my ($path, $code) = @_; |
51
|
60
|
100
|
|
|
|
194
|
my @pathes = ref($path) eq 'ARRAY' ? @$path : ( $path ); |
52
|
60
|
|
|
|
|
368
|
$router->connect($_, {action => $code}, {method => $method}) for @pathes; |
53
|
80
|
|
|
|
|
765
|
}; |
54
|
|
|
|
|
|
|
} |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
{ |
57
|
2
|
|
|
2
|
|
25
|
no strict qw/refs/; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
248
|
|
58
|
|
|
|
|
|
|
my %methods = (get => 'GET', post => 'POST', put => 'PUT', del => 'DELETE'); |
59
|
|
|
|
|
|
|
for my $dsl (keys %methods) { |
60
|
|
|
|
|
|
|
*$dsl = sub { |
61
|
80
|
|
|
80
|
|
576
|
my ($self, $context) = @_; |
62
|
80
|
|
|
|
|
199
|
$self->path($context, $methods{$dsl}); |
63
|
|
|
|
|
|
|
}; |
64
|
|
|
|
|
|
|
}; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
1; |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
__END__ |