Mojolic...patcher(3)

πŸ“¦
version 0.13 Β· 2 versions
, in fedora 43

πŸ’– 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 dispatcher Mojolicious::Controller object
  • $err β€” the $@ received from the exception
  • $m β€” the MojoX::JSON::RPC::Dispatcher::Method object 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