added link to registration
[angular-drzb] / angular-server.pl
1 #!/usr/bin/env perl
2
3 use Mojolicious::Lite;
4 use Data::Dump qw(dump);
5 use Time::HiRes;
6 use Clone qw(clone);
7 use Mojo::UserAgent;
8
9 our $VERSION = `git describe`;
10 chomp $VERSION;
11
12 sub new_uuid { Time::HiRes::time * 100000 }
13
14 #push @{app->static->paths}, 'app'; # default angular-seed app directory
15
16
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;
21
22 our $view_cache;
23
24 sub _couchdb_put {
25         my ( $url, $data ) = @_;
26
27         $view_cache = undef;
28
29         $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
30
31         my $json = Mojo::JSON->new->encode( $data );
32
33         my $rev;
34
35         warn "# _couchdb_put $url = $json";
36         return $client->put( "$couchdb/$url" => $json)->res->json;
37 }
38
39 sub _couchdb_get {
40         my ( $url ) = @_;
41         my $return = $client->get( "$couchdb/$url" )->res->json;
42 #       warn "# _couchdb_get $url = ",dump($return);
43         return $return;
44 }
45
46
47 our $id2nr;
48
49
50 sub _render_jsonp {
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)";
57         }
58         $self->render( data => $data, format => 'js' );
59 }
60
61 get '/' => sub {
62         my $self = shift;
63         $self->render_text("...");
64 };
65
66 # define languages
67
68 helper locale => sub {
69         my $self = shift;
70         my %locale = @_;
71         my $lang = $self->stash('lang');
72         $lang =~ s/-dev$//;
73         return $locale{ $lang } || "MISSING $lang $_[1]";
74 };
75
76 helper ip => sub {
77         my $self = shift;
78         return
79                 $self->req->headers->header('X-Forwarded-For')
80                 || $self->req->headers->header('X-Real-IP')
81                 || $self->tx->{remote_address}
82         ;
83 };
84
85
86 get '/js/services.js' => sub {
87         my $self = shift;
88         $self->stash( VERSION => $VERSION );
89         $self->stash( couchdb_database => $couchdb_database );
90         $self->render( 'js/services', format => 'js' );
91 };
92
93 # short public URLs
94 get '/hr' => sub { shift->redirect_to('/lang/hr/drzb2013') };
95 get '/en' => sub { shift->redirect_to('/lang/en/drzb2013') };
96
97 get '/lang/:lang/:template' => sub {
98         my $self = shift;
99         $self->render( $self->stash('template') , lang => $self->stash('lang') );
100 };
101
102 get '/lang/:lang/partials/:template' => sub {
103         my $self = shift;
104         $self->stash( couchdb_view => $couchdb_view );
105         $self->render( 'partials/' . $self->stash('template') , lang => $self->stash('lang') );
106 };
107
108 get '/lang/:lang/.template' => sub {
109         my $self = shift;
110         $self->render( $self->stash('template') , lang => $self->stash('lang') );
111 };
112
113 get '/lang/:lang/template/*template' => sub { # angular-ui templates
114         my $self = shift;
115         my $path = '/template/' . $self->stash('template');
116         warn "# render_static $path";
117         $self->render_static( $path );
118 };
119
120 get '/data/' => sub {
121         my $self = shift;
122         _render_jsonp( $self, _couchdb_get('/_all_dbs') );
123 };
124
125 get '/data/:database' => sub {
126         my $self = shift;
127         my $database = $self->param('database');
128
129         my $list_databases = { name => $database };
130
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',
137                         views => {
138                                 counts => {
139                                         map    => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
140                                         reduce => q| function(keys,values,rereduce) { return sum(values); } |,
141                                 }
142                         }
143                 };
144                 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
145                 || die "give up!";
146         }
147
148         warn "# counts ",dump($counts);
149
150         foreach my $row ( @{ $counts->{rows} } ) {
151                 my $n = $row->{value};
152                 $list_databases->{entities}->{ $row->{key} } = $n;
153                 $list_databases->{document_counts} += $n;
154         }
155         warn dump($list_databases);
156         _render_jsonp( $self,  $list_databases );
157 };
158
159 get '/data/:database/:entity' => sub {
160         my $self = shift;
161
162         my $database = $self->param('database');
163         my $entity   = $self->param('entity');
164
165         my $endkey = $entity;
166         $endkey++;
167
168         my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
169 #       warn "# counts ",dump($counts);
170
171         _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
172 };
173
174 get '/data/:database/:entity/:id' => sub {
175     my $self = shift;
176
177         my $database = $self->param('database');
178         my $entity   = $self->param('entity');
179         my $id       = $self->param('id');
180
181         _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
182 };
183
184 any [ 'post' ] => '/data/:database/:entity' => sub {
185         my $self = shift;
186         my $database = $self->param('database');
187         my $entity   = $self->param('entity');
188         my $json = $self->req->json;
189         my $id;
190         if ( exists $json->{'id'} ) { # @id in resource
191                 $id = $json->{'id'};
192                 warn "EXISTING $id\n";
193         } else {
194                 $id = $json->{'id'} = new_uuid;
195                 $json->{entity} = $entity;
196                 warn "NEW $id\n";
197         }
198
199         $json->{x_audit} = {
200                 t  => Time::HiRes::time,
201                 ip => $self->ip(),
202         };
203
204         warn "## $database $entity $id body ",dump($self->req->body, $json);
205
206         my $new = _couchdb_put "/$database/$entity.$id" => $json;
207         warn "new: ",dump($new);
208         if ( $new->{ok} ) {
209                 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
210         } else {
211                 warn "ERROR: ",dump($new);
212                 $json->{error} = $new;
213         }
214
215         _render_jsonp( $self,  $json );
216 };
217
218
219 #get '/' => sub { shift->redirect_to('/app/') };
220
221 # CouchDB proxy for _design _view
222
223 get '/:database/_design/:design/_view/:view' => sub {
224         my $self = shift;
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 ) {
228                 $url .= '?' . $param
229         }
230
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} );
236                 } else {
237                         warn "REFRESH CouchDB cache $url";
238                         $view_cache->{$url}->{refresh}++;
239                 }
240         } else {
241                 $view_cache->{$url}->{miss}++;
242         }
243         warn "CouchDB proxy $url";
244         my $json = _couchdb_get($url);
245
246         if ( exists $json->{error} ) {
247                 warn "creating CouchDB view because of ", dump($json);
248                 my $url = "/" . $self->param('database') . "/_design/registration";
249                 _couchdb_put $url, {
250                         _id => '_design/registration',
251                         language => 'javascript',
252                         views => {
253                                 organizations => {
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);
262     });
263   }
264 }
265                                         }q|,
266                                         reduce => q| function(keys,values,rereduce) { return sum(values); } |,
267                                 },
268                                 authors => {
269                                         map => q|
270 function(doc) {
271   if ( doc.work.persons ) {
272     doc.work.persons.forEach( function(person) {
273       emit(person.surname+person.firstname ,
274         person
275       );
276     });
277   }
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 ,
282           person
283         );
284       });
285     });
286   }
287 }
288                                         |,
289                                 },
290                         }
291                 };
292                 $json = _couchdb_get($url)
293                 || die "give up!";
294         }
295
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' ) {
299
300                 my $found;
301                 $json->{rows} = [ grep { ++$found->{ $_->{key} } == 1 } @{ $json->{rows} } ];
302                 $json->{distinct_rows} = scalar @{ $json->{rows} };
303                 warn "## distinct stats ", dump( $found );
304
305         } elsif ( $format ) {
306
307                 die "unknown format: $format";
308
309         }
310
311         $view_cache->{$url}->{time} = time();
312         $view_cache->{$url}->{json} = $json;
313
314         warn "# view_cache ",dump($view_cache);
315
316         _render_jsonp( $self, $json );
317 };
318
319 # http://showmetheco.de/articles/2010/10/adding-etag-caching-to-your-mojolicious-app.html
320
321 hook after_dispatch => sub {
322         my $self = shift;
323
324         return unless $self->req->method eq 'GET';
325
326         my $body = $self->res->body;
327         return unless defined $body;
328
329         return if $self->res->headers->header('ETag');
330
331         my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum;
332         $self->res->headers->header('ETag' => $our_etag);
333
334         my $browser_etag = $self->req->headers->header('If-None-Match');
335         return unless $browser_etag && $browser_etag eq $our_etag;
336
337         $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" );
338
339         $self->res->code(304);
340         $self->res->body('');
341 };
342
343
344 app->start;
345