/[Frey]/trunk/lib/Frey/CouchAPI.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/Frey/CouchAPI.pm

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.26