/[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 1049 by dpavlin, Thu Apr 23 17:26:04 2009 UTC revision 1050 by dpavlin, Thu Apr 23 18:45:42 2009 UTC
# Line 10  use File::Path qw(make_path remove_tree) Line 10  use File::Path qw(make_path remove_tree)
10  use Storable;  use Storable;
11    
12  our $VERSION = '0.1';  our $VERSION = '0.1';
13  $VERSION .= '-Frey-' . $Frey::VERSION;  $VERSION .= ' on Frey ' . $Frey::VERSION;
14    
15  our $debug = $Frey::debug || 0;  our $debug = $Frey::debug || 0;
16    
# Line 28  sub rewrite_urls { Line 28  sub rewrite_urls {
28          }          }
29  }  }
30    
31  my $path = '/data/webpac2/var/ds';  our $config = {
32            path => '/data/webpac2/var/row',
33    };
34    
35    my $p = $config->{path};
36  my @all_dbs = map {  my @all_dbs = map {
37          s{^\Q$path\E/*}{};          s{^\Q$p\E/*}{};
38          $_;          $_;
39  } glob "$path/*/*";  } glob "$p/*/*";
40    
41  my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+';  my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+';
42    
# Line 44  sub ok { Line 48  sub ok {
48          $status = 200;          $status = 200;
49  }  }
50    
51    
52  sub dispatch {  sub dispatch {
53          my ($self,$tx) = @_;          my ($self,$tx) = @_;
54    
55          my $url = $tx->req->url->to_string;          my $url = $tx->req->url->to_string;
56          $url = uri_unescape( $url );          $url = uri_unescape( $url );
57          my $method = $tx->req->method;          my $method = $tx->req->method;
58            
59          if ( $url eq '/' ) {          if ( $url eq '/' ) {
60                  $json = {                  $json = {
61                          couchdb => "Welcome",                          couchdb => "Welcome",
62                          version => $VERSION,                          version => "CouchAPI $VERSION",
63                  }                  }
64          } elsif ( $url eq '/_all_dbs' ) {          } elsif ( $url eq '/_all_dbs' ) {
65                  $json = [ @all_dbs ];                  $json = [ @all_dbs ];
66                  $status = 200;                  $status = 200;
67          } elsif ( $url =~ m{^/_config} ) {          } elsif ( $url =~ m{^/_config/?(.+)} ) {
68                  $json = {  
69                          couchdb => {                  $json = { CouchAPI => $config };
70                                  version => $VERSION,  
71                                  path => $path,                  if ( $method eq 'PUT' ) {
72    
73                            my $part = $1;
74                            warn "## part $part";
75    
76                            $part =~ s!^!->{'!;
77                            $part =~ s!/!'}->{'!;
78                            $part =~ s/$/'}/;
79    
80                            my $data = $tx->req->content->file->slurp;
81                            $data = JSON->new->allow_nonref->decode( $data );
82                            warn "## data ",dump( $data );
83                            # poor man's transaction :-)
84                            my $code = "\$json$part = \$data; \$config$part = \$data;";
85                            eval $code;
86                            if ( $@ ) {
87                                    warn "ERROR: $code -> $@";
88                                    $status = 500;
89                            } else {
90                                    $status = 200;
91                          }                          }
92                  };  
93                  $status = 200;  warn "json ",dump( $json ), " config ", dump( $config );
94    
95                    } elsif ( $method eq 'GET' ) {
96                            $status = 200;
97                    } else {
98                            $status = 501;
99                    }
100    
101          } elsif ( $url =~ m{($regex_dbs)/$} ) {          } elsif ( $url =~ m{($regex_dbs)/$} ) {
102    
103                  my $database = $1;                  my $database = $1;
104                  my $dir = "$path/$database";                  my $dir = "$config->{path}/$database";
105    
106                  if ( $method eq 'GET' ) {                  if ( $method eq 'GET' ) {
107                          $json = database_get( $database );                          $json = database_get( $database );
# Line 99  sub dispatch { Line 130  sub dispatch {
130                                  $arg->{$n} = $v;                                  $arg->{$n} = $v;
131                          }                          }
132                  }                  }
133            
134                    my $path = $config->{path};
135                    warn "ERROR: path $path doesn't exist\n" unless -e $path;
136    
137                  my $p = "$path/$database/$id";                  my $p = "$path/$database/$id";
138                  warn "## database: $database id: $id -> $p [$args]\n";                  warn "## database: $database id: $id -> $p [$args]\n";
139    
# Line 127  warn "STARTKEY: $startkey\n"; Line 161  warn "STARTKEY: $startkey\n";
161                                  }                                  }
162                          } glob( "$path/$database/*" );                          } glob( "$path/$database/*" );
163    
164                          warn "## docs ", dump( @docs );                          warn "## docs ", dump( @docs ) if $debug;
165    
166                          $json = {                          $json = {
167                                  total_rows =>  $total_rows,                                  total_rows =>  $total_rows,
# Line 159  warn "STARTKEY: $startkey\n"; Line 193  warn "STARTKEY: $startkey\n";
193                          my $descending = delete $arg->{descending};                          my $descending = delete $arg->{descending};
194                          my @sorted = sort @ids;                          my @sorted = sort @ids;
195    
196                            warn "creating rows in ", $descending ? "descending" : "", " order\n";
197    
198                          foreach my $id ( $descending ? reverse @sorted : @sorted ) {                          foreach my $id ( $descending ? reverse @sorted : @sorted ) {
199                                  warn ">> $id ", $descending ? 'desc' : 'asc', "\n";                                  warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug;
200                                  push @{ $json->{rows} }, $rows->{$id};                                  push @{ $json->{rows} }, $rows->{$id};
201                          }                          }
202    
# Line 209  warn "STARTKEY: $startkey\n"; Line 245  warn "STARTKEY: $startkey\n";
245    
246  sub database_get {  sub database_get {
247          my ($db_name) = @_;          my ($db_name) = @_;
248            my $path = $config->{path};
249          warn "# collecting docs from $path/$db_name/*\n";          warn "# collecting docs from $path/$db_name/*\n";
250          my @docs = glob "$path/$db_name/*";          my @docs = glob "$path/$db_name/*";
251          my $json = {          my $json = {

Legend:
Removed from v.1049  
changed lines
  Added in v.1050

  ViewVC Help
Powered by ViewVC 1.1.26