prepare for customization of pagination template
[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 sub _couchdb_put {
23         my ( $url, $data ) = @_;
24
25         $data->{'$entity'} = $1 if $url =~ m{/(\w+)\.\d+/$/};
26
27         my $json = Mojo::JSON->new->encode( $data );
28
29         my $rev;
30
31         warn "# _couchdb_put $url = $json";
32         return $client->put( "$couchdb/$url" => $json)->res->json;
33 }
34
35 sub _couchdb_get {
36         my ( $url ) = @_;
37         my $return = $client->get( "$couchdb/$url" )->res->json;
38 #       warn "# _couchdb_get $url = ",dump($return);
39         return $return;
40 }
41
42
43 our $id2nr;
44
45
46 sub _render_jsonp {
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)";
53         }
54         $self->render( data => $data, format => 'js' );
55 }
56
57 get '/' => sub {
58         my $self = shift;
59         $self->render_text("...");
60 };
61
62 # define languages
63
64 helper locale => sub {
65         my $self = shift;
66         my %locale = @_;
67         my $lang = $self->stash('lang');
68         $lang =~ s/-dev$//;
69         return $locale{ $lang } || "MISSING $lang $_[1]";
70 };
71
72 helper ip => sub {
73         my $self = shift;
74         return
75                 $self->req->headers->header('X-Forwarded-For')
76                 || $self->req->headers->header('X-Real-IP')
77                 || $self->tx->{remote_address}
78         ;
79 };
80
81
82 get '/js/services.js' => sub {
83         my $self = shift;
84         $self->stash( VERSION => $VERSION );
85         $self->stash( couchdb_database => $couchdb_database );
86         $self->render( 'js/services', format => 'js' );
87 };
88
89 # short public URLs
90 get '/hr' => sub { shift->redirect_to('/lang/hr/drzb2013') };
91 get '/en' => sub { shift->redirect_to('/lang/en/drzb2013') };
92
93 get '/lang/:lang/:template' => sub {
94         my $self = shift;
95         $self->render( $self->stash('template') , lang => $self->stash('lang') );
96 };
97
98 get '/lang/:lang/partials/:template' => sub {
99         my $self = shift;
100         $self->stash( couchdb_view => $couchdb_view );
101         $self->render( 'partials/' . $self->stash('template') , lang => $self->stash('lang') );
102 };
103
104 get '/lang/:lang/.template' => sub {
105         my $self = shift;
106         $self->render( $self->stash('template') , lang => $self->stash('lang') );
107 };
108
109 get '/lang/:lang/template/*template' => sub { # angular-ui templates
110         my $self = shift;
111         my $path = '/template/' . $self->stash('template');
112         warn "# render_static $path";
113         $self->render_static( $path );
114 };
115
116 get '/data/' => sub {
117         my $self = shift;
118         _render_jsonp( $self, _couchdb_get('/_all_dbs') );
119 };
120
121 get '/data/:database' => sub {
122         my $self = shift;
123         my $database = $self->param('database');
124
125         my $list_databases = { name => $database };
126
127         my $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true");
128         if ( exists $counts->{error} ) {
129                 warn "creating CouchDB view because of ", dump($counts);
130                 _couchdb_put "/$database/_design/entity", {
131                         _id => '_design/entity',
132                         language => 'javascript',
133                         views => {
134                                 counts => {
135                                         map    => q| function(doc) { emit(doc._id.split('.')[0],1); } |,
136                                         reduce => q| function(keys,values,rereduce) { return sum(values); } |,
137                                 }
138                         }
139                 };
140                 $counts = _couchdb_get("/$database/_design/entity/_view/counts?group=true")
141                 || die "give up!";
142         }
143
144         warn "# counts ",dump($counts);
145
146         foreach my $row ( @{ $counts->{rows} } ) {
147                 my $n = $row->{value};
148                 $list_databases->{entities}->{ $row->{key} } = $n;
149                 $list_databases->{document_counts} += $n;
150         }
151         warn dump($list_databases);
152         _render_jsonp( $self,  $list_databases );
153 };
154
155 get '/data/:database/:entity' => sub {
156         my $self = shift;
157
158         my $database = $self->param('database');
159         my $entity   = $self->param('entity');
160
161         my $endkey = $entity;
162         $endkey++;
163
164         my $counts = _couchdb_get qq|/$database/_all_docs?startkey="$entity";endkey="$endkey";include_docs=true|;
165 #       warn "# counts ",dump($counts);
166
167         _render_jsonp( $self, [ map { $_->{doc} } @{ $counts->{rows} } ] )
168 };
169
170 get '/data/:database/:entity/:id' => sub {
171     my $self = shift;
172
173         my $database = $self->param('database');
174         my $entity   = $self->param('entity');
175         my $id       = $self->param('id');
176
177         _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
178 };
179
180 any [ 'post' ] => '/data/:database/:entity' => sub {
181         my $self = shift;
182         my $database = $self->param('database');
183         my $entity   = $self->param('entity');
184         my $json = $self->req->json;
185         my $id;
186         if ( exists $json->{'id'} ) { # @id in resource
187                 $id = $json->{'id'};
188                 warn "EXISTING $id\n";
189         } else {
190                 $id = $json->{'id'} = new_uuid;
191                 $json->{entity} = $entity;
192                 warn "NEW $id\n";
193         }
194
195         $json->{x_audit} = {
196                 t  => Time::HiRes::time,
197                 ip => $self->ip(),
198         };
199
200         warn "## $database $entity $id body ",dump($self->req->body, $json);
201
202         my $new = _couchdb_put "/$database/$entity.$id" => $json;
203         warn "new: ",dump($new);
204         if ( $new->{ok} ) {
205                 $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' );
206         } else {
207                 warn "ERROR: ",dump($new);
208                 $json->{error} = $new;
209         }
210
211         _render_jsonp( $self,  $json );
212 };
213
214
215 #get '/' => sub { shift->redirect_to('/app/') };
216
217 # CouchDB proxy for _design _view
218
219 our $view_cache;
220
221 get '/:database/_design/:design/_view/:view' => sub {
222         my $self = shift;
223         my $format = $self->param('format');
224         my $url = join('/', $self->param('database'),'_design',$self->param('design'),'_view',$self->param('view') );
225         if ( my $param = $self->req->url->query->clone->remove('callback')->remove('format')->to_string ) {
226                 $url .= '?' . $param
227         }
228
229         if ( exists $view_cache->{$url}->{time} ) {
230                 if ( time() - $view_cache->{$url}->{time} < 60 ) {
231                         warn "HIT CouchDB cache $url";
232                         $view_cache->{$url}->{hit}++;
233                         return _render_jsonp( $self, $view_cache->{$url}->{json} );
234                 } else {
235                         warn "REFRESH CouchDB cache $url";
236                         $view_cache->{$url}->{refresh}++;
237                 }
238         } else {
239                 $view_cache->{$url}->{miss}++;
240         }
241         warn "CouchDB proxy $url";
242         my $json = _couchdb_get($url);
243
244         if ( exists $json->{error} ) {
245                 warn "creating CouchDB view because of ", dump($json);
246                 my $url = "/" . $self->param('database') . "/_design/registration";
247                 _couchdb_put $url, {
248                         _id => '_design/registration',
249                         language => 'javascript',
250                         views => {
251                                 organizations => {
252                                         map    => q| function(doc) {
253 if ( doc.user.organization != '' ) {
254   if ( doc.user.organization )
255     emit(doc.user.organization, 1);
256   if ( doc.user.persons ) {
257     doc.user.persons.forEach( function(person) {
258       if ( person.organization )
259         emit(person.organization, 1);
260     });
261   }
262 }
263                                         }q|,
264                                         reduce => q| function(keys,values,rereduce) { return sum(values); } |,
265                                 }
266                         }
267                 };
268                 $json = _couchdb_get($url)
269                 || die "give up!";
270         }
271
272         if ( $format eq 'key_array' ) { # array of keys sorted by value
273                 $json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ];
274         }
275
276         $view_cache->{$url}->{time} = time();
277         $view_cache->{$url}->{json} = $json;
278
279         warn "# view_cache ",dump($view_cache);
280
281         _render_jsonp( $self, $json );
282 };
283
284 # http://showmetheco.de/articles/2010/10/adding-etag-caching-to-your-mojolicious-app.html
285
286 hook after_dispatch => sub {
287         my $self = shift;
288
289         return unless $self->req->method eq 'GET';
290
291         my $body = $self->res->body;
292         return unless defined $body;
293
294         return if $self->res->headers->header('ETag');
295
296         my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum;
297         $self->res->headers->header('ETag' => $our_etag);
298
299         my $browser_etag = $self->req->headers->header('If-None-Match');
300         return unless $browser_etag && $browser_etag eq $our_etag;
301
302         $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" );
303
304         $self->res->code(304);
305         $self->res->body('');
306 };
307
308
309 app->start;
310