4 use Data::Dump qw(dump);
9 sub new_uuid { Time::HiRes::time * 100000 }
11 push @{app->static->paths}, 'app'; # default angular-seed app directory
14 my $couchdb = $ENV{COUCHDB} || 'http://localhost:5984';
15 my $client = Mojo::UserAgent->new;
18 my ( $url, $data ) = @_;
20 $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
22 my $json = Mojo::JSON->new->encode( $data );
26 warn "# _couchdb_put $url = $json";
27 return $client->put( "$couchdb/$url" => $json)->res->json;
32 my $return = $client->get( "$couchdb/$url" )->res->json;
33 warn "# _couchdb_get $url = ",dump($return);
42 my ( $self, $json ) = @_;
43 #warn "## _render_json ",dump($json);
44 my $data = $self->render( json => $json, partial => 1 );
45 warn "## _render_json $data";
46 if ( my $callback = $self->param('callback') ) {
47 $data = "$callback($data)";
49 $self->render( data => $data, format => 'js' );
54 $self->render_static('/index.html'); # main AngularJS application page
60 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
63 get '/data/:database' => sub {
65 my $database = $self->param('database');
67 my $list_databases = { name => $database };
69 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
70 if ( exists $counts->{error} ) {
71 warn "creating CouchDB view because of ", dump($counts);
72 _couchdb_put "/$database/_design/entity", {
73 _id => '_design/entity',
74 language => 'javascript',
77 map => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
78 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
82 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
86 warn "# counts ",dump($counts);
88 foreach my $row ( @{ $counts->{rows} } ) {
89 my $n = $row->{value};
90 $list_databases->{entities}->{ $row->{key} } = $n;
91 $list_databases->{document_counts} += $n;
93 warn dump($list_databases);
94 _render_jsonp( $self, $list_databases );
97 get '/data/:database/:entity' => sub {
100 my $database = $self->param('database');
101 my $entity = $self->param('entity');
103 my $endkey = $entity;
106 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
107 warn "# counts ",dump($counts);
109 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
112 get '/data/:database/:entity/:id' => sub {
115 my $database = $self->param('database');
116 my $entity = $self->param('entity');
117 my $id = $self->param('id');
119 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
122 any [ 'post' ] => '/data/:database/:entity' => sub {
124 my $database = $self->param('database');
125 my $entity = $self->param('entity');
126 my $json = $self->req->json;
128 if ( exists $json->{'id'} ) { # @id in resource
130 warn "EXISTING $id\n";
132 $id = $json->{'id'} = new_uuid;
133 $json->{entity} = $entity;
136 warn "## $database $entity $id body ",dump($self->req->body, $json);
138 my $new = _couchdb_put "/$database/$entity.$id" => $json;
139 warn "new: ",dump($new);
141 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
143 warn "ERROR: ",dump($new);
144 $json->{error} = $new;
147 _render_jsonp( $self, $json );
151 #get '/' => sub { shift->redirect_to('/app/') };
153 # CouchDB proxy for _design _view
155 get '/:database/_design/:design/_view/:view' => sub {
157 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
158 my $param = $self->req->url->query->clone->remove('callback')->to_string;
159 $url .= '?' . $param if $param;
160 warn "CouchDB proxy $url";
161 _render_jsonp( $self, _couchdb_get($url));