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 {
126 _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME
129 get '/data/:database/:entity/:id' => sub {
132 my $database = $self->param('database');
133 my $entity = $self->param('entity');
134 my $id = $self->param('id');
136 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
139 any [ 'post' ] => '/data/:database/:entity' => sub {
141 my $database = $self->param('database');
142 my $entity = $self->param('entity');
143 my $json = $self->req->json;
144 my $id = $json->{'$id'} # XXX we don't get it back from angular.js
146 warn "## $database $entity $id body ",dump($self->req->body, $json);
148 $json->{'$id'} ||= $id; # make sure $id is in there
150 _couchdb_put "/$database/$entity.$id" => $json;
152 _render_jsonp( $self, $json );
156 get '/' => sub { shift->redirect_to('/Cookbook') };
158 get '/Cookbook' => 'Cookbook';
159 get '/Cookbook/:example' => sub {
161 $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
164 get '/conference/:page' => sub {
166 $self->render( "conference/" . $self->param('page'), layout => 'angular' );
176 @@ layouts/funky.html.ep
177 <!doctype html><html>
178 <head><title>Funky!</title></head>
179 <body><%== content %></body>
182 @@ layouts/angular.html.ep
184 <html xmlns:ng="http://angularjs.org">
186 <meta charset="utf-8">
187 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
188 <script type="text/javascript"
189 src="<%== $ANGULAR_JS %>" ng:autobind></script>
191 <body><%== content %></body>