1 |
package Frey::CouchAPI; |
package Frey::CouchAPI; |
2 |
|
|
3 |
|
=head1 DESCRIPTION |
4 |
|
|
5 |
|
This is REST wrapper using following L<Mojo> implement Apache's CouchDB API |
6 |
|
|
7 |
|
|
8 |
|
L<Mojo::URL> |
9 |
|
|
10 |
|
L<Mojo::Transaction> |
11 |
|
|
12 |
|
|
13 |
|
=head1 Supported HTTP API |
14 |
|
|
15 |
|
=cut |
16 |
|
|
17 |
use warnings; |
use warnings; |
18 |
use strict; |
use strict; |
19 |
|
|
24 |
use Storable; |
use Storable; |
25 |
|
|
26 |
our $VERSION = '0.1'; |
our $VERSION = '0.1'; |
27 |
$VERSION .= '-Frey-' . $Frey::VERSION; |
$VERSION .= ' on Frey ' . $Frey::VERSION; |
28 |
|
|
29 |
our $debug = $Frey::debug || 0; |
our $debug = $Frey::debug || 0; |
30 |
|
|
42 |
} |
} |
43 |
} |
} |
44 |
|
|
45 |
my $path = '/data/webpac2/var/ds'; |
our $config = { |
46 |
|
path => '/data/webpac2/var/row', |
47 |
|
}; |
48 |
|
|
49 |
|
my $p = $config->{path}; |
50 |
my @all_dbs = map { |
my @all_dbs = map { |
51 |
s{^\Q$path\E/*}{}; |
s{^\Q$p\E/*}{}; |
52 |
$_; |
$_; |
53 |
} glob "$path/*/*"; |
} glob "$p/*/*"; |
54 |
|
|
55 |
my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+'; |
my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+'; |
56 |
|
|
57 |
our $json = {}; |
our $json = {}; |
58 |
our $status = 500; |
our $status; |
59 |
|
|
60 |
sub ok { |
sub ok { |
61 |
$json = { ok => JSON::true }; |
$json = { ok => JSON::true }; |
62 |
$status = 200; |
$status = 200; |
63 |
|
warn "ok\n"; |
64 |
} |
} |
65 |
|
|
66 |
|
|
67 |
sub dispatch { |
sub dispatch { |
68 |
my ($self,$tx) = @_; |
my ($self,$tx) = @_; |
69 |
|
|
70 |
|
$status = 500; # Internal Error |
71 |
|
|
72 |
my $url = $tx->req->url->to_string; |
my $url = $tx->req->url->to_string; |
73 |
$url = uri_unescape( $url ); |
$url = uri_unescape( $url ); |
74 |
my $method = $tx->req->method; |
my $method = $tx->req->method; |
75 |
|
|
76 |
if ( $url eq '/' ) { |
if ( $url eq '/' ) { |
77 |
$json = { |
$json = { |
78 |
couchdb => "Welcome", |
couchdb => "Welcome", |
79 |
version => $VERSION, |
version => "CouchAPI $VERSION", |
80 |
} |
}; |
81 |
|
$status = 200; |
82 |
} elsif ( $url eq '/_all_dbs' ) { |
} elsif ( $url eq '/_all_dbs' ) { |
83 |
$json = [ @all_dbs ]; |
$json = [ @all_dbs ]; |
84 |
$status = 200; |
$status = 200; |
85 |
} elsif ( $url =~ m{^/_config} ) { |
} elsif ( $url =~ m{^/_config/?(.+)} ) { |
86 |
$json = { |
|
87 |
couchdb => { |
$json = { CouchAPI => $config }; |
88 |
version => $VERSION, |
|
89 |
path => $path, |
if ( $method eq 'PUT' ) { |
90 |
|
|
91 |
|
my $part = $1; |
92 |
|
warn "## part $part"; |
93 |
|
|
94 |
|
$part =~ s!^!->{'!; |
95 |
|
$part =~ s!/!'}->{'!; |
96 |
|
$part =~ s/$/'}/; |
97 |
|
|
98 |
|
my $data = $tx->req->content->file->slurp; |
99 |
|
$data = JSON->new->allow_nonref->decode( $data ); |
100 |
|
warn "## data ",dump( $data ); |
101 |
|
# poor man's transaction :-) |
102 |
|
my $code = "\$json$part = \$data; \$config$part = \$data;"; |
103 |
|
eval $code; |
104 |
|
if ( $@ ) { |
105 |
|
warn "ERROR: $code -> $@"; |
106 |
|
$status = 500; |
107 |
|
} else { |
108 |
|
$status = 200; |
109 |
} |
} |
110 |
}; |
|
111 |
$status = 200; |
warn "json ",dump( $json ), " config ", dump( $config ); |
112 |
|
|
113 |
|
} elsif ( $method eq 'GET' ) { |
114 |
|
$status = 200; |
115 |
|
} else { |
116 |
|
$status = 501; |
117 |
|
} |
118 |
|
|
119 |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
120 |
|
|
121 |
|
=head2 Database |
122 |
|
|
123 |
|
L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction |
124 |
|
|
125 |
|
=cut |
126 |
|
|
127 |
my $database = $1; |
my $database = $1; |
128 |
my $dir = "$path/$database"; |
my $dir = "$config->{path}/$database"; |
129 |
|
|
130 |
if ( $method eq 'GET' ) { |
if ( $method eq 'GET' ) { |
131 |
$json = database_get( $database ); |
$json = database_get( $database ); |
133 |
if ( ! -e $dir ) { |
if ( ! -e $dir ) { |
134 |
$status = 404; |
$status = 404; |
135 |
} else { |
} else { |
136 |
remove_tree($dir) && ok || { $status = 501 }; |
remove_tree($dir) && ok || { $status = 500 }; |
137 |
} |
} |
138 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
139 |
if ( ! -e $dir ) { |
if ( ! -e $dir ) { |
140 |
make_path($dir) && ok && warn "created $dir" || { $status = 501 }; |
make_path($dir) && ok && warn "created $dir" || { $status = 500 }; |
141 |
} else { |
} else { |
142 |
$status = 412; |
$status = 412; |
143 |
} |
} |
146 |
} elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) { |
} elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) { |
147 |
my ($database,$id,$args) = ($1,$2,$3); |
my ($database,$id,$args) = ($1,$2,$3); |
148 |
|
|
149 |
|
=head2 Document |
150 |
|
|
151 |
|
L<http://wiki.apache.org/couchdb/HTTP_Document_API> |
152 |
|
|
153 |
|
=cut |
154 |
|
|
155 |
my $arg; |
my $arg; |
156 |
if ( $args ) { |
if ( $args ) { |
157 |
foreach my $a ( split(/[&;]/,$args) ) { |
foreach my $a ( split(/[&;]/,$args) ) { |
160 |
$arg->{$n} = $v; |
$arg->{$n} = $v; |
161 |
} |
} |
162 |
} |
} |
163 |
|
|
164 |
|
my $path = $config->{path}; |
165 |
|
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
166 |
|
|
167 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
168 |
warn "## database: $database id: $id -> $p [$args]\n"; |
warn "## database: $database id: $id -> $p ", dump( $arg ),"\n"; |
169 |
|
|
170 |
|
|
171 |
if ( $id =~ m{_all_docs(\w+)?$} ) { |
if ( $id =~ m{_all_docs(\w+)?$} ) { |
173 |
my $by = $1; |
my $by = $1; |
174 |
my $offset = 0; |
my $offset = 0; |
175 |
my $startkey = delete $arg->{startkey}; |
my $startkey = delete $arg->{startkey}; |
176 |
warn "STARTKEY: $startkey\n"; |
my $endkey = delete $arg->{endkey}; |
177 |
|
my $limit = delete $arg->{limit}; |
178 |
my $total_rows = 0; |
my $total_rows = 0; |
179 |
|
|
180 |
my @docs = grep { length $_ } map { |
my @docs = grep { length $_ } map { |
181 |
|
return '' if defined $limit && $total_rows == $limit; |
182 |
|
|
183 |
s{^$path/$database/}{}; |
s{^$path/$database/}{}; |
184 |
|
return '' if defined $endkey && $_ gt $endkey; |
185 |
|
|
186 |
if ( $startkey ) { |
if ( $startkey ) { |
187 |
if ( $_ >= $startkey ) { |
if ( $_ ge $startkey ) { |
188 |
$total_rows++; |
$total_rows++; |
189 |
$_; |
$_; |
190 |
} else { |
} else { |
191 |
$offset++; |
$offset++; |
192 |
|
return ''; |
193 |
} |
} |
194 |
} else { |
} else { |
195 |
$total_rows++; |
$total_rows++; |
196 |
$_; |
$_; |
197 |
} |
} |
198 |
|
|
199 |
} glob( "$path/$database/*" ); |
} glob( "$path/$database/*" ); |
200 |
|
|
201 |
warn "## docs ", dump( @docs ); |
warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug; |
202 |
|
|
203 |
$json = { |
$json = { |
204 |
total_rows => $total_rows, |
total_rows => $total_rows, |
230 |
my $descending = delete $arg->{descending}; |
my $descending = delete $arg->{descending}; |
231 |
my @sorted = sort @ids; |
my @sorted = sort @ids; |
232 |
|
|
233 |
|
warn "creating rows in ", $descending ? "descending" : "", " order\n"; |
234 |
|
|
235 |
foreach my $id ( $descending ? reverse @sorted : @sorted ) { |
foreach my $id ( $descending ? reverse @sorted : @sorted ) { |
236 |
warn ">> $id ", $descending ? 'desc' : 'asc', "\n"; |
warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug; |
237 |
push @{ $json->{rows} }, $rows->{$id}; |
push @{ $json->{rows} }, $rows->{$id}; |
238 |
} |
} |
239 |
|
|
245 |
|
|
246 |
Storable::store( from_json($data), $p ); |
Storable::store( from_json($data), $p ); |
247 |
warn "store $p ", -s $p, " bytes: $data\n"; |
warn "store $p ", -s $p, " bytes: $data\n"; |
248 |
|
$status = 201; # Created |
249 |
|
|
250 |
} elsif ( $method eq 'GET' ) { |
} elsif ( $method eq 'GET' ) { |
251 |
if ( ! -e $p ) { |
if ( ! -e $p ) { |
252 |
$status = 404; |
$status = 404; |
256 |
} |
} |
257 |
} elsif ( $method eq 'DELETE' ) { |
} elsif ( $method eq 'DELETE' ) { |
258 |
if ( -e $p ) { |
if ( -e $p ) { |
259 |
unlink $p || { $status = 501 }; |
unlink $p || { $status = 500 }; |
260 |
} else { |
} else { |
261 |
$status = 404; |
$status = 404; |
262 |
} |
} |
268 |
|
|
269 |
} |
} |
270 |
|
|
271 |
if ( $status >= 400 && $status < 500 && ! defined $json) { |
$json = { error => 'not_found', reason => 'Missing' } if $status == 404; |
272 |
$json = { error => 'not_found', reason => 'Missing' }; |
|
273 |
warn "fake $status"; |
if ( $method =~ m{(DELETE|PUT)} ) { |
274 |
|
$tx->res->headers->add_line( 'Location' => $tx->req->url->to_abs ); |
275 |
} |
} |
276 |
|
|
277 |
$tx->res->code( $status ); |
$tx->res->code( $status ); |
279 |
my $body = to_json $json; |
my $body = to_json $json; |
280 |
$tx->res->body( $body ); |
$tx->res->body( $body ); |
281 |
warn "CouchDB API: $method $url $status $body\n"; |
warn "CouchDB API: $method $url $status $body\n"; |
282 |
|
|
283 |
|
warn "## headers ", $tx->res->headers->to_string; |
284 |
|
|
285 |
return $tx; |
return $tx; |
286 |
|
|
287 |
} |
} |
288 |
|
|
289 |
sub database_get { |
sub database_get { |
290 |
my ($db_name) = @_; |
my ($db_name) = @_; |
291 |
|
my $path = $config->{path}; |
292 |
warn "# collecting docs from $path/$db_name/*\n"; |
warn "# collecting docs from $path/$db_name/*\n"; |
293 |
my @docs = glob "$path/$db_name/*"; |
my @docs = glob "$path/$db_name/*"; |
294 |
my $json = { |
my $json = { |
309 |
} |
} |
310 |
|
|
311 |
1; |
1; |
312 |
|
__END__ |
313 |
|
|
314 |
|
=head1 SEE ALSO |
315 |
|
|
316 |
|
L<http://wiki.apache.org/couchdb/Reference> |
317 |
|
|