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 );
28 warn "# _couchdb_put $url = $json";
29 $client->put( "$couchdb/$url" => $json => sub {
30 my ($client,$tx) = @_;
31 my ($message, $code) = $tx->error;
32 my $response = $tx->res->json;
33 warn "## response $code ",dump($response);
35 die "ERROR $code $message";
38 $rev = $response->{rev};
47 my $return = $client->get( "$couchdb/$url" )->res->json;
48 warn "# _couchdb_get $url = ",dump($return);
57 my ( $self, $json ) = @_;
58 #warn "## _render_json ",dump($json);
59 my $data = $self->render( json => $json, partial => 1 );
60 warn "## _render_json $data";
61 if ( my $callback = $self->param('callback') ) {
62 $data = "$callback($data)";
64 $self->render( data => $data, format => 'js' );
69 get '/_replicate' => sub {
72 if ( my $from = $self->param('from') ) {
73 my $got = $self->client->get( $from )->res->json;
74 warn "# from $from ",dump($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 $all = $self->client->get( $url )->res->json;
85 warn "# replicated $url ", dump($all);
86 foreach my $e ( @$all ) {
87 delete $e->{_id}; # sanitize data from older implementation
88 _couchdb_put( "/$database/$entity." . $e->{'$id'} => $e );
92 _render_jsonp( $self, $got );
98 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
101 get '/data/:database' => sub {
103 my $database = $self->param('database');
105 my $list_databases = { name => $database };
107 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
108 if ( exists $counts->{error} ) {
109 warn "creating CouchDB view because of ", dump($counts);
110 _couchdb_put "/$database/_design/entity", {
111 _id => '_design/entity',
112 language => 'javascript',
115 map => q| function(doc) { emit(doc.$entity,1); } |,
116 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
120 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
124 warn "# counts ",dump($counts);
126 foreach my $row ( @{ $counts->{rows} } ) {
127 my $n = $row->{value};
128 $list_databases->{entities}->{ $row->{key} } = $n;
129 $list_databases->{document_counts} += $n;
131 warn dump($list_databases);
132 _render_jsonp( $self, $list_databases );
135 get '/data/:database/:entity' => sub {
138 my $database = $self->param('database');
139 my $entity = $self->param('entity');
141 my $endkey = $entity;
144 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
145 warn "# counts ",dump($counts);
147 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
150 get '/data/:database/:entity/:id' => sub {
153 my $database = $self->param('database');
154 my $entity = $self->param('entity');
155 my $id = $self->param('id');
157 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
160 any [ 'post' ] => '/data/:database/:entity' => sub {
162 my $database = $self->param('database');
163 my $entity = $self->param('entity');
164 my $json = $self->req->json;
165 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
167 warn "## $database $entity $id body ",dump($self->req->body, $json);
169 $json->{'$id'} ||= $id; # make sure $id is in there
171 my $rev = _couchdb_put "/$database/$entity.$id" => $json;
172 $json->{_rev} = $rev;
174 _render_jsonp( $self, $json );
178 get '/' => sub { shift->redirect_to('/Cookbook') };
180 get '/Cookbook' => 'Cookbook';
181 get '/Cookbook/:example' => sub {
183 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
186 get '/conference/:page' => sub {
188 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
198 @@ layouts/funky.html.ep
199 <!doctype html><html>
200 <head><title>Funky!</title></head>
201 <body><%== content %></body>
204 @@ layouts/angular.html.ep
206 <html xmlns:ng="http://angularjs.org">
208 <meta charset="utf-8">
209 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
210 <script type="text/javascript"
211 src="<%== $ANGULAR_JS %>" ng:autobind></script>
213 <body><%== content %></body>