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 warn "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' );
72 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
75 get '/data/:database' => sub {
77 my $database = $self->param('database');
79 my $list_databases = { name => $database };
81 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
82 if ( exists $counts->{error} ) {
83 warn "creating CouchDB view because of ", dump($counts);
84 _couchdb_put "/$database/_design/entity", {
85 _id => '_design/entity',
86 language => 'javascript',
89 map => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
90 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
94 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
98 warn "# counts ",dump($counts);
100 foreach my $row ( @{ $counts->{rows} } ) {
101 my $n = $row->{value};
102 $list_databases->{entities}->{ $row->{key} } = $n;
103 $list_databases->{document_counts} += $n;
105 warn dump($list_databases);
106 _render_jsonp( $self, $list_databases );
109 get '/data/:database/:entity' => sub {
112 my $database = $self->param('database');
113 my $entity = $self->param('entity');
115 my $endkey = $entity;
118 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
119 warn "# counts ",dump($counts);
121 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
124 get '/data/:database/:entity/:id' => sub {
127 my $database = $self->param('database');
128 my $entity = $self->param('entity');
129 my $id = $self->param('id');
131 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
134 any [ 'post' ] => '/data/:database/:entity' => sub {
136 my $database = $self->param('database');
137 my $entity = $self->param('entity');
138 my $json = $self->req->json;
139 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
141 warn "## $database $entity $id body ",dump($self->req->body, $json);
143 $json->{'$id'} ||= $id; # make sure $id is in there
145 my $rev = _couchdb_put "/$database/$entity.$id" => $json;
146 $json->{_rev} = $rev;
148 _render_jsonp( $self, $json );
152 get '/' => sub { shift->redirect_to('/Cookbook') };
154 get '/Cookbook' => 'Cookbook';
155 get '/Cookbook/:example' => sub {
157 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
160 get '/conference/:page' => sub {
162 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
167 get '/app/:database/angular.js' => sub {
169 my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
170 warn "# $ANGULAR_JS";
171 $self->render_static( $ANGULAR_JS );
174 # CouchDB proxy for _design _view
176 get '/:database/_design/:design/_view/:view' => sub {
178 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
179 my $param = $self->req->url->query->clone->remove('callback')->to_string;
180 $url .= '?' . $param if $param;
181 warn "CouchDB proxy $url";
182 _render_jsonp( $self, _couchdb_get($url));
185 # static JSON files from public/json/database/entity/json
188 _render_jsonp( shift, [ map { s{public/json/}{}; $_ } glob 'public/json/*' ] );
191 get '/json/:database' => sub {
193 my $database = $self->param('database');
196 document_counts => 0,
200 foreach my $path ( glob "public/json/$database/*" ) {
201 my @entities = glob "$path/*";
202 $path =~ s{public/json/$database/}{};
203 $status->{entities}->{$path} = scalar @entities;
204 $status->{document_counts}++;
207 _render_jsonp( $self, $status );
210 get '/json/:database/:entity' => sub {
213 my $database = $self->param('database');
214 my $entity = $self->param('entity');
216 my $path = "public/json/$database/$entity";
217 die "$path: $!" unless -d $path;
220 foreach my $path ( sort glob "$path/*" ) {
221 open(my $fh, '<', $path) || die $!;
225 my $data = Mojo::JSON->new->decode( $str );
226 $data->{_key} = $1 if $path =~ m{/([^/]+$)};
230 _render_jsonp( $self, $docs )
240 @@ layouts/funky.html.ep
241 <!doctype html><html>
242 <head><title>Funky!</title></head>
243 <body><%== content %></body>
246 @@ layouts/angular.html.ep
248 <html xmlns:ng="http://angularjs.org">
250 <meta charset="utf-8">
251 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
252 <script type="text/javascript"
253 src="<%== $ANGULAR_JS %>" ng:autobind></script>
255 <body><%== content %></body>