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