X-Git-Url: http://git.rot13.org/?p=angular-mojolicious.git;a=blobdiff_plain;f=angular-server.pl;h=948759928ee073f0f6a416978e284164c6181e9f;hp=d8d9b9e03a9a1c561c01f0a97e22c77eed62b1f1;hb=1c641d9c92981bab0e0fe9ad2e89bdf2e6d3f088;hpb=056811ffc60ac27e286d80dfea9a46f95a6984e4 diff --git a/angular-server.pl b/angular-server.pl index d8d9b9e..9487599 100755 --- a/angular-server.pl +++ b/angular-server.pl @@ -6,6 +6,7 @@ use Mojolicious::Lite; use Data::Dump qw(dump); use Time::HiRes; use Clone qw(clone); +use Mojo::UserAgent; sub new_uuid { Time::HiRes::time * 100000 } @@ -13,8 +14,8 @@ sub new_uuid { Time::HiRes::time * 100000 } # http://docs.getangular.com/REST.Basic # http://angular.getangular.com/data -my $couchdb = 'http://localhost:5984'; -my $client = Mojo::Client->new; +my $couchdb = $ENV{COUCHDB} || 'http://localhost:5984'; +my $client = Mojo::UserAgent->new; sub _couchdb_put { my ( $url, $data ) = @_; @@ -26,20 +27,7 @@ sub _couchdb_put { my $rev; warn "# _couchdb_put $url = $json"; - $client->put( "$couchdb/$url" => $json => sub { - my ($client,$tx) = @_; - my ($message, $code) = $tx->error; - my $response = $tx->res->json; - warn "## response $code ",dump($response); - if ($tx->error) { - warn "ERROR $code $message"; - } - return - $rev = $response->{rev}; - })->process; - - warn "## rev = $rev"; - return $rev; + return $client->put( "$couchdb/$url" => $json)->res->json; } sub _couchdb_get { @@ -142,8 +130,14 @@ any [ 'post' ] => '/data/:database/:entity' => sub { $json->{'$id'} ||= $id; # make sure $id is in there - my $rev = _couchdb_put "/$database/$entity.$id" => $json; - $json->{_rev} = $rev; + my $new = _couchdb_put "/$database/$entity.$id" => $json; + warn "new: ",dump($new); + if ( $new->{ok} ) { + $json->{'_'.$_} = $new->{$_} foreach ( 'rev','id' ); + } else { + warn "ERROR: ",dump($new); + $json->{_error} = $new; + } _render_jsonp( $self, $json ); }; @@ -182,6 +176,94 @@ get '/:database/_design/:design/_view/:view' => sub { _render_jsonp( $self, _couchdb_get($url)); }; +# static JSON files from public/json/database/entity/json + +get '/json' => sub { + _render_jsonp( shift, [ map { s{public/json/}{}; $_ } glob 'public/json/*' ] ); +}; + +get '/json/:database' => sub { + my $self = shift; + my $database = $self->param('database'); + + my $status = { + document_counts => 0, + name => $database, + }; + + foreach my $path ( glob "public/json/$database/*" ) { + my @entities = glob "$path/*"; + $path =~ s{public/json/$database/}{}; + $status->{entities}->{$path} = scalar @entities; + $status->{document_counts}++; + } + + _render_jsonp( $self, $status ); +}; + +get '/json/:database/:entity' => sub { + my $self = shift; + + my $database = $self->param('database'); + my $entity = $self->param('entity'); + + my $path = "public/json/$database/$entity"; + die "$path: $!" unless -d $path; + + my $docs; + foreach my $path ( sort glob "$path/*" ) { + open(my $fh, '<', $path) || die $!; + local $/ = undef; + my $str = <$fh>; + warn "# $path $str"; + my $data = Mojo::JSON->new->decode( $str ); + $data->{_key} = $1 if $path =~ m{/([^/]+$)}; + push @$docs, $data; + } + + _render_jsonp( $self, $docs ) +}; + +# app/resevations +use Encode; +use iCal::Parser; + +get '/reservations/get/(*url)' => sub { + my $self = shift; + + my $text = $client->get( 'http://' . $self->param('url') )->res->body; + warn "# get ", $self->param('url'), dump($text); + + $text = decode( 'utf-8', $text ); + $text =~ s{\\,}{,}gs; + $text =~ s{\\n}{ }gs; + + my $c = iCal::Parser->new->parse_strings( $text ); + + warn "# iCal::Parser = ",dump($c); + + my $ical = { + cal => $c->{cals}->[0], # FIXME assume single calendar + }; + + my $e = $c->{events}; + my @events; + + foreach my $yyyy ( sort keys %$e ) { + foreach my $mm ( sort keys %{ $e->{$yyyy} } ) { + foreach my $dd ( sort keys %{ $e->{$yyyy}->{$mm} } ) { + push @events, values %{ $e->{$yyyy}->{$mm}->{$dd} }; + } + } + } + + $ical->{events} = [ sort { + $a->{DTSTART} cmp $b->{DTSTART} + } @events ]; + + _render_jsonp( $self, $ical ); +}; + app->start; __DATA__