-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcouchdb-view-server.pl
More file actions
executable file
·68 lines (58 loc) · 1.29 KB
/
couchdb-view-server.pl
File metadata and controls
executable file
·68 lines (58 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/perl
use warnings;
use strict;
# http://wiki.apache.org/couchdb/View_server
#
# /etc/couchdb/local.ini add:
#
# [query_servers]
# perl = /usr/bin/perl /srv/angular-mojolicious/couchdb-view-server.pl
#
# example view:
#
# sub { [ undef, shift ] }
use JSON::XS;
use IO::Handle;
use Data::Dump qw(dump);
my $j = JSON::XS->new;
my $in = IO::Handle->new_from_fd(\*STDIN, 'r');
my $out = IO::Handle->new_from_fd(\*STDOUT, 'w');
$out->autoflush(1);
open(my $l_fh, '>>', "/tmp/couchdb-perl-view.log");
$l_fh->autoflush(1);
sub _debug {
print $l_fh "@_\n";
}
sub _log {
$out->print($j->encode([ 'log' => @_ ]), "\n");
}
our @fun;
while(defined(my $line = $in->getline)) {
chomp $line;
_debug $line;
my $input = $j->decode($line);
my ($cmd, @args) = @$input;
if ( $cmd eq 'reset' ) {
@fun = ();
$out->print("true\n");
} elsif ( $cmd eq 'add_fun' ) {
push @fun, eval $args[0];
if ( $@ ) {
$out->print( qq|{"error": "$!", "reason": "$@"}\n| );
} else {
$out->print("true\n");
}
} elsif ( $cmd eq 'map_doc' ) {
my @results;
foreach my $fun ( @fun ) {
my $d = eval { $fun->(@args) };
_log $@ if $@;
push @results, [$d];
}
my $json = $j->utf8->encode( \@results );
$out->print("$json\n");
_debug "# $json";
} else {
_log "$cmd unimplemented", dump( $input );
}
}