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';
17 my $client = Mojo::Client->new;
20 my ( $url, $data ) = @_;
22 $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
24 my $json = Mojo::JSON->new->encode( $data );
26 warn "# _couchdb_put $url = $json";
27 $client->put( "$couchdb/$url" => $json => sub {
28 my ($client,$tx) = @_;
30 die "ERROR CouchDB ",$tx->error;
32 my $response = $tx->res->json;
33 warn "## CouchDB response ",dump($response);
39 my $return = $client->get( "$couchdb/$url" )->res->json;
40 warn "# _couchdb_get $url = ",dump($return);
49 my ( $self, $json ) = @_;
50 #warn "## _render_json ",dump($json);
51 my $data = $self->render( json => $json, partial => 1 );
52 warn "## _render_json $data";
53 if ( my $callback = $self->param('callback') ) {
54 $data = "$callback($data)";
56 $self->render( data => $data, format => 'js' );
61 get '/_replicate' => sub {
64 if ( my $from = $self->param('from') ) {
65 my $got = $self->client->get( $from )->res->json;
66 warn "# from $from ",dump($got);
67 _render_jsonp( $self, $got );
69 my $database = $got->{name};
70 my $entities = $got->{entities};
72 if ( $database && $entities ) {
73 foreach my $entity ( keys %$entities ) {
75 $url =~ s{/?$}{/}; # add slash at end
77 my $e = $self->client->get( $url )->res->json;
78 warn "# replicated $url ", dump($e);
79 _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e );
87 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
90 get '/data/:database' => sub {
92 my $database = $self->param('database');
94 my $list_databases = { name => $database };
96 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
97 if ( exists $counts->{error} ) {
98 warn "creating CouchDB view because of ", dump($counts);
99 _couchdb_put "/$database/_design/entity", {
100 _id => '_design/entity',
101 language => 'javascript',
104 map => q| function(doc) { emit(doc.$entity,1); } |,
105 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
109 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
113 warn "# counts ",dump($counts);
115 foreach my $row ( @{ $counts->{rows} } ) {
116 my $n = $row->{value};
117 $list_databases->{entities}->{ $row->{key} } = $n;
118 $list_databases->{document_counts} += $n;
120 warn dump($list_databases);
121 _render_jsonp( $self, $list_databases );
124 get '/data/:database/:entity' => sub {
127 my $database = $self->param('database');
128 my $entity = $self->param('entity');
130 my $endkey = $entity;
133 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
134 warn "# counts ",dump($counts);
136 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
139 get '/data/:database/:entity/:id' => sub {
142 my $database = $self->param('database');
143 my $entity = $self->param('entity');
144 my $id = $self->param('id');
146 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
149 any [ 'post' ] => '/data/:database/:entity' => sub {
151 my $database = $self->param('database');
152 my $entity = $self->param('entity');
153 my $json = $self->req->json;
154 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
156 warn "## $database $entity $id body ",dump($self->req->body, $json);
158 $json->{'$id'} ||= $id; # make sure $id is in there
160 _couchdb_put "/$database/$entity.$id" => $json;
162 _render_jsonp( $self, $json );
166 get '/' => sub { shift->redirect_to('/Cookbook') };
168 get '/Cookbook' => 'Cookbook';
169 get '/Cookbook/:example' => sub {
171 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
174 get '/conference/:page' => sub {
176 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
186 @@ layouts/funky.html.ep
187 <!doctype html><html>
188 <head><title>Funky!</title></head>
189 <body><%== content %></body>
192 @@ layouts/angular.html.ep
194 <html xmlns:ng="http://angularjs.org">
196 <meta charset="utf-8">
197 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
198 <script type="text/javascript"
199 src="<%== $ANGULAR_JS %>" ng:autobind></script>
201 <body><%== content %></body>