remove in-memory data store and use CouchDB only
[angular-mojolicious.git] / angular-server.pl
1 #!/usr/bin/env perl
2
3 use lib 'common/mojo/lib';
4
5 use Mojolicious::Lite;
6 use Data::Dump qw(dump);
7 use Time::HiRes;
8 use Clone qw(clone);
9
10 sub new_uuid { Time::HiRes::time * 100000 }
11
12 # based on
13 # http://docs.getangular.com/REST.Basic
14 # http://angular.getangular.com/data
15
16 my $couchdb = 'http://localhost:5984';
17 our $couchdb_rev;
18
19 sub _couchdb_put {
20         my ( $database, $entity, $id, $hash ) = @_;
21
22         my $data = clone $hash;
23         delete $data->{_id}; # CouchDB doesn't like _ prefixed attributes, and will generate it's own _id
24         $data->{'$entity'} = $entity;
25         if ( my $rev = $couchdb_rev->{$database}->{$entity}->{$id} ) {
26                 $data->{_rev} = $rev;
27         }
28
29         my $json = Mojo::JSON->new->encode( $data );
30         my $client = Mojo::Client->new;
31
32         warn "# _couchdb_put $couchdb/$database/$entity.$id = $json";
33         $client->put( "$couchdb/$database/$entity.$id" => $json => sub {
34                 my ($client,$tx) = @_;
35                 if ($tx->error) {
36                         die $tx->error;
37                 }
38                 my $response = $tx->res->json;
39                 warn "## CouchDB response ",dump($response);
40                 $couchdb_rev->{$database}->{$entity}->{$id} = $response->{rev} || die "no rev";
41         })->process;
42 }
43
44 sub _couchdb_get {
45         my ( $url ) = @_;
46         my $client = Mojo::Client->new;
47         my $return = $client->get( "$couchdb/$url" )->res->json;
48         warn "# _couchdb_get $url = ",dump($return);
49         return $return;
50 }
51
52 our $id2nr;
53
54
55 sub _render_jsonp {
56         my ( $self, $json ) = @_;
57 #warn "## _render_json ",dump($json);
58         my $data = $self->render( json => $json, partial => 1 );
59 warn "## _render_json $data";
60         if ( my $callback = $self->param('callback') ) {
61                 $data = "$callback($data)";
62         }
63         $self->render( data => $data, format => 'js' );
64 }
65
66 #get '/' => 'index';
67
68 get '/_replicate' => sub {
69         my $self = shift;
70
71         if ( my $from = $self->param('from') ) {
72                 my $got = $self->client->get( $from )->res->json;
73                 warn "# from $from ",dump($got);
74                 _render_jsonp( $self,  $got );
75
76                 my $database = $got->{name};
77                 my $entities = $got->{entities};
78
79                 if ( $database && $entities ) {
80                         foreach my $entity ( keys %$entities ) {
81                                 my $url = $from;
82                                 $url =~ s{/?$}{/}; # add slash at end
83                                 $url .= $entity;
84                                 my $e = $self->client->get( $url )->res->json;
85                                 warn "# replicated $url ", dump($e);
86                                 _chouchdb_put( $self, $database, $entity, $e->{'$id'}, $e );
87                         }
88                 }
89         }
90 };
91
92 get '/data/' => sub {
93         my $self = shift;
94         _render_jsonp( $self, _couchdb_get('/_all_dbs') );
95 };
96
97 get '/data/:database' => sub {
98         die "FIXME";
99 =for FIXME
100         my $self = shift;
101         my $database = $self->param('database');
102         my $list_databases = { name => $database };
103         foreach my $entity ( keys %{ $data->{ $database }} ) {
104 warn "# entry $entity ", dump( $data->{$database}->{$entity} );
105                 my $count = $#{ $data->{$database}->{$entity} } + 1;
106                 $list_databases->{entities}->{$entity} = $count;
107                 $list_databases->{document_count} += $count;
108         }
109         warn dump($list_databases);
110         _render_jsonp( $self,  $list_databases );
111 =cut
112 };
113
114 get '/data/:database/:entity' => sub {
115         my $self = shift;
116         _render_jsonp( $self, _couchdb_get( '/' . $self->param('database') . '/_all_docs' ) ); # FIXME
117 };
118
119 get '/data/:database/:entity/:id' => sub {
120     my $self = shift;
121
122         my $database = $self->param('database');
123         my $entity   = $self->param('entity');
124         my $id       = $self->param('id');
125
126         _render_jsonp( $self, _couchdb_get( "/$database/$entity.$id" ) );
127 };
128
129 any [ 'post' ] => '/data/:database/:entity' => sub {
130         my $self = shift;
131         my $json = $self->req->json;
132         my $id = $json->{'$id'} # XXX we don't get it back from angular.js
133                 || $json->{'_id'}  # so we use our version
134                 || new_uuid;
135         warn "## $id body ",dump($self->req->body, $json);
136
137         $json->{'$id'} ||= $id; # angular.js doesn't resend this one
138         $json->{'_id'} = $id;   # but does this one :-)
139
140         _couchdb_put( $self->param('database'), $self->param('entity'), $id, $json );
141
142         _render_jsonp( $self,  $json );
143 };
144
145
146 get '/' => sub { shift->redirect_to('/Cookbook') };
147
148 get '/Cookbook' => 'Cookbook';
149 get '/Cookbook/:example' => sub {
150         my $self = shift;
151         $self->render( "Cookbook/" . $self->param('example'), layout => 'angular' );
152 };
153
154 get '/conference/:page' => sub {
155         my $self = shift;
156         $self->render( "conference/" . $self->param('page'), layout => 'angular' );
157 };
158
159 app->start;
160 __DATA__
161
162 @@ index.html.ep
163 % layout 'funky';
164 Yea baby!
165
166 @@ layouts/funky.html.ep
167 <!doctype html><html>
168     <head><title>Funky!</title></head>
169     <body><%== content %></body>
170 </html>
171
172 @@ layouts/angular.html.ep
173 <!DOCTYPE HTML>
174 <html xmlns:ng="http://angularjs.org">
175   <head>
176    <meta charset="utf-8">
177 % my $ANGULAR_JS = $ENV{ANGULAR_JS} || ( -e 'public/angular/build/angular.js' ? '/angular/build/angular.js' : '/angular/src/angular-bootstrap.js' );
178     <script type="text/javascript"
179          src="<%== $ANGULAR_JS %>" ng:autobind></script>
180   </head>
181   <body><%== content %></body>
182 </html>