MojoX::JSON::RPC::Service - JSON RPC Service registration
use MojoX::JSON::RPC::Service; my $svc = MojoX::JSON::RPC::Service->new; $svc->register( 'sum', sub { my @params = @_; my $sum = 0; $sum += $_ for @params; return $sum; } ); $svc->register( 'remote_address', sub { my $tx = shift; return $tx->remote_address; }, { with_mojo_tx => 1 } ); ## Then in Mojolicious application $self->plugin( 'json_rpc_dispatcher', services => { '/jsonrpc' => $svc, } );
This package can also be used as a base class to make it easy to create object-oriented JSON-RPC applications:
package MyService; use Mojo::Base 'MojoX::JSON::RPC::Service'; sub sum { my ($self, @params) = @_; my $sum = 0; $sum += $_ for @params; return $sum; } __PACKAGE__->register_rpc_method_names( 'sum' ); ## Then in Mojolicious application $self->plugin( 'json_rpc_dispatcher', services => { '/jsonrpc' => MyService->new, } );
Register JSON-RPC service calls.
MojoX::JSON::RPC::Service inherits all methods from Mojo::Base and implements the following new ones.
register
Register RPC methods.
$svc->register( 'sum', sub { my @params = @_; my $sum = 0; $sum += $_ for @params; return $sum; } );
with_mojo_tx can be passed as options. In that case, Mojo::Transaction object will be pass as first argument of the subroutine.
$svc->register( 'remote_address', sub { my $tx = shift; return $tx->remote_address; }, { with_mojo_tx => 1 } );
register_rpc_method_names
Class method. Register a list of methods as JSON-RPC calls.
__PACKAGE__->register_rpc_method_names( 'sum', 'multiply' );
MojoX::JSON::RPC::Dispatcher
To install MojoX::JSON::RPC, copy and paste the appropriate command in to your terminal.
cpanm
cpanm MojoX::JSON::RPC
CPAN shell
perl -MCPAN -e shell install MojoX::JSON::RPC
For more information on module installation, please visit the detailed CPAN module installation guide.