X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;ds=sidebyside;f=angular-server.pl;h=50ed42618a1406074f8649ce5565763132c11de1;hb=fbcb633fbd69753d569c7787d88889dd8f60e5ea;hp=30a99b9d56381f87f3a6d6a12154925ef4151891;hpb=7e72f290040ac6ee65d0e3db2842430c81f1c050;p=angular-mojolicious.git diff --git a/angular-server.pl b/angular-server.pl index 30a99b9..50ed426 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -1,34 +1,67 @@ #!/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 = { - 'Cookbook' => { - test => [ - { '$id' => 1, foo => 1, bar => 2, baz => 3 }, - { '$id' => 2, foo => 1 }, - { '$id' => 3, bar => 2 }, - { '$id' => 4, baz => 3 }, - ], +my $couchdb = 'http://localhost:5984'; +our $couchdb_rev; + +sub _couchdb_put { + my ( $database, $entity, $id, $data ) = @_; + + $data->{'$entity'} = $entity; + if ( my $rev = $couchdb_rev->{$database}->{$entity}->{$id} ) { + $data->{_rev} = $rev; } -}; + + 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 ($client,$tx) = @_; + if ($tx->error) { + die $tx->error; + } + my $response = $tx->res->json; + warn "## CouchDB response ",dump($response); + $couchdb_rev->{$database}->{$entity}->{$id} = $response->{rev} || die "no rev"; + })->process; +} + +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; + 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 '/' => 'index'; get '/_replicate' => sub { my $self = shift; @@ -48,24 +81,20 @@ 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 { - 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 { + die "FIXME"; +=for FIXME my $self = shift; my $database = $self->param('database'); my $list_databases = { name => $database }; @@ -77,11 +106,12 @@ warn "# entry $entity ", dump( $data->{$database}->{$entity} ); } warn dump($list_databases); _render_jsonp( $self, $list_databases ); +=cut }; get '/data/:database/:entity' => sub { my $self = shift; - _render_jsonp( $self, $data->{ $self->param('database') }->{ $self->param('entity' ) } ); + _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME }; get '/data/:database/:entity/:id' => sub { @@ -91,43 +121,36 @@ 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 [ 'put', 'post' ] => '/data/:database/:entity/:id' => sub { +any [ 'post' ] => '/data/:database/:entity' => sub { my $self = shift; - my $data = $self->req->json; - warn "# body ",dump($self->req->body, $data); - die "no data" unless $data; - $data->{ $self->param('database') }->{ $self->param('entity') }->{ $self->param('id') } = $data; - _render_jsonp( $self, $data ); -}; + my $json = $self->req->json; + my $id = $json->{'$id'} # XXX we don't get it back from angular.js + || new_uuid; + warn "## $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( $self->param('database'), $self->param('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__ @@ -146,8 +169,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 %>