Created
October 2, 2012 21:55
-
-
Save leedo/3823566 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use AnyEvent; | |
use AnyEvent::Redis; | |
use Plack::Request; | |
use v5.12; | |
my $redis = AnyEvent::Redis->new; | |
sub { | |
my $req = Plack::Request->new(shift); | |
my $params = $req->parameters; | |
my ($key) = $req->path =~ /^\/(.+)/; | |
if (!defined $key) { | |
return [404, [], ["not found"]]; | |
} | |
return sub { | |
my $res = shift; | |
given ($req->method) { | |
when ("GET") { | |
$redis->get($key, sub { | |
my $value = shift; | |
if ($value) { | |
$res->([200, [], [$value]]); | |
} | |
else { | |
$res->([404, [], ["not found"]]); | |
} | |
}); | |
} | |
when ("PUT") { | |
my $value = $params->{value}; | |
$redis->set($key, $value, sub { | |
$res->([200, [], [$value]]); | |
}); | |
} | |
default { | |
$res->([405, [], ["method not allowed"]]); | |
} | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment