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, $hash ) = @_;
22 my $data = clone $hash;
23 delete $data->{_id}; # CouchDB doesn't like _ prefixed attributes, and will generate it's own _id
24 $data->{'$entity'} = $entity;
25 if ( my $rev = $couchdb_rev->{$database}->{$entity}->{$id} ) {
29 my $json = Mojo::JSON->new->encode( $data );
30 my $client = Mojo::Client->new;
32 warn "# _couchdb_put $couchdb/$database/$entity.$id = $json";
33 $client->put( "$couchdb/$database/$entity.$id" => $json => sub {
34 my ($client,$tx) = @_;
38 my $response = $tx->res->json;
39 warn "## CouchDB response ",dump($response);
40 $couchdb_rev->{$database}->{$entity}->{$id} = $response->{rev} || die "no rev";
46 my $client = Mojo::Client->new;
47 my $return = $client->get( "$couchdb/$url" )->res->json;
48 warn "# _couchdb_get $url = ",dump($return);
56 my ( $self, $json ) = @_;
57 #warn "## _render_json ",dump($json);
58 my $data = $self->render( json => $json, partial => 1 );
59 warn "## _render_json $data";
60 if ( my $callback = $self->param('callback') ) {
61 $data = "$callback($data)";
63 $self->render( data => $data, format => 'js' );
68 get '/_replicate' => sub {
71 if ( my $from = $self->param('from') ) {
72 my $got = $self->client->get( $from )->res->json;
73 warn "# from $from ",dump($got);
74 _render_jsonp( $self, $got );
76 my $database = $got->{name};
77 my $entities = $got->{entities};
79 if ( $database && $entities ) {
80 foreach my $entity ( keys %$entities ) {
82 $url =~ s{/?$}{/}; # add slash at end
84 my $e = $self->client->get( $url )->res->json;
85 warn "# replicated $url ", dump($e);
86 _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e );
94 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
97 get '/data/:database' => sub {
101 my $database = $self->param('database');
102 my $list_databases = { name => $database };
103 foreach my $entity ( keys %{ $data->{ $database }} ) {
104 warn "# entry $entity ", dump( $data->{$database}->{$entity} );
105 my $count = $#{ $data->{$database}->{$entity} } + 1;
106 $list_databases->{entities}->{$entity} = $count;
107 $list_databases->{document_count} += $count;
109 warn dump($list_databases);
110 _render_jsonp( $self, $list_databases );
114 get '/data/:database/:entity' => sub {
116 _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME
119 get '/data/:database/:entity/:id' => sub {
122 my $database = $self->param('database');
123 my $entity = $self->param('entity');
124 my $id = $self->param('id');
126 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
129 any [ 'post' ] => '/data/:database/:entity' => sub {
131 my $json = $self->req->json;
132 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
133 || $json->{'_id'} # so we use our version
135 warn "## $id body ",dump($self->req->body, $json);
137 $json->{'$id'} ||= $id; # angular.js doesn't resend this one
138 $json->{'_id'} = $id; # but does this one :-)
140 _couchdb_put( $self->param('database'), $self->param('entity'), $id, $json );
142 _render_jsonp( $self, $json );
146 get '/' => sub { shift->redirect_to('/Cookbook') };
148 get '/Cookbook' => 'Cookbook';
149 get '/Cookbook/:example' => sub {
151 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
154 get '/conference/:page' => sub {
156 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
166 @@ layouts/funky.html.ep
167 <!doctype html><html>
168 <head><title>Funky!</title></head>
169 <body><%== content %></body>
172 @@ layouts/angular.html.ep
174 <html xmlns:ng="http://angularjs.org">
176 <meta charset="utf-8">
177 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
178 <script type="text/javascript"
179 src="<%== $ANGULAR_JS %>" ng:autobind></script>
181 <body><%== content %></body>