3 use lib 'common/mojo/lib';
6 use Data::Dump qw(dump);
10 sub new_uuid { Time::HiRes::time * 100000 }
13 # http://docs.getangular.com/REST.Basic
14 # http://angular.getangular.com/data
16 my $couchdb = 'http://localhost:5984';
20 my ( $database, $entity, $id, $data ) = @_;
22 $data->{'$entity'} = $entity;
23 if ( my $rev = $couchdb_rev->{$database}->{$entity}->{$id} ) {
27 my $json = Mojo::JSON->new->encode( $data );
28 my $client = Mojo::Client->new;
30 warn "# _couchdb_put $couchdb/$database/$entity.$id = $json";
31 $client->put( "$couchdb/$database/$entity.$id" => $json => sub {
32 my ($client,$tx) = @_;
36 my $response = $tx->res->json;
37 warn "## CouchDB response ",dump($response);
38 $couchdb_rev->{$database}->{$entity}->{$id} = $response->{rev} || die "no rev";
44 my $client = Mojo::Client->new;
45 my $return = $client->get( "$couchdb/$url" )->res->json;
46 warn "# _couchdb_get $url = ",dump($return);
54 my ( $self, $json ) = @_;
55 #warn "## _render_json ",dump($json);
56 my $data = $self->render( json => $json, partial => 1 );
57 warn "## _render_json $data";
58 if ( my $callback = $self->param('callback') ) {
59 $data = "$callback($data)";
61 $self->render( data => $data, format => 'js' );
66 get '/_replicate' => sub {
69 if ( my $from = $self->param('from') ) {
70 my $got = $self->client->get( $from )->res->json;
71 warn "# from $from ",dump($got);
72 _render_jsonp( $self, $got );
74 my $database = $got->{name};
75 my $entities = $got->{entities};
77 if ( $database && $entities ) {
78 foreach my $entity ( keys %$entities ) {
80 $url =~ s{/?$}{/}; # add slash at end
82 my $e = $self->client->get( $url )->res->json;
83 warn "# replicated $url ", dump($e);
84 _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e );
92 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
95 get '/data/:database' => sub {
99 my $database = $self->param('database');
100 my $list_databases = { name => $database };
101 foreach my $entity ( keys %{ $data->{ $database }} ) {
102 warn "# entry $entity ", dump( $data->{$database}->{$entity} );
103 my $count = $#{ $data->{$database}->{$entity} } + 1;
104 $list_databases->{entities}->{$entity} = $count;
105 $list_databases->{document_count} += $count;
107 warn dump($list_databases);
108 _render_jsonp( $self, $list_databases );
112 get '/data/:database/:entity' => sub {
114 _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME
117 get '/data/:database/:entity/:id' => sub {
120 my $database = $self->param('database');
121 my $entity = $self->param('entity');
122 my $id = $self->param('id');
124 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
127 any [ 'post' ] => '/data/:database/:entity' => sub {
129 my $json = $self->req->json;
130 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
132 warn "## $id body ",dump($self->req->body, $json);
134 $json->{'$id'} ||= $id; # make sure $id is in there
136 _couchdb_put( $self->param('database'), $self->param('entity'), $id, $json );
138 _render_jsonp( $self, $json );
142 get '/' => sub { shift->redirect_to('/Cookbook') };
144 get '/Cookbook' => 'Cookbook';
145 get '/Cookbook/:example' => sub {
147 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
150 get '/conference/:page' => sub {
152 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
162 @@ layouts/funky.html.ep
163 <!doctype html><html>
164 <head><title>Funky!</title></head>
165 <body><%== content %></body>
168 @@ layouts/angular.html.ep
170 <html xmlns:ng="http://angularjs.org">
172 <meta charset="utf-8">
173 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
174 <script type="text/javascript"
175 src="<%== $ANGULAR_JS %>" ng:autobind></script>
177 <body><%== content %></body>