Mojolic...patcher(3)
π Mojolic...patcher(3) π
okay bestie, gather round, because this is the User Contributed Perl Documentation for Mojolic...patcher β yes that's the manual name, it's giving mysterious, it's giving truncated, we love her anyway π
β¨ NAME β¨
Mojolicious::Plugin::JsonRpcDispatcher β a plugin that lets your Mojolicious app clock in and be a JSON-RPC server. no cap, that's the whole job description π
πΈ SYNOPSIS πΈ
so here's the vibe in a full app, bestie:
# lib/your-application.pm
use base 'Mojolicious';
use MojoX::JSON::RPC::Service;
sub startup {
my $self = shift;
my $svc = MojoX::JSON::RPC::Service->new;
$svc->register(
'sum',
sub {
my @params = @_;
my $sum = 0;
$sum += $_ for @params;
return $sum;
}
);
$self->plugin(
'json_rpc_dispatcher',
services => {
'/jsonrpc' => $svc
}
);
}
orrr if you're living that lite-app lifestyle (short, sweet, iconic):
use Mojolicious::Lite;
use MojoX::JSON::RPC::Service;
plugin 'json_rpc_dispatcher' => {
services => {
'/jsonrpc' => MojoX::JSON::RPC::Service->new->register(
'sum',
sub {
my @params = @_;
my $sum = 0;
$sum += $_ for @params;
return $sum;
}
)
}
};
π¦ DESCRIPTION π¦
okay so the way that this plugin turns your Mojolicious or Mojolicious::Lite application into a JSON-RPC 2.0 server?? literally main character energy. periodt. π
and it understands these parameters, bestie β listen up:
π services (mandatory)
a pointer to a hash of service instances. if you wanna know how to actually write a service, go read MojoX::JSON::RPC::Service, she explains everything.
not optional. mandatory. like, you can't skip this one, that's the rule π
$self->plugin(
'json_rpc_dispatcher',
services => {
'/jsonrpc' => $svc,
'/jsonrpc2' => $svc2,
'/jsonrpc3' => $svc3,
'/jsonrpc4' => $svc4
}
);
see how we just stacked four of them?? one hash, four services, we ate π₯
π exception_handler (optional)
a reference to a method that gets called when an uncatched exception happens inside an rpc call. and here's the tea: if you don't specify exception_handler, then an internal error gets returned as the result of the rpc call. so like, either you handle it or the internal error handles it for you. your call, bestie π
$self->plugin(
'json_rpc_dispatcher',
services => {
'/jsonrpc' => $svc,
},
exception_handler => sub {
my ( $dispatcher, $err, $m ) = @_;
# $dispatcher is the dispatcher Mojolicious::Controller object
# $err is $@ received from the exception
# $m is the MojoX::JSON::RPC::Dispatcher::Method object to be returned.
$dispatcher->app->log->error(qq{Internal error: $err});
# Fake invalid request
$m->invalid_request('Faking invalid request');
return;
}
);
so what's in those args, you ask?? lemme spell it out π«Ά
$dispatcherβ the dispatcherMojolicious::Controllerobject$errβ the$@received from the exception$mβ theMojoX::JSON::RPC::Dispatcher::Methodobject to be returned
and then the example goes and does a little "fake invalid request" move with $m->invalid_request('Faking invalid request') and returns. iconic behaviour honestly π
β SEE ALSO β
MojoX::JSON::RPC β go read her, she's the whole foundation π
and that's the page bestie!! slay β¨
this whole thing came to you courtesy of perl v5.42.0, dated 2025-07-25, and it lives in the Mojolic...patcher(3) section of the User Contributed Perl Documentation. mwah, go build something cute with it ππ¦πΈ
π¦ where this page comes from
- fedora 43: perl-MojoX-JSON-RPC 0.13-14.fc43 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms