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);
68 my $database = $got->{name};
69 my $entities = $got->{entities};
71 if ( $database && $entities ) {
72 foreach my $entity ( keys %$entities ) {
74 $url =~ s{/?$}{/}; # add slash at end
76 my $all = $self->client->get( $url )->res->json;
77 warn "# replicated $url ", dump($all);
78 foreach my $e ( @$all ) {
79 delete $e->{_id}; # sanitize data from older implementation
80 _couchdb_put( "/$database/$entity." . $e->{'$id'} => $e );
84 _render_jsonp( $self, $got );
90 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
93 get '/data/:database' => sub {
95 my $database = $self->param('database');
97 my $list_databases = { name => $database };
99 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
100 if ( exists $counts->{error} ) {
101 warn "creating CouchDB view because of ", dump($counts);
102 _couchdb_put "/$database/_design/entity", {
103 _id => '_design/entity',
104 language => 'javascript',
107 map => q| function(doc) { emit(doc.$entity,1); } |,
108 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
112 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
116 warn "# counts ",dump($counts);
118 foreach my $row ( @{ $counts->{rows} } ) {
119 my $n = $row->{value};
120 $list_databases->{entities}->{ $row->{key} } = $n;
121 $list_databases->{document_counts} += $n;
123 warn dump($list_databases);
124 _render_jsonp( $self, $list_databases );
127 get '/data/:database/:entity' => sub {
130 my $database = $self->param('database');
131 my $entity = $self->param('entity');
133 my $endkey = $entity;
136 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
137 warn "# counts ",dump($counts);
139 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
142 get '/data/:database/:entity/:id' => sub {
145 my $database = $self->param('database');
146 my $entity = $self->param('entity');
147 my $id = $self->param('id');
149 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
152 any [ 'post' ] => '/data/:database/:entity' => sub {
154 my $database = $self->param('database');
155 my $entity = $self->param('entity');
156 my $json = $self->req->json;
157 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
159 warn "## $database $entity $id body ",dump($self->req->body, $json);
161 $json->{'$id'} ||= $id; # make sure $id is in there
163 _couchdb_put "/$database/$entity.$id" => $json;
165 _render_jsonp( $self, $json );
169 get '/' => sub { shift->redirect_to('/Cookbook') };
171 get '/Cookbook' => 'Cookbook';
172 get '/Cookbook/:example' => sub {
174 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
177 get '/conference/:page' => sub {
179 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
189 @@ layouts/funky.html.ep
190 <!doctype html><html>
191 <head><title>Funky!</title></head>
192 <body><%== content %></body>
195 @@ layouts/angular.html.ep
197 <html xmlns:ng="http://angularjs.org">
199 <meta charset="utf-8">
200 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
201 <script type="text/javascript"
202 src="<%== $ANGULAR_JS %>" ng:autobind></script>
204 <body><%== content %></body>