X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=angular-server.pl;h=ba1cc101f376a7688152f3ee31bace5537210fb4;hb=9c5624e352449b2012aada84b3e210a8c043ddac;hp=bec44b8ed59dc1cb5fd222504a7804e36f160c9b;hpb=21335d26a773e971d5d20d9a9806cfa1bc5c1e80;p=angular-drzb diff --git a/angular-server.pl b/angular-server.pl index bec44b8..ba1cc10 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -6,12 +6,17 @@ use Time::HiRes; use Clone qw(clone); use Mojo::UserAgent; +our $VERSION = `git describe`; +chomp $VERSION; + sub new_uuid { Time::HiRes::time * 100000 } #push @{app->static->paths}, 'app'; # default angular-seed app directory my $couchdb = $ENV{COUCHDB} || 'http://localhost:5984'; +my $couchdb_database = 'drzb2013v2'; +my $couchdb_view = "http://10.60.0.92:5984/_utils/document.html?$couchdb_database"; my $client = Mojo::UserAgent->new; sub _couchdb_put { @@ -59,9 +64,32 @@ get '/' => sub { helper locale => sub { my $self = shift; my %locale = @_; - return $locale{ $self->stash('lang') } || "MISSING $_[1]"; + my $lang = $self->stash('lang'); + $lang =~ s/-dev$//; + 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 ); + $self->stash( couchdb_database => $couchdb_database ); + $self->render( 'js/services', format => 'js' ); }; +# short public URLs +get '/hr' => sub { shift->redirect_to('/lang/hr/drzb2013') }; +get '/en' => sub { shift->redirect_to('/lang/en/drzb2013') }; + get '/lang/:lang/:template' => sub { my $self = shift; $self->render( $self->stash('template') , lang => $self->stash('lang') ); @@ -69,6 +97,7 @@ get '/lang/:lang/:template' => sub { get '/lang/:lang/partials/:template' => sub { my $self = shift; + $self->stash( couchdb_view => $couchdb_view ); $self->render( 'partials/' . $self->stash('template') , lang => $self->stash('lang') ); }; @@ -77,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; @@ -156,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; @@ -175,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'); @@ -182,11 +225,59 @@ 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); + + if ( exists $json->{error} ) { + warn "creating CouchDB view because of ", dump($json); + my $url = "/" . $self->param('database') . "/_design/registration"; + _couchdb_put $url, { + _id => '_design/registration', + language => 'javascript', + views => { + organizations => { + map => q| function(doc) { +if ( doc.user.organization != '' ) { + if ( doc.user.organization ) + emit(doc.user.organization, 1); + if ( doc.user.persons ) { + doc.user.persons.forEach( function(person) { + if ( person.organization ) + emit(person.organization, 1); + }); + } +} + }q|, + reduce => q| function(keys,values,rereduce) { return sum(values); } |, + } + } + }; + $json = _couchdb_get($url) + || die "give up!"; + } + 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 ); }; @@ -202,13 +293,13 @@ hook after_dispatch => sub { return if $self->res->headers->header('ETag'); - my $our_etag = Mojo::ByteStream->new($body)->md5_sum; + my $our_etag = Mojo::ByteStream->new($body . $VERSION)->md5_sum; $self->res->headers->header('ETag' => $our_etag); 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('');