/[Frey]/branches/zimbardo/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

Diff of /branches/zimbardo/lib/Frey/CouchAPI.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1051 by dpavlin, Thu Apr 23 19:35:26 2009 UTC revision 1054 by dpavlin, Thu Apr 23 21:06:48 2009 UTC
# Line 2  package Frey::CouchAPI; Line 2  package Frey::CouchAPI;
2    
3  =head1 DESCRIPTION  =head1 DESCRIPTION
4    
5  This is REST wrapper using L<Mojo::Transaction> to implement Apache's CouchDB API  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  =head1 Supported HTTP API
14    
# Line 17  use URI::Escape; Line 23  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.1';  our $VERSION = '0.2';
27  $VERSION .= ' on Frey ' . $Frey::VERSION;  $VERSION .= " (Frey $Frey::VERSION)" if $Frey::VERSION;
28    
29  our $debug = $Frey::debug || 0;  our $debug = $Frey::debug || 0;
30    
# Line 49  my @all_dbs = map { Line 55  my @all_dbs = map {
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 from ",join(' ',caller),$/;
64  }  }
65    
66    sub file_rev { (stat($_[0]))[9] } # mtime
67    
68  sub dispatch {  sub dispatch {
69          my ($self,$tx) = @_;          my ($self,$tx) = @_;
70    
71            $status = 500; # Internal Error
72    
73          my $url = $tx->req->url->to_string;          my $url = $tx->req->url->to_string;
74          $url = uri_unescape( $url );          $url = uri_unescape( $url );
75          my $method = $tx->req->method;          my $method = $tx->req->method;
76            my $path = $config->{path};
77                    
78          if ( $url eq '/' ) {          if ( $url eq '/' ) {
79                  $json = {                  $json = {
80                          couchdb => "Welcome",                          couchdb => "Welcome",
81                          version => "CouchAPI $VERSION",                          version => "CouchAPI $VERSION",
82                  }                  };
83                    $status = 200;
84          } elsif ( $url eq '/_all_dbs' ) {          } elsif ( $url eq '/_all_dbs' ) {
85                  $json = [ @all_dbs ];                  $json = [ @all_dbs ];
86                  $status = 200;                  $status = 200;
# Line 115  L<http://wiki.apache.org/couchdb/HTTP_da Line 127  L<http://wiki.apache.org/couchdb/HTTP_da
127  =cut  =cut
128    
129                  my $database = $1;                  my $database = $1;
130                  my $dir = "$config->{path}/$database";  
131                    my $dir = "$path/$database";
132    
133                  if ( $method eq 'GET' ) {                  if ( $method eq 'GET' ) {
134                          $json = database_get( $database );                          $json = database_get( $database );
# Line 123  L<http://wiki.apache.org/couchdb/HTTP_da Line 136  L<http://wiki.apache.org/couchdb/HTTP_da
136                          if ( ! -e $dir ) {                          if ( ! -e $dir ) {
137                                  $status = 404;                                  $status = 404;
138                          } else {                          } else {
139                                  remove_tree($dir) && ok || { $status = 501 };                                  remove_tree($dir);
140                                    if ( ! -d $dir ) {
141                                            ok;
142                                    } else {
143                                            $status = 500;
144                                    }
145                          }                          }
146                  } elsif ( $method eq 'PUT' ) {                  } elsif ( $method eq 'PUT' ) {
147                          if ( ! -e $dir ) {                          if ( -e $dir ) {
                                 make_path($dir) && ok && warn "created $dir" || { $status = 501 };  
                         } else {  
148                                  $status = 412;                                  $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    
# Line 151  L<http://wiki.apache.org/couchdb/HTTP_Do Line 175  L<http://wiki.apache.org/couchdb/HTTP_Do
175                          }                          }
176                  }                  }
177    
                 my $path = $config->{path};  
178                  warn "ERROR: path $path doesn't exist\n" unless -e $path;                  warn "ERROR: path $path doesn't exist\n" unless -e $path;
179    
180                  my $p = "$path/$database/$id";                  my $p = "$path/$database/$id";
# Line 212  L<http://wiki.apache.org/couchdb/HTTP_Do Line 235  L<http://wiki.apache.org/couchdb/HTTP_Do
235                                          id => $id,                                          id => $id,
236                                          key => $id,                                          key => $id,
237                                          value => {                                          value => {
238                                                  rev => (stat($p))[9], # mtime                                                  rev => file_rev $p,
239                                          }                                          }
240                                  };                                  };
241                          }                          }
# Line 229  L<http://wiki.apache.org/couchdb/HTTP_Do Line 252  L<http://wiki.apache.org/couchdb/HTTP_Do
252    
253                  } elsif ( $method eq 'PUT' ) {                  } elsif ( $method eq 'PUT' ) {
254                    
255                          warn "## ",dump( $tx->req ) if $debug;                          warn "## ",dump( $tx->req ); # if $debug;
256    
257                          my $data = $tx->req->content->file->slurp;                          my $data = $tx->req->content->file->slurp;
258    
259                          Storable::store( from_json($data), $p );                          Storable::store( from_json($data), $p );
260                          warn "store $p ", -s $p, " bytes: $data\n";                          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' ) {                  } elsif ( $method eq 'GET' ) {
271                          if ( ! -e $p ) {                          if ( ! -e $p ) {
272                                  $status = 404;                                  $status = 404;
# Line 244  L<http://wiki.apache.org/couchdb/HTTP_Do Line 276  L<http://wiki.apache.org/couchdb/HTTP_Do
276                          }                          }
277                  } elsif ( $method eq 'DELETE' ) {                  } elsif ( $method eq 'DELETE' ) {
278                          if ( -e $p ) {                          if ( -e $p ) {
279                                  unlink $p || { $status = 501 };                                  unlink $p && ok || { $status = 500 };
280                          } else {                          } else {
281                                  $status = 404;                                  $status = 404;
282                          }                          }
# Line 256  L<http://wiki.apache.org/couchdb/HTTP_Do Line 288  L<http://wiki.apache.org/couchdb/HTTP_Do
288    
289          }          }
290    
291          if ( $status >= 400 && $status < 500 && ! defined $json) {          $json = { error => 'not_found', reason => 'Missing' } if $status == 404;
292                  $json = { error => 'not_found', reason => 'Missing' };  
293                  warn "fake $status";          if ( $method =~ m{(DELETE|PUT)} ) {
294                    $tx->res->headers->add_line( 'Location' => $tx->req->url->to_abs );
295          }          }
296    
297          $tx->res->code( $status );          $tx->res->code( $status );
298          $tx->res->headers->content_type( 'text/json' );          $tx->res->headers->content_type( 'text/plain;charset=utf-8' );
299          my $body = to_json $json;          my $body = to_json $json;
300          $tx->res->body( $body );          $tx->res->body( $body );
301          warn "CouchDB API: $method $url $status $body\n";          $tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' );
302            $tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" );
303    
304            print "$method $url $status\n$body\n";
305    
306            warn "## headers ", $tx->res->headers->to_string;
307    
308          return $tx;          return $tx;
309    
310  }  }

Legend:
Removed from v.1051  
changed lines
  Added in v.1054

  ViewVC Help
Powered by ViewVC 1.1.26