comment out work submission, leave just registration
[angular-drzb] / angular-server.pl
index 7b75c41..ba1cc10 100755 (executable)
@@ -69,6 +69,16 @@ helper locale => sub {
        return $locale{ $lang } || "MISSING $lang $_[1]";
 };
 
+helper ip => sub {
+       my $self = shift;
+       return
+               $self->req->headers->header('X-Forwarded-For')
+               || $self->req->headers->header('X-Real-IP')
+               || $self->tx->{remote_address}
+       ;
+};
+
+
 get '/js/services.js' => sub {
        my $self = shift;
        $self->stash( VERSION => $VERSION );
@@ -96,6 +106,12 @@ get '/lang/:lang/.template' => sub {
        $self->render( $self->stash('template') , lang => $self->stash('lang') );
 };
 
+get '/lang/:lang/template/*template' => sub { # angular-ui templates
+       my $self = shift;
+       my $path = '/lib/angular-ui/bootstrap/template/' . $self->stash('template');
+       warn "# render_static $path";
+       $self->render_static( $path );
+};
 
 get '/data/' => sub {
        my $self = shift;
@@ -175,6 +191,12 @@ any [ 'post' ] => '/data/:database/:entity' => sub {
                $json->{entity} = $entity;
                warn "NEW $id\n";
        }
+
+       $json->{x_audit} = {
+               t  => Time::HiRes::time,
+               ip => $self->ip(),
+       };
+
        warn "## $database $entity $id body ",dump($self->req->body, $json);
 
        my $new = _couchdb_put "/$database/$entity.$id" => $json;
@@ -194,6 +216,8 @@ any [ 'post' ] => '/data/:database/:entity' => sub {
 
 # CouchDB proxy for _design _view
 
+our $view_cache;
+
 get '/:database/_design/:design/_view/:view' => sub {
        my $self = shift;
        my $format = $self->param('format');
@@ -201,6 +225,19 @@ get '/:database/_design/:design/_view/:view' => sub {
        if ( my $param = $self->req->url->query->clone->remove('callback')->remove('format')->to_string ) {
                $url .= '?' . $param
        }
+
+       if ( exists $view_cache->{$url}->{time} ) {
+               if ( time() - $view_cache->{$url}->{time} < 60 ) {
+                       warn "HIT CouchDB cache $url";
+                       $view_cache->{$url}->{hit}++;
+                       return _render_jsonp( $self, $view_cache->{$url}->{json} );
+               } else {
+                       warn "REFRESH CouchDB cache $url";
+                       $view_cache->{$url}->{refresh}++;
+               }
+       } else {
+               $view_cache->{$url}->{miss}++;
+       }
        warn "CouchDB proxy $url";
        my $json = _couchdb_get($url);
 
@@ -235,6 +272,12 @@ if ( doc.user.organization != '' ) {
        if ( $format eq 'key_array' ) { # array of keys sorted by value
                $json->{rows} = [ map { $_->{key} } sort { $b->{value} <=> $a->{value} } @{ $json->{rows} } ];
        }
+
+       $view_cache->{$url}->{time} = time();
+       $view_cache->{$url}->{json} = $json;
+
+       warn "# view_cache ",dump($view_cache);
+
        _render_jsonp( $self, $json );
 };
 
@@ -256,7 +299,7 @@ hook after_dispatch => sub {
        my $browser_etag = $self->req->headers->header('If-None-Match');
        return unless $browser_etag && $browser_etag eq $our_etag;
 
-       $self->app->log->info("HTTP cache hit ", dump( $self->req->url->to_string ), $our_etag );
+       $self->app->log->debug("HTTP cache hit " . $self->req->url->to_string . " $our_etag" );
 
        $self->res->code(304);
        $self->res->body('');