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_text("...");
61 $self->render_static('/en/index.html'); # main AngularJS application page
66 $self->render_static('/hr/index.html'); # main AngularJS application page
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;
140 if ( exists $json->{'id'} ) { # @id in resource
142 warn "EXISTING $id\n";
144 $id = $json->{'id'} = new_uuid;
145 $json->{entity} = $entity;
148 warn "## $database $entity $id body ",dump($self->req->body, $json);
150 my $new = _couchdb_put "/$database/$entity.$id" => $json;
151 warn "new: ",dump($new);
153 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
155 warn "ERROR: ",dump($new);
156 $json->{error} = $new;
159 _render_jsonp( $self, $json );
163 #get '/' => sub { shift->redirect_to('/app/') };
165 # CouchDB proxy for _design _view
167 get '/:database/_design/:design/_view/:view' => sub {
169 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
170 my $param = $self->req->url->query->clone->remove('callback')->to_string;
171 $url .= '?' . $param if $param;
172 warn "CouchDB proxy $url";
173 _render_jsonp( $self, _couchdb_get($url));