line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::RestAPI; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
64940
|
use 5.006; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
72
|
|
4
|
2
|
|
|
2
|
|
7
|
use strict; |
|
2
|
|
|
|
|
13
|
|
|
2
|
|
|
|
|
90
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings FATAL => 'all'; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
95
|
|
6
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
406
|
use parent qw( Plack::Middleware ); |
|
2
|
|
|
|
|
237
|
|
|
2
|
|
|
|
|
10
|
|
8
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
13101
|
use HTTP::Exception '4XX'; |
|
2
|
|
|
|
|
7620
|
|
|
2
|
|
|
|
|
10
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 NAME |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Plack::Middleware::RestAPI - Perl PSGI middleware that just call GET, PUT, POST, DELETE from mounted class. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 VERSION |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Version 0.01 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
=head1 SYNOPSIS |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
use Plack::Middleware::RestAPI; |
27
|
|
|
|
|
|
|
use Test::Root; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
builder { |
30
|
|
|
|
|
|
|
mount "/api" => builder { |
31
|
|
|
|
|
|
|
enable 'RestAPI'; |
32
|
|
|
|
|
|
|
mount "/" => sub { 'Test::Root' }; |
33
|
|
|
|
|
|
|
}; |
34
|
|
|
|
|
|
|
}; |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
package Test::Root; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub GET { |
39
|
|
|
|
|
|
|
return [ 200, [ 'Content-Type' => 'text/plain' ], [ 'app/root' ] ]; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=head1 DESCRIPTION |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Plack::Middleware::RestAPI is simple middleware that call requested method directly from mounted class. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
Method can be GET, PUT, POST, DELETE, HEAD. |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
For complete RestAPI in Perl use: |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
=over 4 |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=item * Plack::Middleware::ParseContent |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=item * Plack::Middleware::SetAccept |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=item * Plack::Middleware::FormatOutput |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=back |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=cut |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub call { |
63
|
3
|
|
|
3
|
1
|
20990
|
my($self, $env) = @_; |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Get class |
66
|
3
|
|
|
|
|
14
|
my $class = $self->app->($env); |
67
|
3
|
50
|
|
|
|
317
|
return $class if ref $class; # Return if returned value is not string |
68
|
|
|
|
|
|
|
|
69
|
3
|
|
|
|
|
6
|
my $method = $env->{REQUEST_METHOD}; |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Throw an exception if method is not defined |
72
|
3
|
100
|
|
|
|
29
|
if (!UNIVERSAL::can($class, $method)){ |
73
|
1
|
|
|
|
|
17
|
HTTP::Exception::405->throw(); |
74
|
|
|
|
|
|
|
} |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# Set rest api class to env |
77
|
2
|
|
|
|
|
5
|
$env->{'restapi.class'} = $class; |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# compatibility with Plack::Middleware::ParseContent |
80
|
2
|
50
|
|
|
|
8
|
my $data = $env->{'restapi.parseddata'} if exists $env->{'restapi.parseddata'}; |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Call method |
83
|
2
|
|
|
|
|
3
|
my $ret; |
84
|
2
|
|
|
2
|
|
69268
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
93
|
|
85
|
2
|
|
|
|
|
10
|
$ret = "${class}::${method}"->($env, $data); |
86
|
2
|
|
|
2
|
|
9
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
200
|
|
87
|
|
|
|
|
|
|
|
88
|
2
|
|
|
|
|
28
|
return $ret; |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head1 STORED PARAMS TO ENV (Fulfill the PSGI specification) |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=over 4 |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=item restapi.class |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Store name of called class. |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=back |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=head1 TUTORIAL |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
L |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 AUTHOR |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Vaclav Dovrtel, C<< >> |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 BUGS |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Please report any bugs or feature requests to github repository. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
Inspired by L |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=head1 REPOSITORY |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
L |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Copyright 2015 Vaclav Dovrtel. |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
126
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
127
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
See L for more information. |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=cut |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
1; # End of Plack::Middleware::RestAPI |