X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=angular-server.pl;h=294af13a0ea100220b0d17c09137f66c194d93c7;hb=ec54ccc6ef3015369a7d36d7c6e2c4a0961706a0;hp=5ac99de81bebaea55a19e5659a3de2ec59c7012a;hpb=4baffa6f3524fc7d3911b350adc3cf80353fc52f;p=angular-mojolicious.git diff --git a/angular-server.pl b/angular-server.pl index 5ac99de..294af13 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -1,16 +1,62 @@ #!/usr/bin/env perl +use lib 'common/mojo/lib'; + use Mojolicious::Lite; use Data::Dump qw(dump); +use Time::HiRes; +use Clone qw(clone); + +sub new_uuid { Time::HiRes::time * 100000 } # based on # http://docs.getangular.com/REST.Basic # http://angular.getangular.com/data -our $data; +my $couchdb = 'http://localhost:5984'; +my $client = Mojo::Client->new; + +sub _couchdb_put { + my ( $url, $data ) = @_; + + $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/}; + + my $json = Mojo::JSON->new->encode( $data ); + + warn "# _couchdb_put $url = $json"; + $client->put( "$couchdb/$url" => $json => sub { + my ($client,$tx) = @_; + if ($tx->error) { + die "ERROR CouchDB ",$tx->error; + } + my $response = $tx->res->json; + warn "## CouchDB response ",dump($response); + })->process; +} + +sub _couchdb_get { + my ( $url ) = @_; + my $return = $client->get( "$couchdb/$url" )->res->json; + warn "# _couchdb_get $url = ",dump($return); + return $return; +} + + our $id2nr; -get '/' => 'index'; + +sub _render_jsonp { + my ( $self, $json ) = @_; +#warn "## _render_json ",dump($json); + my $data = $self->render( json => $json, partial => 1 ); +warn "## _render_json $data"; + if ( my $callback = $self->param('callback') ) { + $data = "$callback($data)"; + } + $self->render( data => $data, format => 'js' ); +} + +#get '/' => 'index'; get '/_replicate' => sub { my $self = shift; @@ -18,7 +64,7 @@ get '/_replicate' => sub { if ( my $from = $self->param('from') ) { my $got = $self->client->get( $from )->res->json; warn "# from $from ",dump($got); - $self->render_json( $got ); + _render_jsonp( $self, $got ); my $database = $got->{name}; my $entities = $got->{entities}; @@ -30,39 +76,54 @@ get '/_replicate' => sub { $url .= $entity; my $e = $self->client->get( $url )->res->json; warn "# replicated $url ", dump($e); - $data->{$database}->{$entity} = $e; - delete $id2nr->{$database}->{$entity}; + _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e ); } } } }; -get '/_data' => sub { - shift->render_json( $data ) -}; - get '/data/' => sub { my $self = shift; - $self->render_json( [ keys %$data ] ); + _render_jsonp( $self, _couchdb_get('/_all_dbs') ); }; get '/data/:database' => sub { 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.$entity,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); - $self->render_json( $list_databases ); + _render_jsonp( $self, $list_databases ); }; get '/data/:database/:entity' => sub { my $self = shift; - $self->render_json( $data->{ $self->param('database') }->{ $self->param('entity' ) } ); + _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME }; get '/data/:database/:entity/:id' => sub { @@ -72,40 +133,38 @@ get '/data/:database/:entity/:id' => sub { my $entity = $self->param('entity'); my $id = $self->param('id'); - my $e = $data->{$database}->{$entity} || die "no entity $entity"; - - if ( ! defined $id2nr->{$database}->{$entity} ) { - foreach my $i ( 0 .. $#$e ) { - $id2nr->{$database}->{$entity}->{ $e->[$i]->{'$id'} } = $i; - } - } - - if ( exists $id2nr->{$database}->{$entity}->{$id} ) { - my $nr = $id2nr->{$database}->{$entity}->{$id}; - warn "# entity $id -> $nr\n"; - $self->render_json( $data->{$database}->{$entity}->[$nr] ); - } else { - die "no entity $entity $id in ", dump( $id2nr->{$database}->{$entity} ); - } + _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) ); }; -any [ 'put' ] => '/data/:database/:entity/:id' => sub { +any [ 'post' ] => '/data/:database/:entity' => sub { my $self = shift; - $data->{ $self->param('database') }->{ $self->param('entity') }->{ $self->param('id') } = $self->req->json; - dumper $data; -}; + 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 + || new_uuid; + warn "## $database $entity $id body ",dump($self->req->body, $json); -get '/demo/:groovy' => sub { - my $self = shift; - $self->render(text => $self->param('groovy'), layout => 'funky'); + $json->{'$id'} ||= $id; # make sure $id is in there + + _couchdb_put "/$database/$entity.$id" => $json; + + _render_jsonp( $self, $json ); }; + +get '/' => sub { shift->redirect_to('/Cookbook') }; + get '/Cookbook' => 'Cookbook'; get '/Cookbook/:example' => sub { my $self = shift; $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' ); }; +get '/conference/:page' => sub { + my $self = shift; + $self->render( "conference/" . $self->param('page'), layout => 'angular' ); +}; app->start; __DATA__ @@ -124,8 +183,10 @@ Yea baby! + +% my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' ); + src="<%== $ANGULAR_JS %>" ng:autobind> <%== content %>