4 use Data::Dump qw(dump);
9 our $VERSION = `git describe`;
12 sub new_uuid { Time::HiRes::time * 100000 }
14 #push @{app->static->paths}, 'app'; # default angular-seed app directory
17 my $couchdb = $ENV{COUCHDB} || 'http://localhost:5984';
18 my $couchdb_database = 'drzb2013v2';
19 my $couchdb_view = "http://10.60.0.92:5984/_utils/document.html?$couchdb_database";
20 my $client = Mojo::UserAgent->new;
23 my ( $url, $data ) = @_;
25 $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
27 my $json = Mojo::JSON->new->encode( $data );
31 warn "# _couchdb_put $url = $json";
32 return $client->put( "$couchdb/$url" => $json)->res->json;
37 my $return = $client->get( "$couchdb/$url" )->res->json;
38 # warn "# _couchdb_get $url = ",dump($return);
47 my ( $self, $json ) = @_;
48 #warn "## _render_json ",dump($json);
49 my $data = $self->render( json => $json, partial => 1 );
50 #warn "## _render_json $data";
51 if ( my $callback = $self->param('callback') ) {
52 $data = "$callback($data)";
54 $self->render( data => $data, format => 'js' );
59 $self->render_text("...");
64 helper locale => sub {
67 my $lang = $self->stash('lang');
69 return $locale{ $lang } || "MISSING $lang $_[1]";
72 get '/js/services.js' => sub {
74 $self->stash( VERSION => $VERSION );
75 $self->stash( couchdb_database => $couchdb_database );
76 $self->render( 'js/services', format => 'js' );
80 get '/hr' => sub { shift->redirect_to('/lang/hr/drzb2013') };
81 get '/en' => sub { shift->redirect_to('/lang/en/drzb2013') };
83 get '/lang/:lang/:template' => sub {
85 $self->render( $self->stash('template') , lang => $self->stash('lang') );
88 get '/lang/:lang/partials/:template' => sub {
90 $self->stash( couchdb_view => $couchdb_view );
91 $self->render( 'partials/' . $self->stash('template') , lang => $self->stash('lang') );
94 get '/lang/:lang/.template' => sub {
96 $self->render( $self->stash('template') , lang => $self->stash('lang') );
100 get '/data/' => sub {
102 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
105 get '/data/:database' => sub {
107 my $database = $self->param('database');
109 my $list_databases = { name => $database };
111 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
112 if ( exists $counts->{error} ) {
113 warn "creating CouchDB view because of ", dump($counts);
114 _couchdb_put "/$database/_design/entity", {
115 _id => '_design/entity',
116 language => 'javascript',
119 map => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
120 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
124 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
128 warn "# counts ",dump($counts);
130 foreach my $row ( @{ $counts->{rows} } ) {
131 my $n = $row->{value};
132 $list_databases->{entities}->{ $row->{key} } = $n;
133 $list_databases->{document_counts} += $n;
135 warn dump($list_databases);
136 _render_jsonp( $self, $list_databases );
139 get '/data/:database/:entity' => sub {
142 my $database = $self->param('database');
143 my $entity = $self->param('entity');
145 my $endkey = $entity;
148 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
149 # warn "# counts ",dump($counts);
151 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
154 get '/data/:database/:entity/:id' => sub {
157 my $database = $self->param('database');
158 my $entity = $self->param('entity');
159 my $id = $self->param('id');
161 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
164 any [ 'post' ] => '/data/:database/:entity' => sub {
166 my $database = $self->param('database');
167 my $entity = $self->param('entity');
168 my $json = $self->req->json;
170 if ( exists $json->{'id'} ) { # @id in resource
172 warn "EXISTING $id\n";
174 $id = $json->{'id'} = new_uuid;
175 $json->{entity} = $entity;
178 warn "## $database $entity $id body ",dump($self->req->body, $json);
180 my $new = _couchdb_put "/$database/$entity.$id" => $json;
181 warn "new: ",dump($new);
183 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
185 warn "ERROR: ",dump($new);
186 $json->{error} = $new;
189 _render_jsonp( $self, $json );
193 #get '/' => sub { shift->redirect_to('/app/') };
195 # CouchDB proxy for _design _view
197 get '/:database/_design/:design/_view/:view' => sub {
199 my $format = $self->param('format');
200 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
201 if ( my $param = $self->req->url->query->clone->remove('callback')->remove('format')->to_string ) {
204 warn "CouchDB proxy $url";
205 my $json = _couchdb_get($url);
207 if ( exists $json->{error} ) {
208 warn "creating CouchDB view because of ", dump($json);
209 my $url = "/" . $self->param('database') . "/_design/registration";
211 _id => '_design/registration',
212 language => 'javascript',
215 map => q| function(doc) {
216 if ( doc.user.organization != '' ) {
217 if ( doc.user.organization )
218 emit(doc.user.organization, 1);
219 if ( doc.user.persons ) {
220 doc.user.persons.forEach( function(person) {
221 if ( person.organization )
222 emit(person.organization, 1);
227 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
231 $json = _couchdb_get($url)
235 if ( $format eq 'key_array' ) { # array of keys sorted by value
236 $json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ];
238 _render_jsonp( $self, $json );
241 # http://showmetheco.de/articles/2010/10/adding-etag-caching-to-your-mojolicious-app.html
243 hook after_dispatch => sub {
246 return unless $self->req->method eq 'GET';
248 my $body = $self->res->body;
249 return unless defined $body;
251 return if $self->res->headers->header('ETag');
253 my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum;
254 $self->res->headers->header('ETag' => $our_etag);
256 my $browser_etag = $self->req->headers->header('If-None-Match');
257 return unless $browser_etag && $browser_etag eq $our_etag;
259 $self->app->log->info("HTTP cache hit ", dump( $self->req->url->to_string ), $our_etag );
261 $self->res->code(304);
262 $self->res->body('');