--- trunk/lib/Frey/CouchAPI.pm 2009/04/23 17:26:04 1049 +++ trunk/lib/Frey/CouchAPI.pm 2009/04/23 18:45:42 1050 @@ -10,7 +10,7 @@ use Storable; our $VERSION = '0.1'; -$VERSION .= '-Frey-' . $Frey::VERSION; +$VERSION .= ' on Frey ' . $Frey::VERSION; our $debug = $Frey::debug || 0; @@ -28,11 +28,15 @@ } } -my $path = '/data/webpac2/var/ds'; +our $config = { + path => '/data/webpac2/var/row', +}; + +my $p = $config->{path}; my @all_dbs = map { - s{^\Q$path\E/*}{}; + s{^\Q$p\E/*}{}; $_; -} glob "$path/*/*"; +} glob "$p/*/*"; my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+'; @@ -44,33 +48,60 @@ $status = 200; } + sub dispatch { my ($self,$tx) = @_; my $url = $tx->req->url->to_string; $url = uri_unescape( $url ); my $method = $tx->req->method; - + if ( $url eq '/' ) { $json = { couchdb => "Welcome", - version => $VERSION, + version => "CouchAPI $VERSION", } } elsif ( $url eq '/_all_dbs' ) { $json = [ @all_dbs ]; $status = 200; - } elsif ( $url =~ m{^/_config} ) { - $json = { - couchdb => { - version => $VERSION, - path => $path, + } elsif ( $url =~ m{^/_config/?(.+)} ) { + + $json = { CouchAPI => $config }; + + if ( $method eq 'PUT' ) { + + my $part = $1; + warn "## part $part"; + + $part =~ s!^!->{'!; + $part =~ s!/!'}->{'!; + $part =~ s/$/'}/; + + my $data = $tx->req->content->file->slurp; + $data = JSON->new->allow_nonref->decode( $data ); + warn "## data ",dump( $data ); + # poor man's transaction :-) + my $code = "\$json$part = \$data; \$config$part = \$data;"; + eval $code; + if ( $@ ) { + warn "ERROR: $code -> $@"; + $status = 500; + } else { + $status = 200; } - }; - $status = 200; + +warn "json ",dump( $json ), " config ", dump( $config ); + + } elsif ( $method eq 'GET' ) { + $status = 200; + } else { + $status = 501; + } + } elsif ( $url =~ m{($regex_dbs)/$} ) { my $database = $1; - my $dir = "$path/$database"; + my $dir = "$config->{path}/$database"; if ( $method eq 'GET' ) { $json = database_get( $database ); @@ -99,7 +130,10 @@ $arg->{$n} = $v; } } - + + my $path = $config->{path}; + warn "ERROR: path $path doesn't exist\n" unless -e $path; + my $p = "$path/$database/$id"; warn "## database: $database id: $id -> $p [$args]\n"; @@ -127,7 +161,7 @@ } } glob( "$path/$database/*" ); - warn "## docs ", dump( @docs ); + warn "## docs ", dump( @docs ) if $debug; $json = { total_rows => $total_rows, @@ -159,8 +193,10 @@ my $descending = delete $arg->{descending}; my @sorted = sort @ids; + warn "creating rows in ", $descending ? "descending" : "", " order\n"; + foreach my $id ( $descending ? reverse @sorted : @sorted ) { - warn ">> $id ", $descending ? 'desc' : 'asc', "\n"; + warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug; push @{ $json->{rows} }, $rows->{$id}; } @@ -209,6 +245,7 @@ sub database_get { my ($db_name) = @_; + my $path = $config->{path}; warn "# collecting docs from $path/$db_name/*\n"; my @docs = glob "$path/$db_name/*"; my $json = {