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;
25 my ( $url, $data ) = @_;
29 $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
31 my $json = Mojo::JSON->new->encode( $data );
35 warn "# _couchdb_put $url = $json";
36 return $client->put( "$couchdb/$url" => $json)->res->json;
41 my $return = $client->get( "$couchdb/$url" )->res->json;
42 # warn "# _couchdb_get $url = ",dump($return);
51 my ( $self, $json ) = @_;
52 #warn "## _render_json ",dump($json);
53 my $data = $self->render( json => $json, partial => 1 );
54 #warn "## _render_json $data";
55 if ( my $callback = $self->param('callback') ) {
56 $data = "$callback($data)";
58 $self->render( data => $data, format => 'js' );
63 $self->render_text("...");
68 helper locale => sub {
71 my $lang = $self->stash('lang');
73 return $locale{ $lang } || "MISSING $lang $_[1]";
79 $self->req->headers->header('X-Forwarded-For')
80 || $self->req->headers->header('X-Real-IP')
81 || $self->tx->{remote_address}
86 get '/js/services.js' => sub {
88 $self->stash( VERSION => $VERSION );
89 $self->stash( couchdb_database => $couchdb_database );
90 $self->render( 'js/services', format => 'js' );
94 get '/hr' => sub { shift->redirect_to('/lang/hr/drzb2013') };
95 get '/en' => sub { shift->redirect_to('/lang/en/drzb2013') };
97 get '/lang/:lang/:template' => sub {
99 $self->render( $self->stash('template') , lang => $self->stash('lang') );
102 get '/lang/:lang/partials/:template' => sub {
104 $self->stash( couchdb_view => $couchdb_view );
105 $self->render( 'partials/' . $self->stash('template') , lang => $self->stash('lang') );
108 get '/lang/:lang/.template' => sub {
110 $self->render( $self->stash('template') , lang => $self->stash('lang') );
113 get '/lang/:lang/template/*template' => sub { # angular-ui templates
115 my $path = '/template/' . $self->stash('template');
116 warn "# render_static $path";
117 $self->render_static( $path );
120 get '/data/' => sub {
122 _render_jsonp( $self, _couchdb_get('/_all_dbs') );
125 get '/data/:database' => sub {
127 my $database = $self->param('database');
129 my $list_databases = { name => $database };
131 my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
132 if ( exists $counts->{error} ) {
133 warn "creating CouchDB view because of ", dump($counts);
134 _couchdb_put "/$database/_design/entity", {
135 _id => '_design/entity',
136 language => 'javascript',
139 map => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
140 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
144 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
148 warn "# counts ",dump($counts);
150 foreach my $row ( @{ $counts->{rows} } ) {
151 my $n = $row->{value};
152 $list_databases->{entities}->{ $row->{key} } = $n;
153 $list_databases->{document_counts} += $n;
155 warn dump($list_databases);
156 _render_jsonp( $self, $list_databases );
159 get '/data/:database/:entity' => sub {
162 my $database = $self->param('database');
163 my $entity = $self->param('entity');
165 my $endkey = $entity;
168 my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
169 # warn "# counts ",dump($counts);
171 _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
174 get '/data/:database/:entity/:id' => sub {
177 my $database = $self->param('database');
178 my $entity = $self->param('entity');
179 my $id = $self->param('id');
181 _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
184 any [ 'post' ] => '/data/:database/:entity' => sub {
186 my $database = $self->param('database');
187 my $entity = $self->param('entity');
188 my $json = $self->req->json;
190 if ( exists $json->{'id'} ) { # @id in resource
192 warn "EXISTING $id\n";
194 $id = $json->{'id'} = new_uuid;
195 $json->{entity} = $entity;
200 t => Time::HiRes::time,
204 warn "## $database $entity $id body ",dump($self->req->body, $json);
206 my $new = _couchdb_put "/$database/$entity.$id" => $json;
207 warn "new: ",dump($new);
209 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
211 warn "ERROR: ",dump($new);
212 $json->{error} = $new;
215 _render_jsonp( $self, $json );
219 #get '/' => sub { shift->redirect_to('/app/') };
221 # CouchDB proxy for _design _view
223 get '/:database/_design/:design/_view/:view' => sub {
225 my $format = $self->param('format');
226 my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
227 if ( my $param = $self->req->url->query->clone->remove('callback')->remove('format')->to_string ) {
231 if ( exists $view_cache->{$url}->{time} ) {
232 if ( time() - $view_cache->{$url}->{time} < 60 ) {
233 warn "HIT CouchDB cache $url";
234 $view_cache->{$url}->{hit}++;
235 return _render_jsonp( $self, $view_cache->{$url}->{json} );
237 warn "REFRESH CouchDB cache $url";
238 $view_cache->{$url}->{refresh}++;
241 $view_cache->{$url}->{miss}++;
243 warn "CouchDB proxy $url";
244 my $json = _couchdb_get($url);
246 if ( exists $json->{error} ) {
247 warn "creating CouchDB view because of ", dump($json);
248 my $url = "/" . $self->param('database') . "/_design/registration";
250 _id => '_design/registration',
251 language => 'javascript',
254 map => q| function(doc) {
255 if ( doc.user.organization != '' ) {
256 if ( doc.user.organization )
257 emit(doc.user.organization, 1);
258 if ( doc.work.persons ) {
259 doc.work.persons.forEach( function(person) {
260 if ( person.organization )
261 emit(person.organization, 1);
266 reduce => q| function(keys,values,rereduce) { return sum(values); } |,
271 if ( doc.work.persons ) {
272 doc.work.persons.forEach( function(person) {
273 emit(person.surname+person.firstname ,
278 if ( doc.work.symposium_works ) {
279 doc.work.symposium_works.forEach( function(work) {
280 work.persons.forEach( function(person) {
281 emit(person.surname+person.firstname ,
292 $json = _couchdb_get($url)
296 if ( $format eq 'key_array' ) { # array of keys sorted by value
297 $json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ];
298 } elsif ( $format eq 'key_distinct' ) {
302 push @{ $found->{ $_->{key} } }, $_->{id};
303 $#{ $found->{ $_->{key} } } == 0; # take just first registration
304 } @{ $json->{rows} };
306 $json->{rows} = [ map {
307 $_->{distinct_ids} = $found->{ $_->{key} };
311 $json->{distinct_rows} = scalar @{ $json->{rows} };
312 warn "## distinct stats ", dump( $found );
314 } elsif ( $format ) {
316 die "unknown format: $format";
320 $view_cache->{$url}->{time} = time();
321 $view_cache->{$url}->{json} = $json;
323 warn "# view_cache ",dump($view_cache);
325 _render_jsonp( $self, $json );
328 # http://showmetheco.de/articles/2010/10/adding-etag-caching-to-your-mojolicious-app.html
330 hook after_dispatch => sub {
333 return unless $self->req->method eq 'GET';
335 my $body = $self->res->body;
336 return unless defined $body;
338 return if $self->res->headers->header('ETag');
340 my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum;
341 $self->res->headers->header('ETag' => $our_etag);
343 my $browser_etag = $self->req->headers->header('If-None-Match');
344 return unless $browser_etag && $browser_etag eq $our_etag;
346 $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" );
348 $self->res->code(304);
349 $self->res->body('');