X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=angular-server.pl;h=0d1622e18c127341c5b8ec1bb95c5d0f517ddb1d;hb=05c21646949b9850e70268f90e9cd98e2236b654;hp=bcf3e4e7825402cc9de796b64f2d3ad3d22a1fa5;hpb=5e71f2790c64bcbdc6133d9636162e67f4dbffa3;p=angular-mojolicious.git diff --git a/angular-server.pl b/angular-server.pl index bcf3e4e..0d1622e 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -14,41 +14,42 @@ sub new_uuid { Time::HiRes::time * 100000 } # http://angular.getangular.com/data my $couchdb = 'http://localhost:5984'; -our $couchdb_rev; +my $client = Mojo::Client->new; sub _couchdb_put { - my ( $database, $entity, $id, $hash ) = @_; + my ( $url, $data ) = @_; - my $data = clone $hash; - delete $data->{_id}; # CouchDB doesn't like _ prefixed attributes, and will generate it's own _id - $data->{'$entity'} = $entity; - if ( my $rev = $couchdb_rev->{$database}->{$entity}->{$id} ) { - $data->{_rev} = $rev; - } + $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/}; my $json = Mojo::JSON->new->encode( $data ); - my $client = Mojo::Client->new; - warn "# _couchdb_put $couchdb/$database/$entity.$id = $json"; - $client->put( "$couchdb/$database/$entity.$id" => $json => sub { + my $rev; + + warn "# _couchdb_put $url = $json"; + $client->put( "$couchdb/$url" => $json => sub { my ($client,$tx) = @_; + my ($message, $code) = $tx->error; + my $response = $tx->res->json; + warn "## response $code ",dump($response); if ($tx->error) { - die $tx->error; + warn "ERROR $code $message"; } - my $response = $tx->res->json; - warn "## CouchDB response ",dump($response); - $couchdb_rev->{$database}->{$entity}->{$id} = $response->{rev} || die "no rev"; + return + $rev = $response->{rev}; })->process; + + warn "## rev = $rev"; + return $rev; } sub _couchdb_get { my ( $url ) = @_; - my $client = Mojo::Client->new; my $return = $client->get( "$couchdb/$url" )->res->json; warn "# _couchdb_get $url = ",dump($return); return $return; } + our $id2nr; @@ -65,29 +66,6 @@ warn "## _render_json $data"; #get '/' => 'index'; -get '/_replicate' => sub { - my $self = shift; - - if ( my $from = $self->param('from') ) { - my $got = $self->client->get( $from )->res->json; - warn "# from $from ",dump($got); - _render_jsonp( $self, $got ); - - my $database = $got->{name}; - my $entities = $got->{entities}; - - if ( $database && $entities ) { - foreach my $entity ( keys %$entities ) { - my $url = $from; - $url =~ s{/?$}{/}; # add slash at end - $url .= $entity; - my $e = $self->client->get( $url )->res->json; - warn "# replicated $url ", dump($e); - _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e ); - } - } - } -}; get '/data/' => sub { my $self = shift; @@ -95,25 +73,52 @@ get '/data/' => sub { }; get '/data/:database' => sub { - die "FIXME"; -=for FIXME my $self = shift; my $database = $self->param('database'); + my $list_databases = { name => $database }; - foreach my $entity ( keys %{ $data->{ $database }} ) { -warn "# entry $entity ", dump( $data->{$database}->{$entity} ); - my $count = $#{ $data->{$database}->{$entity} } + 1; - $list_databases->{entities}->{$entity} = $count; - $list_databases->{document_count} += $count; + + my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true"); + if ( exists $counts->{error} ) { + warn "creating CouchDB view because of ", dump($counts); + _couchdb_put "/$database/_design/entity", { + _id => '_design/entity', + language => 'javascript', + views => { + counts => { + map => q| function(doc) { emit(doc._id.split('.')[0],1); } |, + reduce => q| function(keys,values,rereduce) { return sum(values); } |, + } + } + }; + $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true") + || die "give up!"; + } + + warn "# counts ",dump($counts); + + foreach my $row ( @{ $counts->{rows} } ) { + my $n = $row->{value}; + $list_databases->{entities}->{ $row->{key} } = $n; + $list_databases->{document_counts} += $n; } warn dump($list_databases); _render_jsonp( $self, $list_databases ); -=cut }; get '/data/:database/:entity' => sub { my $self = shift; - _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME + + my $database = $self->param('database'); + my $entity = $self->param('entity'); + + my $endkey = $entity; + $endkey++; + + my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|; + warn "# counts ",dump($counts); + + _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] ) }; get '/data/:database/:entity/:id' => sub { @@ -128,16 +133,17 @@ get '/data/:database/:entity/:id' => sub { any [ 'post' ] => '/data/:database/:entity' => sub { my $self = shift; + my $database = $self->param('database'); + my $entity = $self->param('entity'); my $json = $self->req->json; my $id = $json->{'$id'} # XXX we don't get it back from angular.js - || $json->{'_id'} # so we use our version || new_uuid; - warn "## $id body ",dump($self->req->body, $json); + warn "## $database $entity $id body ",dump($self->req->body, $json); - $json->{'$id'} ||= $id; # angular.js doesn't resend this one - $json->{'_id'} = $id; # but does this one :-) + $json->{'$id'} ||= $id; # make sure $id is in there - _couchdb_put( $self->param('database'), $self->param('entity'), $id, $json ); + my $rev = _couchdb_put "/$database/$entity.$id" => $json; + $json->{_rev} = $rev; _render_jsonp( $self, $json ); }; @@ -156,6 +162,24 @@ get '/conference/:page' => sub { $self->render( "conference/" . $self->param('page'), layout => 'angular' ); }; +# /app/ + +get '/app/:database/angular.js' => sub { + my $self = shift; + my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' ); + warn "# $ANGULAR_JS"; + $self->render_static( $ANGULAR_JS ); +}; + +# CouchDB proxy for _design _view + +get '/:database/_design/:design/_view/:view' => sub { + my $self = shift; + my $url = $self->param('url'); + warn "# /couchdb $url"; + _render_jsonp( $self, _couchdb_get( $self->param('database') . '/_design/' . $self->param('design') . '/_view/' . $self->param('view') ) ); +}; + app->start; __DATA__