AnyEvent-JSONRPC
view release on metacpan or search on metacpan
lib/AnyEvent/JSONRPC/HTTP/Server.pm view on Meta::CPAN
package AnyEvent::JSONRPC::HTTP::Server;
use Moose;
extends 'AnyEvent::JSONRPC::Server';
use Carp;
use Scalar::Util 'weaken';
use AnyEvent::JSONRPC::CondVar;
use AnyEvent::HTTPD;
use JSON::XS;
use JSON::RPC::Common::Procedure::Call;
has host => (
is => 'ro',
isa => 'Str',
default => '127.0.0.1',
);
has port => (
is => 'ro',
isa => 'Int|Str',
default => 8080,
);
has httpd => (
is => 'rw',
isa => 'AnyEvent::HTTPD',
predicate => 'has_httpd',
);
has methods => (
isa => 'HashRef[CodeRef]',
lazy => 1,
traits => ['Hash'],
handles => {
reg_cb => 'set',
method => 'get',
},
default => sub { {} },
);
no Moose;
sub BUILD {
my $self = shift;
unless ( $self->has_httpd ) {
$self->httpd( AnyEvent::HTTPD->new( host => $self->host, port => $self->port ) );
}
$self->httpd->reg_cb(
request => sub {
my ($httpd, $req) = @_;
my $request = eval { $self->json->decode( $req->content ) };
unless (defined $request ) {
$req->respond( [ 400, 'Bad Request' ] );
warn "Bad content: [[[" . $req->content . "]]]" ;
$httpd->stop_request;
}
my $response = $self->_dispatch( $request );
if ($response) {
$req->respond( [ 200, 'Ok', { "Content-Type" => "application/json" }, $self->json->encode( $response ) ] );
} else {
$req->respond( [ 204, 'No Content' ] );
}
( run in 1.001 second using v1.01-cache-2.11-cpan-39bf76dae61 )