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; |
18 |
|
use strict; |
19 |
|
|
20 |
use JSON; |
use JSON; |
21 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
22 |
use URI::Escape; |
use URI::Escape; |
23 |
use File::Path qw(make_path remove_tree); |
use File::Path qw(make_path remove_tree); |
24 |
use Storable; |
use Storable; |
25 |
|
|
26 |
|
our $VERSION = '0.2'; |
27 |
|
$VERSION .= " (Frey $Frey::VERSION)" if $Frey::VERSION; |
28 |
|
|
29 |
|
our $debug = $Frey::debug || 0; |
30 |
|
|
31 |
sub rewrite_urls { |
sub rewrite_urls { |
32 |
my ( $self, $tx ) = @_; |
my ( $self, $tx ) = @_; |
33 |
if ( $tx->req->url->path =~ m{/_utils/} ) { |
if ( $tx->req->url->path =~ m{/_utils/} ) { |
42 |
} |
} |
43 |
} |
} |
44 |
|
|
45 |
my $path = '/data/webpac2/var/row'; |
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 $stauts = 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 from ",join(' ',caller),$/; |
64 |
|
} |
65 |
|
|
66 |
|
sub file_rev { (stat($_[0]))[9] } # mtime |
67 |
|
|
68 |
|
sub data_from_tx { |
69 |
|
my $tx = shift; |
70 |
|
my $data = $tx->req->content->file->slurp; |
71 |
|
$data = JSON->new->allow_nonref->decode( $data ); |
72 |
|
warn "## data ",dump( $data ); |
73 |
|
return $data; |
74 |
} |
} |
75 |
|
|
76 |
sub dispatch { |
sub dispatch { |
77 |
my ($self,$tx) = @_; |
my ($self,$tx) = @_; |
78 |
|
|
79 |
|
$status = 500; # Internal Error |
80 |
|
|
81 |
my $url = $tx->req->url->to_string; |
my $url = $tx->req->url->to_string; |
82 |
$url = uri_unescape( $url ); |
$url = uri_unescape( $url ); |
83 |
my $method = $tx->req->method; |
my $method = $tx->req->method; |
84 |
|
my $path = $config->{path}; |
85 |
warn "INFO: using Apache CouchDB emulation API\n"; |
|
|
|
|
86 |
if ( $url eq '/' ) { |
if ( $url eq '/' ) { |
87 |
$json = { |
$json = { |
88 |
couchdb => "Welcome", |
couchdb => "Welcome", |
89 |
version => "0-Frey", |
version => "CouchAPI $VERSION", |
90 |
} |
}; |
91 |
|
$status = 200; |
92 |
} elsif ( $url eq '/_all_dbs' ) { |
} elsif ( $url eq '/_all_dbs' ) { |
93 |
$json = [ @all_dbs ]; |
$json = [ @all_dbs ]; |
94 |
$status = 200; |
$status = 200; |
95 |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
} elsif ( $url =~ m{^/_config/?(.+)} ) { |
96 |
|
|
97 |
|
$json = { CouchAPI => $config }; |
98 |
|
|
99 |
|
if ( $method eq 'PUT' ) { |
100 |
|
|
101 |
|
my $part = $1; |
102 |
|
warn "## part $part"; |
103 |
|
|
104 |
|
$part =~ s!^!->{'!; |
105 |
|
$part =~ s!/!'}->{'!; |
106 |
|
$part =~ s/$/'}/; |
107 |
|
|
108 |
|
my $data = data_from_tx( $tx ); |
109 |
|
# poor man's transaction :-) |
110 |
|
my $code = "\$json$part = \$data; \$config$part = \$data;"; |
111 |
|
eval $code; |
112 |
|
if ( $@ ) { |
113 |
|
warn "ERROR: $code -> $@"; |
114 |
|
$status = 500; |
115 |
|
} else { |
116 |
|
$status = 200; |
117 |
|
} |
118 |
|
|
119 |
|
warn "json ",dump( $json ), " config ", dump( $config ); |
120 |
|
|
121 |
|
} elsif ( $method eq 'GET' ) { |
122 |
|
$status = 200; |
123 |
|
} else { |
124 |
|
$status = 501; |
125 |
|
} |
126 |
|
|
127 |
|
=head2 Database |
128 |
|
|
129 |
|
L<http://wiki.apache.org/couchdb/HTTP_database_API> except compaction |
130 |
|
|
131 |
|
=cut |
132 |
|
|
133 |
|
} elsif ( |
134 |
|
$url =~ m{($regex_dbs)/$} |
135 |
|
# DELETE doesn't have trailing slash |
136 |
|
|| $method eq 'DELETE' && $url =~ m{($regex_dbs)$} |
137 |
|
) { |
138 |
|
|
139 |
my $database = $1; |
my $database = $1; |
140 |
|
|
141 |
my $dir = "$path/$database"; |
my $dir = "$path/$database"; |
142 |
|
|
143 |
if ( $method eq 'GET' ) { |
if ( $method eq 'GET' ) { |
146 |
if ( ! -e $dir ) { |
if ( ! -e $dir ) { |
147 |
$status = 404; |
$status = 404; |
148 |
} else { |
} else { |
149 |
remove_tree($dir) && ok || { $status = 501 }; |
remove_tree($dir); |
150 |
|
if ( ! -d $dir ) { |
151 |
|
ok; |
152 |
|
} else { |
153 |
|
$status = 500; |
154 |
|
} |
155 |
} |
} |
156 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
157 |
if ( ! -e $dir ) { |
if ( -e $dir ) { |
|
make_path($dir) && ok && warn "created $dir" || { $status = 501 }; |
|
|
} else { |
|
158 |
$status = 412; |
$status = 412; |
159 |
|
} else { |
160 |
|
make_path($dir); |
161 |
|
if ( -e $path ) { |
162 |
|
ok; |
163 |
|
$status = 201; |
164 |
|
} else { |
165 |
|
$status = 500; |
166 |
|
} |
167 |
} |
} |
168 |
} |
} |
169 |
|
|
170 |
} elsif ( $url =~ m{($regex_dbs)/(.+)$} ) { |
warn "## database $database $status ",dump( $json ); |
171 |
my ($database,$id) = ($1,$2); |
|
172 |
|
} elsif ( $url =~ m{($regex_dbs)/([^?]+)\??(.+)?$} ) { |
173 |
|
my ($database,$id,$args) = ($1,$2,$3); |
174 |
|
|
175 |
|
=head2 Document |
176 |
|
|
177 |
|
L<http://wiki.apache.org/couchdb/HTTP_Document_API> |
178 |
|
|
179 |
|
=cut |
180 |
|
|
181 |
|
my $arg; |
182 |
|
if ( $args ) { |
183 |
|
foreach my $a ( split(/[&;]/,$args) ) { |
184 |
|
my ($n,$v) = split(/=/,$a); |
185 |
|
$v =~ s{(["'])(.+)\1}{$2}; |
186 |
|
$arg->{$n} = $v; |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
191 |
|
|
192 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
193 |
warn "## database: $database id: $id -> $p "; |
warn "## database: $database id: $id -> $p ", dump( $arg ),"\n"; |
194 |
|
|
|
if ( $id eq '_all_docs' ) { |
|
195 |
|
|
196 |
my @docs = map { |
if ( $id =~ m{_all_docs(\w*)$} ) { |
197 |
s{^$path/$database/}{}; |
|
198 |
$_; |
my $by = $1; |
199 |
|
my $offset = 0; |
200 |
|
my $startkey = delete $arg->{startkey}; |
201 |
|
my $endkey = delete $arg->{endkey}; |
202 |
|
my $limit = delete $arg->{limit}; |
203 |
|
my $skip = delete $arg->{skip}; |
204 |
|
my $total_rows = 0; |
205 |
|
|
206 |
|
my @docs = grep { length $_ } map { |
207 |
|
|
208 |
|
if ( $limit > 0 && $total_rows == $limit ) { |
209 |
|
''; |
210 |
|
} else { |
211 |
|
|
212 |
|
s{^$path/$database/}{}; |
213 |
|
|
214 |
|
if ( defined $endkey && $_ gt $endkey ) { |
215 |
|
''; |
216 |
|
} elsif ( $startkey ) { |
217 |
|
if ( $_ ge $startkey ) { |
218 |
|
$total_rows++; |
219 |
|
$_; |
220 |
|
} else { |
221 |
|
$offset++; |
222 |
|
''; |
223 |
|
} |
224 |
|
} else { |
225 |
|
$total_rows++; |
226 |
|
$_; |
227 |
|
} |
228 |
|
} |
229 |
|
|
230 |
} glob( "$path/$database/*" ); |
} glob( "$path/$database/*" ); |
231 |
|
|
232 |
warn "## docs ", dump( @docs ); |
$offset += $skip if $skip; |
233 |
|
|
234 |
|
warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug; |
235 |
|
|
236 |
$json = { |
$json = { |
237 |
total_rows => $#docs + 1, |
total_rows => $total_rows, |
238 |
offset => 0, |
offset => $offset, |
239 |
rows => [], |
rows => [], |
240 |
}; |
}; |
241 |
|
|
242 |
|
my $rows; |
243 |
|
my @ids; |
244 |
|
|
245 |
foreach my $id ( @docs ) { |
foreach my $id ( @docs ) { |
246 |
warn "++ $id\n"; |
warn "++ $id\n" if $debug; |
247 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
248 |
my $data = Storable::retrieve( $p ); |
my $data = eval { Storable::retrieve( $p ) }; |
249 |
push @{ $json->{rows} }, { |
if ( $@ ) { |
250 |
|
warn "ERROR: $p | $@\n"; |
251 |
|
next; |
252 |
|
} |
253 |
|
push @ids, $id; |
254 |
|
$rows->{$id} = { |
255 |
id => $id, |
id => $id, |
256 |
key => $id, |
key => $id, |
257 |
value => { |
value => { |
258 |
rev => (stat($p))[9], # mtime |
rev => file_rev $p, |
259 |
} |
} |
260 |
}; |
}; |
261 |
} |
} |
262 |
|
|
263 |
|
my $descending = delete $arg->{descending}; |
264 |
|
my @sorted = sort @ids; |
265 |
|
|
266 |
|
warn "creating rows in ", $descending ? "descending" : "", " order\n"; |
267 |
|
|
268 |
|
foreach my $id ( $descending ? reverse @sorted : @sorted ) { |
269 |
|
warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug; |
270 |
|
push @{ $json->{rows} }, $rows->{$id}; |
271 |
|
} |
272 |
|
|
273 |
|
$status = 200; |
274 |
|
|
275 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
276 |
|
|
277 |
warn "## ",dump( $tx->req ); |
warn "## ",dump( $tx->req ) if $debug; |
278 |
|
|
279 |
my $data = $tx->req->content->file->slurp; |
my $data = $tx->req->content->file->slurp; |
280 |
|
|
281 |
|
my $db_path = "$path/$database"; |
282 |
|
make_path $db_path unless -e $db_path; |
283 |
|
|
284 |
Storable::store( from_json($data), $p ); |
Storable::store( from_json($data), $p ); |
285 |
warn "store $p ", -s $p, " bytes: $data\n"; |
my $rev = file_rev $p; |
286 |
|
warn "store $p $rev size ", -s $p, " bytes | $data\n"; |
287 |
|
|
288 |
|
$status = 201; # Created |
289 |
|
$json = { |
290 |
|
id => $id, |
291 |
|
ok => JSON::true, |
292 |
|
rev => $rev, |
293 |
|
}; |
294 |
|
|
295 |
} elsif ( $method eq 'GET' ) { |
} elsif ( $method eq 'GET' ) { |
296 |
warn "retrive $p ", -s $p, " bytes\n"; |
if ( ! -e $p ) { |
297 |
$json = Storable::retrieve( $p ); |
$status = 404; |
298 |
|
} else { |
299 |
|
warn "retrive $p ", -s $p, " bytes\n"; |
300 |
|
$json = Storable::retrieve( $p ); |
301 |
|
if ( delete $arg->{revs_info} ) { |
302 |
|
my $rev = file_rev $p; |
303 |
|
$json->{_rev} = $rev; |
304 |
|
$json->{_revs_info} = [ |
305 |
|
{ rev => $rev, status => 'available' } |
306 |
|
]; |
307 |
|
} |
308 |
|
$status = 200; |
309 |
|
|
310 |
|
} |
311 |
|
} elsif ( $method eq 'DELETE' ) { |
312 |
|
if ( -e $p ) { |
313 |
|
unlink $p && ok || { $status = 500 }; |
314 |
|
} else { |
315 |
|
$status = 404; |
316 |
|
} |
317 |
|
} elsif ( $method eq 'POST' ) { |
318 |
|
my $data = data_from_tx( $tx ); |
319 |
|
|
320 |
|
# FIXME implement real view server and return 200 |
321 |
|
$json = { total_rows => 0, offset => 0 }; |
322 |
|
$status = 202; |
323 |
|
|
324 |
} else { |
} else { |
325 |
$status = 501; |
$status = 501; |
326 |
} |
} |
327 |
|
|
328 |
|
if ( keys %$arg ) { |
329 |
|
warn "WARNING: arg left from $url = ",dump( $arg ),$/; |
330 |
|
$status = 501; |
331 |
|
} |
332 |
|
|
333 |
} |
} |
334 |
|
|
335 |
if ( $status >= 400 && $status < 500 && ! defined $json) { |
$json = { error => 'not_found', reason => 'Missing' } if $status == 404; |
336 |
$json = { error => 'not_found', reason => 'Missing' }; |
|
337 |
warn "fake $status"; |
if ( $method =~ m{(DELETE|PUT)} ) { |
338 |
|
$tx->res->headers->add_line( 'Location' => $tx->req->url->to_abs ); |
339 |
} |
} |
340 |
|
|
341 |
$tx->res->code( $status ); |
$tx->res->code( $status ); |
342 |
$tx->res->headers->content_type( 'text/json' ); |
$tx->res->headers->content_type( 'text/plain;charset=utf-8' ); |
343 |
my $body = to_json $json; |
my $body = to_json $json; |
344 |
$tx->res->body( $body ); |
$tx->res->body( $body ); |
345 |
warn "CouchDB API: $method $url $status $body\n"; |
$tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' ); |
346 |
|
$tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" ); |
347 |
|
|
348 |
|
print "$method $url $status\n$body\n"; |
349 |
|
|
350 |
|
warn "## headers ", $tx->res->headers->to_string; |
351 |
|
|
352 |
return $tx; |
return $tx; |
353 |
|
|
354 |
} |
} |
355 |
|
|
356 |
sub database_get { |
sub database_get { |
357 |
my ($db_name) = @_; |
my ($db_name) = @_; |
358 |
|
my $path = $config->{path}; |
359 |
warn "# collecting docs from $path/$db_name/*\n"; |
warn "# collecting docs from $path/$db_name/*\n"; |
360 |
my @docs = glob "$path/$db_name/*"; |
my @docs = glob "$path/$db_name/*"; |
361 |
my $json = { |
my $json = { |
370 |
}; |
}; |
371 |
|
|
372 |
warn "## calculating disk_size\n"; |
warn "## calculating disk_size\n"; |
373 |
$json->{disk_size} += -s "$path/$1/$_" foreach $docs; |
$json->{disk_size} += -s $_ foreach @docs; |
374 |
$status = 200; |
$status = 200; |
375 |
return $json; |
return $json; |
376 |
} |
} |
377 |
|
|
378 |
1; |
1; |
379 |
|
__END__ |
380 |
|
|
381 |
|
=head1 SEE ALSO |
382 |
|
|
383 |
|
L<http://wiki.apache.org/couchdb/Reference> |
384 |
|
|