X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=blobdiff_plain;f=angular-server.pl;h=57e40a475957de8dc64029e6d2de748b92f8d4f2;hp=58e07fd0eebf4d8b583f2c49e62916d2ceac4e42;hb=552f20ee2f3c5d614f9794bb023037f8a3ca8222;hpb=fc17e83fa0cca76ce097fc6fc7c414db08132664 diff --git a/angular-server.pl b/angular-server.pl index 58e07fd..57e40a4 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -1,5 +1,7 @@ #!/usr/bin/env perl +use lib 'common/mojo/lib'; + use Mojolicious::Lite; use Data::Dump qw(dump); use Time::HiRes; @@ -11,26 +13,46 @@ sub new_uuid { Time::HiRes::time * 100000 } # http://docs.getangular.com/REST.Basic # http://angular.getangular.com/data -our $data = { - 'Cookbook' => { - test => [ - { '$id' => 1, foo => 1, bar => 2, baz => 3 }, - { '$id' => 2, foo => 1 }, - { '$id' => 3, bar => 2 }, - { '$id' => 4, baz => 3 }, - ], - }, - 'AddressBook' => { - people => [ - {name=>'Misko'}, - {name=>'Igor'}, - {name=>'Adam'}, - {name=>'Elliott'} - ] - } -}; +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 ); + + 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) { + warn "ERROR $code $message"; + } + return + $rev = $response->{rev}; + })->process; + + warn "## rev = $rev"; + return $rev; +} + +sub _couchdb_get { + my ( $url ) = @_; + my $return = $client->get( "$couchdb/$url" )->res->json; + warn "# _couchdb_get $url = ",dump($return); + return $return; +} + + our $id2nr; + sub _render_jsonp { my ( $self, $json ) = @_; #warn "## _render_json ",dump($json); @@ -44,50 +66,41 @@ 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); - $data->{$database}->{$entity} = $e; - delete $id2nr->{$database}->{$entity}; - } - } - } -}; - -get '/_data' => sub { - my $self = shift; - _render_jsonp( $self, $data ) -}; get '/data/' => sub { my $self = shift; - _render_jsonp( $self, [ 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._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 ); @@ -95,7 +108,17 @@ warn "# entry $entity ", dump( $data->{$database}->{$entity} ); get '/data/:database/:entity' => sub { my $self = shift; - _render_jsonp( $self, $data->{ $self->param('database') }->{ $self->param('entity' ) } ); + + 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 { @@ -105,57 +128,29 @@ 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"; - _render_jsonp( $self, $data->{$database}->{$entity}->[$nr] ); - } else { - die "no entity $entity $id in ", dump( $id2nr->{$database}->{$entity} ); - } + _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) ); }; 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); - die "no data" unless $data; + 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 - my $database = $self->param('database'); - my $entity = $self->param('entity'); + my $rev = _couchdb_put "/$database/$entity.$id" => $json; + $json->{_rev} = $rev; - my $nr = $id2nr->{$database}->{$entity}->{$id}; - if ( defined $nr ) { - $data->{$database}->{$entity}->[$nr] = $json; - warn "# update $nr $id ",dump($json); - } else { - push @{ $data->{$database}->{$entity} }, $json; - my $nr = $#{ $data->{$database}->{$entity} }; - $id2nr->{$database}->{$entity}->{$id} = $nr; - warn "# added $nr $id ",dump($json); - } _render_jsonp( $self, $json ); }; -get '/demo/:groovy' => sub { - my $self = shift; - $self->render(text => $self->param('groovy'), layout => 'funky'); -}; get '/' => sub { shift->redirect_to('/Cookbook') }; + get '/Cookbook' => 'Cookbook'; get '/Cookbook/:example' => sub { my $self = shift; @@ -167,6 +162,74 @@ 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 = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') ); + my $param = $self->req->url->query->clone->remove('callback')->to_string; + $url .= '?' . $param if $param; + warn "CouchDB proxy $url"; + _render_jsonp( $self, _couchdb_get($url)); +}; + +# static JSON files from public/json/database/entity/json + +get '/json' => sub { + _render_jsonp( shift, [ map { s{public/json/}{}; $_ } glob 'public/json/*' ] ); +}; + +get '/json/:database' => sub { + my $self = shift; + my $database = $self->param('database'); + + my $status = { + document_counts => 0, + name => $database, + }; + + foreach my $path ( glob "public/json/$database/*" ) { + my @entities = glob "$path/*"; + $path =~ s{public/json/$database/}{}; + $status->{entities}->{$path} = scalar @entities; + $status->{document_counts}++; + } + + _render_jsonp( $self, $status ); +}; + +get '/json/:database/:entity' => sub { + my $self = shift; + + my $database = $self->param('database'); + my $entity = $self->param('entity'); + + my $path = "public/json/$database/$entity"; + die "$path: $!" unless -d $path; + + my $docs; + foreach my $path ( sort glob "$path/*" ) { + open(my $fh, '<', $path) || die $!; + local $/ = undef; + my $str = <$fh>; + warn "# $path $str"; + my $data = Mojo::JSON->new->decode( $str ); + $data->{_key} = $1 if $path =~ m{/([^/]+$)}; + push @$docs, $data; + } + + _render_jsonp( $self, $docs ) +}; + app->start; __DATA__ @@ -184,6 +247,7 @@ Yea baby! + % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );