3 use lib 'common/mojo/lib';
6 use Data::Dump qw(dump);
11 sub new_uuid { Time::HiRes::time * 100000 }
14 # http://docs.getangular.com/REST.Basic
15 # http://angular.getangular.com/data
17 my $couchdb = 'http://localhost:5984';
18 my $client = Mojo::UserAgent->new;
21 my ( $url, $data ) = @_;
23 $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
25 my $json = Mojo::JSON->new->encode( $data );
29 warn "# _couchdb_put $url = $json";
30 $client->put( "$couchdb/$url" => $json => sub {
31 my ($client,$tx) = @_;
32 my ($message, $code) = $tx->error;
33 my $response = $tx->res->json;
34 warn "## response $code ",dump($response);
36 warn "ERROR $code $message";
39 $rev = $response->{rev};
48 my $return = $client->get( "$couchdb/$url" )->res->json;
49 warn "# _couchdb_get $url = ",dump($return);
58 my ( $self, $json ) = @_;
59 #warn "## _render_json ",dump($json);
60 my $data = $self->render( json => $json, partial => 1 );
61 warn "## _render_json $data";
62 if ( my $callback = $self->param('callback') ) {
63 $data = "$callback($data)";
65 $self->render( data => $data, format => 'js' );
73 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
76 get '/data/:database' => sub {
78 my $database = $self->param('database');
80 my $list_databases = { name => $database };
82 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
83 if ( exists $counts->{error} ) {
84 warn "creating CouchDB view because of ", dump($counts);
85 _couchdb_put "/$database/_design/entity", {
86 _id => '_design/entity',
87 language => 'javascript',
90 map => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
91 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
95 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
99 warn "# counts ",dump($counts);
101 foreach my $row ( @{ $counts->{rows} } ) {
102 my $n = $row->{value};
103 $list_databases->{entities}->{ $row->{key} } = $n;
104 $list_databases->{document_counts} += $n;
106 warn dump($list_databases);
107 _render_jsonp( $self, $list_databases );
110 get '/data/:database/:entity' => sub {
113 my $database = $self->param('database');
114 my $entity = $self->param('entity');
116 my $endkey = $entity;
119 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
120 warn "# counts ",dump($counts);
122 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
125 get '/data/:database/:entity/:id' => sub {
128 my $database = $self->param('database');
129 my $entity = $self->param('entity');
130 my $id = $self->param('id');
132 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
135 any [ 'post' ] => '/data/:database/:entity' => sub {
137 my $database = $self->param('database');
138 my $entity = $self->param('entity');
139 my $json = $self->req->json;
140 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
142 warn "## $database $entity $id body ",dump($self->req->body, $json);
144 $json->{'$id'} ||= $id; # make sure $id is in there
146 my $rev = _couchdb_put "/$database/$entity.$id" => $json;
147 $json->{_rev} = $rev;
149 _render_jsonp( $self, $json );
153 get '/' => sub { shift->redirect_to('/Cookbook') };
155 get '/Cookbook' => 'Cookbook';
156 get '/Cookbook/:example' => sub {
158 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
161 get '/conference/:page' => sub {
163 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
168 get '/app/:database/angular.js' => sub {
170 my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
171 warn "# $ANGULAR_JS";
172 $self->render_static( $ANGULAR_JS );
175 # CouchDB proxy for _design _view
177 get '/:database/_design/:design/_view/:view' => sub {
179 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
180 my $param = $self->req->url->query->clone->remove('callback')->to_string;
181 $url .= '?' . $param if $param;
182 warn "CouchDB proxy $url";
183 _render_jsonp( $self, _couchdb_get($url));
186 # static JSON files from public/json/database/entity/json
189 _render_jsonp( shift, [ map { s{public/json/}{}; $_ } glob 'public/json/*' ] );
192 get '/json/:database' => sub {
194 my $database = $self->param('database');
197 document_counts => 0,
201 foreach my $path ( glob "public/json/$database/*" ) {
202 my @entities = glob "$path/*";
203 $path =~ s{public/json/$database/}{};
204 $status->{entities}->{$path} = scalar @entities;
205 $status->{document_counts}++;
208 _render_jsonp( $self, $status );
211 get '/json/:database/:entity' => sub {
214 my $database = $self->param('database');
215 my $entity = $self->param('entity');
217 my $path = "public/json/$database/$entity";
218 die "$path: $!" unless -d $path;
221 foreach my $path ( sort glob "$path/*" ) {
222 open(my $fh, '<', $path) || die $!;
226 my $data = Mojo::JSON->new->decode( $str );
227 $data->{_key} = $1 if $path =~ m{/([^/]+$)};
231 _render_jsonp( $self, $docs )
241 @@ layouts/funky.html.ep
242 <!doctype html><html>
243 <head><title>Funky!</title></head>
244 <body><%== content %></body>
247 @@ layouts/angular.html.ep
249 <html xmlns:ng="http://angularjs.org">
251 <meta charset="utf-8">
252 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
253 <script type="text/javascript"
254 src="<%== $ANGULAR_JS %>" ng:autobind></script>
256 <body><%== content %></body>