Merge branch 'master' of github.com:dpavlin/angular-mojolicious
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 1 Dec 2010 14:22:19 +0000 (15:22 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 1 Dec 2010 14:22:19 +0000 (15:22 +0100)
couchdb-trigger.pl
public/app/drzb2011/registrations.html [new file with mode: 0644]
public/app/drzb2011/replication-pull.sh [new file with mode: 0755]
trigger/email.pm [new file with mode: 0644]
trigger/shell.pm [new file with mode: 0644]

index d39f007..02f5c32 100755 (executable)
@@ -18,21 +18,14 @@ use lib 'common/mojo/lib';
 use Mojo::Client;
 use Mojo::JSON;
 use Time::HiRes qw(time);
+use Data::Dump qw(dump);
 
-my $url = 'http://localhost:5984/monitor';
+my ( $url, $trigger_path ) = @ARGV;
 
-sub _trigger {
-       my $trigger = $_[0]->{trigger};
-       if ( my $command = $trigger->{command} ) {
-               # FIXME SECURITY HOLE
-               my $output = $trigger->{output} = `$command`;
+$url          ||= 'http://localhost:5984/monitor';
+$trigger_path ||= 'trigger/shell.pm' ;
 
-               $trigger->{output} =
-                       [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ]
-                       if $trigger->{format} =~ m/table/i;
-       }
-       return $trigger;
-}
+require $trigger_path if -e $trigger_path;
 
 my $seq = 0;
 
@@ -77,11 +70,11 @@ while( ! $error ) {
 
                                debug 'change' => $change;
 
-                               if ( my $trigger = $change->{doc}->{trigger} ) {
-                                       if ( exists $trigger->{active} ) {
+                               if ( filter($change) ) {
+                                       if ( exists $change->{doc}->{trigger}->{active} ) {
                                                debug 'trigger.active',  $change->{doc}->{trigger}->{active};
                                        } else {
-                                               $trigger->{active} = [ time() ];
+                                               $change->{doc}->{trigger}->{active} = [ time() ];
 
                                                debug 'TRIGGER start PUT ', $change->{doc};
                                                $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
@@ -97,9 +90,9 @@ while( ! $error ) {
                                                                $change->{doc}->{_rev} = $res->{rev};
 
                                                                debug "TRIGGER execute ", $change->{doc};
-                                                               _trigger( $change->{doc} );
+                                                               trigger( $change );
 
-                                                               push @{ $trigger->{active} }, time(), 0; # last timestamp
+                                                               push @{ $change->{doc}->{trigger}->{active} }, time(), 0; # last timestamp
 
                                                                $client->put( "$url/$id" => $json->encode( $change->{doc} ) => sub {
                                                                        my ($client,$tx) = @_;
diff --git a/public/app/drzb2011/registrations.html b/public/app/drzb2011/registrations.html
new file mode 100644 (file)
index 0000000..cf7c36d
--- /dev/null
@@ -0,0 +1,57 @@
+<!DOCTYPE HTML>
+<html xmlns:ng="http://angularjs.org">
+<head>
+<meta charset="utf-8">
+<script src="angular.js" ng:autobind></script>
+
+<script>
+Registrations.$inject = ['$xhr']; 
+
+function Registrations(xhr){ 
+       this.xhr = xhr; 
+       var self = this;
+       this.xhr("JSON"
+               , "/data/drzb2011/Registration?callback=JSON_CALLBACK"
+               , function(code, response){ 
+                       console.log('xhr JSON', code, response);
+                       self.data = response;
+               }
+       ); 
+
+} 
+
+</script>
+
+<title>DRZB2011 Registrations</title>
+
+<style>
+.address {
+       font-size: 80%;
+}
+</style>
+
+</head>
+<body>
+
+<div ng:controller="Registrations"> 
+
+<ul>
+<li ng:repeat="registration in data">
+{{registration.person.name}}
+{{registration.person.surname}}
+&lt;{{registration.person.email}}&gt;
+<div class=address>{{registration.person.inst}}</div>
+<div class=address>{{registration.person.zip}} {{registration.person.city}}</div>
+</li>
+</ul>
+
+<input name=debug type=checkbox>
+<pre ng:show="debug">
+data={{data}}
+</pre>
+
+</div>
+
+
+</body>
+</html>
diff --git a/public/app/drzb2011/replication-pull.sh b/public/app/drzb2011/replication-pull.sh
new file mode 100755 (executable)
index 0000000..12c8eb4
--- /dev/null
@@ -0,0 +1,3 @@
+#!/bin/sh -x
+
+curl -v -X POST localhost:5984/_replicate -d '{"source":"http://10.60.0.95:5984/drzb2011","target":"drzb2011"}'
diff --git a/trigger/email.pm b/trigger/email.pm
new file mode 100644 (file)
index 0000000..e9739f9
--- /dev/null
@@ -0,0 +1,12 @@
+sub filter {
+       my $change = shift;
+       return 1 if $change->{doc}->{person}->{email};
+}
+
+sub trigger {
+       my $change = shift;
+       warn "# send_email ",dump($change->{doc}->{person});
+       $change->{doc}->{email_sent}++;
+}
+
+1;
diff --git a/trigger/shell.pm b/trigger/shell.pm
new file mode 100644 (file)
index 0000000..1c89f8b
--- /dev/null
@@ -0,0 +1,20 @@
+sub filter {
+       my $change = shift;
+       return 1 if $change->{doc}->{trigger}->{command};
+}
+
+
+sub trigger {
+       my $change = shift;
+       my $trigger = $change->{doc}->{trigger};
+       if ( my $command = $trigger->{command} ) {
+               # FIXME SECURITY HOLE
+               my $output = $trigger->{output} = `$command`;
+
+               $trigger->{output} =
+                       [ map { [ split (/\s+/,$_) ] } split(/\n/,$output) ]
+                       if $trigger->{format} =~ m/table/i;
+       }
+}
+
+1;