/[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

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

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

revision 1053 by dpavlin, Thu Apr 23 20:24:48 2009 UTC revision 1054 by dpavlin, Thu Apr 23 21:06:48 2009 UTC
# Line 60  our $status; Line 60  our $status;
60  sub ok {  sub ok {
61          $json = { ok => JSON::true };          $json = { ok => JSON::true };
62          $status = 200;          $status = 200;
63          warn "ok\n";          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) = @_;
# Line 72  sub dispatch { Line 73  sub dispatch {
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 = {
# Line 125  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 133  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 = 500 };                                  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 = 500 };  
                         } 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 161  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 222  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 239  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                          $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 256  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 = 500 };                                  unlink $p && ok || { $status = 500 };
280                          } else {                          } else {
281                                  $status = 404;                                  $status = 404;
282                          }                          }
# Line 281  L<http://wiki.apache.org/couchdb/HTTP_Do Line 301  L<http://wiki.apache.org/couchdb/HTTP_Do
301          $tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' );          $tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' );
302          $tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" );          $tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" );
303    
304          warn "INFO CouchDB API $method $url $status\n$body\n";          print "$method $url $status\n$body\n";
305    
306          warn "## headers ", $tx->res->headers->to_string;          warn "## headers ", $tx->res->headers->to_string;
307    

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

  ViewVC Help
Powered by ViewVC 1.1.26