1Mojolicious::Plugin::JsUosneRrpcCDoinstpraitbcuhteerdM(o3Pj)eorllicDioocuusm:e:nPtlautgiionn::JsonRpcDispatcher(3)
2
3
4
6 Mojolicious::Plugin::JsonRpcDispatcher - Plugin to allow Mojolicious to
7 act as a JSON-RPC server.
8
10 # lib/your-application.pm
11
12 use base 'Mojolicious';
13 use MojoX::JSON::RPC::Service;
14
15 sub startup {
16 my $self = shift;
17 my $svc = MojoX::JSON::RPC::Service->new;
18
19 $svc->register(
20 'sum',
21 sub {
22 my @params = @_;
23 my $sum = 0;
24 $sum += $_ for @params;
25 return $sum;
26 }
27 );
28
29 $self->plugin(
30 'json_rpc_dispatcher',
31 services => {
32 '/jsonrpc' => $svc
33 }
34 );
35 }
36
37 Or in lite-app:
38
39 use Mojolicious::Lite;
40 use MojoX::JSON::RPC::Service;
41
42 plugin 'json_rpc_dispatcher' => {
43 services => {
44 '/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
45 'sum',
46 sub {
47 my @params = @_;
48 my $sum = 0;
49 $sum += $_ for @params;
50 return $sum;
51 }
52 )
53 }
54 };
55
57 This plugin turns your Mojolicious or Mojolicious::Lite application
58 into a JSON-RPC 2.0 server.
59
60 The plugin understands the following parameters.
61
62 services (mandatory)
63 A pointer to a hash of service instances. See
64 MojoX::JSON::RPC::Service for details on how to write a service.
65
66 $self->plugin(
67 'json_rpc_dispatcher',
68 services => {
69 '/jsonrpc' => $svc,
70 '/jsonrpc2' => $svc2,
71 '/jsonrpc3' => $svc3,
72 '/jsonrpc4' => $svc4
73 }
74 );
75
76 exception_handler (optional)
77 Reference to a method that is called when an uncatched exception
78 occurs within a rpc call. If exception_handler is not specified
79 then internal error is returned as result of the rpc call.
80
81 $self->plugin(
82 'json_rpc_dispatcher',
83 services => {
84 '/jsonrpc' => $svc,
85 },
86 exception_handler => sub {
87 my ( $dispatcher, $err, $m ) = @_;
88
89 # $dispatcher is the dispatcher Mojolicious::Controller object
90 # $err is $@ received from the exception
91 # $m is the MojoX::JSON::RPC::Dispatcher::Method object to be returned.
92
93 $dispatcher->app->log->error(qq{Internal error: $err});
94
95 # Fake invalid request
96 $m->invalid_request('Faking invalid request');
97 return;
98 }
99 );
100
102 MojoX::JSON::RPC
103
104
105
106perl v5.32.0 2020M-o0j7o-l2i8cious::Plugin::JsonRpcDispatcher(3)