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"; |
235 |
id => $id, |
id => $id, |
236 |
key => $id, |
key => $id, |
237 |
value => { |
value => { |
238 |
rev => (stat($p))[9], # mtime |
rev => file_rev $p, |
239 |
} |
} |
240 |
}; |
}; |
241 |
} |
} |
252 |
|
|
253 |
} elsif ( $method eq 'PUT' ) { |
} elsif ( $method eq 'PUT' ) { |
254 |
|
|
255 |
warn "## ",dump( $tx->req ) if $debug; |
warn "## ",dump( $tx->req ); # if $debug; |
256 |
|
|
257 |
my $data = $tx->req->content->file->slurp; |
my $data = $tx->req->content->file->slurp; |
258 |
|
|
259 |
Storable::store( from_json($data), $p ); |
Storable::store( from_json($data), $p ); |
260 |
warn "store $p ", -s $p, " bytes: $data\n"; |
my $rev = file_rev $p; |
261 |
|
warn "store $p $rev size ", -s $p, " bytes | $data\n"; |
262 |
|
|
263 |
$status = 201; # Created |
$status = 201; # Created |
264 |
|
$json = { |
265 |
|
id => $id, |
266 |
|
ok => JSON::true, |
267 |
|
rev => $rev, |
268 |
|
}; |
269 |
|
|
270 |
} elsif ( $method eq 'GET' ) { |
} elsif ( $method eq 'GET' ) { |
271 |
if ( ! -e $p ) { |
if ( ! -e $p ) { |
272 |
$status = 404; |
$status = 404; |
276 |
} |
} |
277 |
} elsif ( $method eq 'DELETE' ) { |
} elsif ( $method eq 'DELETE' ) { |
278 |
if ( -e $p ) { |
if ( -e $p ) { |
279 |
unlink $p || { $status = 500 }; |
unlink $p && ok || { $status = 500 }; |
280 |
} else { |
} else { |
281 |
$status = 404; |
$status = 404; |
282 |
} |
} |
301 |
$tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' ); |
$tx->res->headers->add_line( 'Cache-Control' => 'must-revalidate' ); |
302 |
$tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" ); |
$tx->res->headers->add_line( 'Server' => "Frey::CouchAPI/$VERSION" ); |
303 |
|
|
304 |
warn "INFO CouchDB API $method $url $status\n$body\n"; |
print "$method $url $status\n$body\n"; |
305 |
|
|
306 |
warn "## headers ", $tx->res->headers->to_string; |
warn "## headers ", $tx->res->headers->to_string; |
307 |
|
|