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