10 |
use Storable; |
use Storable; |
11 |
|
|
12 |
our $VERSION = '0.1'; |
our $VERSION = '0.1'; |
13 |
$VERSION .= '-Frey-' . $Frey::VERSION; |
$VERSION .= ' on Frey ' . $Frey::VERSION; |
14 |
|
|
15 |
our $debug = $Frey::debug || 0; |
our $debug = $Frey::debug || 0; |
16 |
|
|
28 |
} |
} |
29 |
} |
} |
30 |
|
|
31 |
my $path = '/data/webpac2/var/ds'; |
our $config = { |
32 |
|
path => '/data/webpac2/var/row', |
33 |
|
}; |
34 |
|
|
35 |
|
my $p = $config->{path}; |
36 |
my @all_dbs = map { |
my @all_dbs = map { |
37 |
s{^\Q$path\E/*}{}; |
s{^\Q$p\E/*}{}; |
38 |
$_; |
$_; |
39 |
} glob "$path/*/*"; |
} glob "$p/*/*"; |
40 |
|
|
41 |
my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+'; |
my $regex_dbs = '[a-z][a-z0-9_\$\(\)\+\-/]+'; |
42 |
|
|
48 |
$status = 200; |
$status = 200; |
49 |
} |
} |
50 |
|
|
51 |
|
|
52 |
sub dispatch { |
sub dispatch { |
53 |
my ($self,$tx) = @_; |
my ($self,$tx) = @_; |
54 |
|
|
55 |
my $url = $tx->req->url->to_string; |
my $url = $tx->req->url->to_string; |
56 |
$url = uri_unescape( $url ); |
$url = uri_unescape( $url ); |
57 |
my $method = $tx->req->method; |
my $method = $tx->req->method; |
58 |
|
|
59 |
if ( $url eq '/' ) { |
if ( $url eq '/' ) { |
60 |
$json = { |
$json = { |
61 |
couchdb => "Welcome", |
couchdb => "Welcome", |
62 |
version => $VERSION, |
version => "CouchAPI $VERSION", |
63 |
} |
} |
64 |
} elsif ( $url eq '/_all_dbs' ) { |
} elsif ( $url eq '/_all_dbs' ) { |
65 |
$json = [ @all_dbs ]; |
$json = [ @all_dbs ]; |
66 |
$status = 200; |
$status = 200; |
67 |
} elsif ( $url =~ m{^/_config} ) { |
} elsif ( $url =~ m{^/_config/?(.+)} ) { |
68 |
$json = { |
|
69 |
couchdb => { |
$json = { CouchAPI => $config }; |
70 |
version => $VERSION, |
|
71 |
path => $path, |
if ( $method eq 'PUT' ) { |
72 |
|
|
73 |
|
my $part = $1; |
74 |
|
warn "## part $part"; |
75 |
|
|
76 |
|
$part =~ s!^!->{'!; |
77 |
|
$part =~ s!/!'}->{'!; |
78 |
|
$part =~ s/$/'}/; |
79 |
|
|
80 |
|
my $data = $tx->req->content->file->slurp; |
81 |
|
$data = JSON->new->allow_nonref->decode( $data ); |
82 |
|
warn "## data ",dump( $data ); |
83 |
|
# poor man's transaction :-) |
84 |
|
my $code = "\$json$part = \$data; \$config$part = \$data;"; |
85 |
|
eval $code; |
86 |
|
if ( $@ ) { |
87 |
|
warn "ERROR: $code -> $@"; |
88 |
|
$status = 500; |
89 |
|
} else { |
90 |
|
$status = 200; |
91 |
} |
} |
92 |
}; |
|
93 |
$status = 200; |
warn "json ",dump( $json ), " config ", dump( $config ); |
94 |
|
|
95 |
|
} elsif ( $method eq 'GET' ) { |
96 |
|
$status = 200; |
97 |
|
} else { |
98 |
|
$status = 501; |
99 |
|
} |
100 |
|
|
101 |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
} elsif ( $url =~ m{($regex_dbs)/$} ) { |
102 |
|
|
103 |
my $database = $1; |
my $database = $1; |
104 |
my $dir = "$path/$database"; |
my $dir = "$config->{path}/$database"; |
105 |
|
|
106 |
if ( $method eq 'GET' ) { |
if ( $method eq 'GET' ) { |
107 |
$json = database_get( $database ); |
$json = database_get( $database ); |
130 |
$arg->{$n} = $v; |
$arg->{$n} = $v; |
131 |
} |
} |
132 |
} |
} |
133 |
|
|
134 |
|
my $path = $config->{path}; |
135 |
|
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
136 |
|
|
137 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
138 |
warn "## database: $database id: $id -> $p [$args]\n"; |
warn "## database: $database id: $id -> $p [$args]\n"; |
139 |
|
|
161 |
} |
} |
162 |
} glob( "$path/$database/*" ); |
} glob( "$path/$database/*" ); |
163 |
|
|
164 |
warn "## docs ", dump( @docs ); |
warn "## docs ", dump( @docs ) if $debug; |
165 |
|
|
166 |
$json = { |
$json = { |
167 |
total_rows => $total_rows, |
total_rows => $total_rows, |
193 |
my $descending = delete $arg->{descending}; |
my $descending = delete $arg->{descending}; |
194 |
my @sorted = sort @ids; |
my @sorted = sort @ids; |
195 |
|
|
196 |
|
warn "creating rows in ", $descending ? "descending" : "", " order\n"; |
197 |
|
|
198 |
foreach my $id ( $descending ? reverse @sorted : @sorted ) { |
foreach my $id ( $descending ? reverse @sorted : @sorted ) { |
199 |
warn ">> $id ", $descending ? 'desc' : 'asc', "\n"; |
warn ">> $id ", $descending ? 'desc' : 'asc', "\n" if $debug; |
200 |
push @{ $json->{rows} }, $rows->{$id}; |
push @{ $json->{rows} }, $rows->{$id}; |
201 |
} |
} |
202 |
|
|
245 |
|
|
246 |
sub database_get { |
sub database_get { |
247 |
my ($db_name) = @_; |
my ($db_name) = @_; |
248 |
|
my $path = $config->{path}; |
249 |
warn "# collecting docs from $path/$db_name/*\n"; |
warn "# collecting docs from $path/$db_name/*\n"; |
250 |
my @docs = glob "$path/$db_name/*"; |
my @docs = glob "$path/$db_name/*"; |
251 |
my $json = { |
my $json = { |