60 |
sub ok { |
sub ok { |
61 |
$json = { ok => JSON::true }; |
$json = { ok => JSON::true }; |
62 |
$status = 200; |
$status = 200; |
63 |
warn "ok\n"; |
warn "ok from ",join(' ',caller),$/; |
64 |
} |
} |
65 |
|
|
66 |
|
sub file_rev { (stat($_[0]))[9] } # mtime |
67 |
|
|
68 |
sub dispatch { |
sub dispatch { |
69 |
my ($self,$tx) = @_; |
my ($self,$tx) = @_; |
73 |
my $url = $tx->req->url->to_string; |
my $url = $tx->req->url->to_string; |
74 |
$url = uri_unescape( $url ); |
$url = uri_unescape( $url ); |
75 |
my $method = $tx->req->method; |
my $method = $tx->req->method; |
76 |
|
my $path = $config->{path}; |
77 |
|
|
78 |
if ( $url eq '/' ) { |
if ( $url eq '/' ) { |
79 |
$json = { |
$json = { |
127 |
=cut |
=cut |
128 |
|
|
129 |
my $database = $1; |
my $database = $1; |
130 |
my $dir = "$config->{path}/$database"; |
|
131 |
|
my $dir = "$path/$database"; |
132 |
|
|
133 |
if ( $method eq 'GET' ) { |
if ( $method eq 'GET' ) { |
134 |
$json = database_get( $database ); |
$json = database_get( $database ); |
136 |
if ( ! -e $dir ) { |
if ( ! -e $dir ) { |
137 |
$status = 404; |
$status = 404; |
138 |
} else { |
} else { |
139 |
remove_tree($dir) && ok || { $status = 500 }; |
remove_tree($dir); |
140 |
|
if ( ! -d $dir ) { |
141 |
|
ok; |
142 |
|
} else { |
143 |
|
$status = 500; |
144 |
|
} |
145 |
} |
} |
146 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
147 |
if ( ! -e $dir ) { |
if ( -e $dir ) { |
|
make_path($dir) && ok && warn "created $dir" || { $status = 500 }; |
|
|
} else { |
|
148 |
$status = 412; |
$status = 412; |
149 |
|
} else { |
150 |
|
make_path($dir); |
151 |
|
if ( -e $path ) { |
152 |
|
ok; |
153 |
|
$status = 201; |
154 |
|
} else { |
155 |
|
$status = 500; |
156 |
|
} |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
175 |
} |
} |
176 |
} |
} |
177 |
|
|
|
my $path = $config->{path}; |
|
178 |
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
warn "ERROR: path $path doesn't exist\n" unless -e $path; |
179 |
|
|
180 |
my $p = "$path/$database/$id"; |
my $p = "$path/$database/$id"; |
181 |
warn "## database: $database id: $id -> $p ", dump( $arg ),"\n"; |
warn "## database: $database id: $id -> $p ", dump( $arg ),"\n"; |
182 |
|
|
183 |
|
|
184 |
if ( $id =~ m{_all_docs(\w+)?$} ) { |
if ( $id =~ m{_all_docs(\w*)$} ) { |
185 |
|
|
186 |
my $by = $1; |
my $by = $1; |
187 |
my $offset = 0; |
my $offset = 0; |
191 |
my $total_rows = 0; |
my $total_rows = 0; |
192 |
|
|
193 |
my @docs = grep { length $_ } map { |
my @docs = grep { length $_ } map { |
194 |
return '' if defined $limit && $total_rows == $limit; |
|
195 |
|
if ( $limit > 0 && $total_rows == $limit ) { |
196 |
|
''; |
197 |
|
} else { |
198 |
|
|
199 |
s{^$path/$database/}{}; |
s{^$path/$database/}{}; |
|
return '' if defined $endkey && $_ gt $endkey; |
|
200 |
|
|
201 |
if ( $startkey ) { |
if ( defined $endkey && $_ gt $endkey ) { |
202 |
if ( $_ ge $startkey ) { |
''; |
203 |
|
} elsif ( $startkey ) { |
204 |
|
if ( $_ ge $startkey ) { |
205 |
|
$total_rows++; |
206 |
|
$_; |
207 |
|
} else { |
208 |
|
$offset++; |
209 |
|
''; |
210 |
|
} |
211 |
|
} else { |
212 |
$total_rows++; |
$total_rows++; |
213 |
$_; |
$_; |
|
} else { |
|
|
$offset++; |
|
|
return ''; |
|
214 |
} |
} |
|
} else { |
|
|
$total_rows++; |
|
|
$_; |
|
215 |
} |
} |
216 |
|
|
217 |
} glob( "$path/$database/*" ); |
} glob( "$path/$database/*" ); |
218 |
|
|
219 |
|
|
220 |
warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug; |
warn "## docs $startkey -> $endkey limit $limit ", dump( @docs ); # if $debug; |
221 |
|
|
222 |
$json = { |
$json = { |
241 |
id => $id, |
id => $id, |
242 |
key => $id, |
key => $id, |
243 |
value => { |
value => { |
244 |
rev => (stat($p))[9], # mtime |
rev => file_rev $p, |
245 |
} |
} |
246 |
}; |
}; |
247 |
} |
} |
256 |
push @{ $json->{rows} }, $rows->{$id}; |
push @{ $json->{rows} }, $rows->{$id}; |
257 |
} |
} |
258 |
|
|
259 |
|
$status = 200; |
260 |
|
|
261 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
262 |
|
|
263 |
warn "## ",dump( $tx->req ) if $debug; |
warn "## ",dump( $tx->req ); # if $debug; |
264 |
|
|
265 |
my $data = $tx->req->content->file->slurp; |
my $data = $tx->req->content->file->slurp; |
266 |
|
|
267 |
Storable::store( from_json($data), $p ); |
Storable::store( from_json($data), $p ); |
268 |
warn "store $p ", -s $p, " bytes: $data\n"; |
my $rev = file_rev $p; |
269 |
|
warn "store $p $rev size ", -s $p, " bytes | $data\n"; |
270 |
|
|
271 |
$status = 201; # Created |
$status = 201; # Created |
272 |
|
$json = { |
273 |
|
id => $id, |
274 |
|
ok => JSON::true, |
275 |
|
rev => $rev, |
276 |
|
}; |
277 |
|
|
278 |
} elsif ( $method eq 'GET' ) { |
} elsif ( $method eq 'GET' ) { |
279 |
if ( ! -e $p ) { |
if ( ! -e $p ) { |
280 |
$status = 404; |
$status = 404; |
281 |
} else { |
} else { |
282 |
warn "retrive $p ", -s $p, " bytes\n"; |
warn "retrive $p ", -s $p, " bytes\n"; |
283 |
$json = Storable::retrieve( $p ); |
$json = Storable::retrieve( $p ); |
284 |
|
if ( delete $arg->{revs_info} ) { |
285 |
|
my $rev = file_rev $p; |
286 |
|
$json->{_rev} = $rev; |
287 |
|
$json->{_revs_info} = [ |
288 |
|
{ rev => $rev, status => 'available' } |
289 |
|
]; |
290 |
|
} |
291 |
|
$status = 200; |
292 |
|
|
293 |
} |
} |
294 |
} elsif ( $method eq 'DELETE' ) { |
} elsif ( $method eq 'DELETE' ) { |
295 |
if ( -e $p ) { |
if ( -e $p ) { |
296 |
unlink $p || { $status = 500 }; |
unlink $p && ok || { $status = 500 }; |
297 |
} else { |
} else { |
298 |
$status = 404; |
$status = 404; |
299 |
} |
} |
300 |
|
} elsif ( $method eq 'POST' ) { |
301 |
|
$json = { total_rows => 0, offset => 0 }; |
302 |
|
$status = 202; # FIXME implement real view server and return 200 |
303 |
} else { |
} else { |
304 |
$status = 501; |
$status = 501; |
305 |
} |
} |
321 |
$tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' ); |
$tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' ); |
322 |
$tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" ); |
$tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" ); |
323 |
|
|
324 |
warn "INFO CouchDB API $method $url $status\n$body\n"; |
print "$method $url $status\n$body\n"; |
325 |
|
|
326 |
warn "## headers ", $tx->res->headers->to_string; |
warn "## headers ", $tx->res->headers->to_string; |
327 |
|
|