Catalyst-Plugin-JSONRPC-Server

 view release on metacpan or  search on metacpan

examples/README.md  view on Meta::CPAN

# JSON-RPC server example

A minimal Catalyst app that mounts a JSON-RPC 2.0 endpoint at `/rpc` and registers
two methods: `echo` (returns its params) and `sum` (adds an array of numbers).

## Run it

```
plackup -p 5000 examples/app.psgi
perl examples/client.pl            # or: perl examples/client.pl http://127.0.0.1:5000
```

## Expected output

examples/lib/Example/JSONRPC/Controller/Root.pm  view on Meta::CPAN

package Example::JSONRPC::Controller::Root;
use v5.36;
use Moose;
use namespace::autoclean;

BEGIN { extends 'Catalyst::Controller' }

__PACKAGE__->config( namespace => '' );

# A JSON-RPC endpoint. Handlers are registered per request (cheap here); a real
# app would register them once at setup. Each handler gets the request's params
# and returns the result; an unregistered method yields a -32601 error.
sub rpc :Path('/rpc') :Args(0) {
    my ( $self, $c ) = @_;
    $c->jsonrpc_register( echo => sub ($p) { $p } );
    $c->jsonrpc_register( sum  => sub ($p) { my $t = 0; $t += $_ for @$p; $t } );
    $c->jsonrpc_dispatch;
}

__PACKAGE__->meta->make_immutable;



( run in 2.185 seconds using v1.01-cache-2.11-cpan-036bef1c656 )