+ my $database = $self->param('database');
+ my $entity = $self->param('entity');
+ my $id = $self->param('id');
+
+ _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
+};
+
+any [ 'post' ] => '/data/:database/:entity' => sub {
+ my $self = shift;
+ my $database = $self->param('database');
+ my $entity = $self->param('entity');
+ my $json = $self->req->json;
+ my $id = $json->{'$id'} # XXX we don't get it back from angular.js
+ || new_uuid;
+ warn "## $database $entity $id body ",dump($self->req->body, $json);
+
+ $json->{'$id'} ||= $id; # make sure $id is in there
+
+ my $new = _couchdb_put "/$database/$entity.$id" => $json;
+ warn "new: ",dump($new);
+ if ( $new->{ok} ) {
+ $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
+ } else {
+ warn "ERROR: ",dump($new);
+ $json->{error} = $new;
+ }
+
+ _render_jsonp( $self, $json );
+};
+
+
+get '/' => sub { shift->redirect_to('/Cookbook') };
+
+get '/Cookbook' => 'Cookbook';
+get '/Cookbook/:example' => sub {
+ my $self = shift;
+ $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
+};
+
+get '/conference/:page' => sub {
+ my $self = shift;
+ $self->render( "conference/" . $self->param('page'), layout => 'angular' );
+};
+
+# /app/
+
+get '/app/:database/angular.js' => sub {
+ my $self = shift;
+ my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
+ warn "# $ANGULAR_JS";
+ $self->render_static( $ANGULAR_JS );
+};
+
+# CouchDB proxy for _design _view
+
+get '/:database/_design/:design/_view/:view' => sub {
+ my $self = shift;
+ my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
+ my $param = $self->req->url->query->clone->remove('callback')->to_string;
+ $url .= '?' . $param if $param;
+ warn "CouchDB proxy $url";
+ _render_jsonp( $self, _couchdb_get($url));
+};
+
+# static JSON files from public/json/database/entity/json
+
+get '/json' => sub {
+ _render_jsonp( shift, [ map { s{public/json/}{}; $_ } glob 'public/json/*' ] );
+};
+
+get '/json/:database' => sub {
+ my $self = shift;
+ my $database = $self->param('database');
+
+ my $status = {
+ document_counts => 0,
+ name => $database,
+ };
+
+ foreach my $path ( glob "public/json/$database/*" ) {
+ my @entities = glob "$path/*";
+ $path =~ s{public/json/$database/}{};
+ $status->{entities}->{$path} = scalar @entities;
+ $status->{document_counts}++;
+ }
+
+ _render_jsonp( $self, $status );