有没有办法将Mojolicious :: Controller与MojoX :: JSON :: RPC :: Service合并?

时间:2012-05-10 18:19:54

标签: perl mojolicious

我正在使用zabbix并编写一个界面来与zabbix api进行交互。由于zabbix公开了一个jsonrpc接口,我决定使用MojoX :: JSON :: RPC :: Service。我遇到的问题是我现在面临与使用Mojolicious :: Controllers编写的其他服务的交互,他们期望Mojolicious :: Controller对象。使用MojoX :: JSON :: RPC :: Service时,没有可用的Mojolicious :: Controller对象。

my $obj = $rpc_obj->register(
               'retrieve',
               sub {
                    # do stuff
               },
               { with_mojo_tx => 1 }
);

注册一个名为'retrieve'的路由。当访问路由和匿名时 子程序运行时,子程序只能访问Mojo :: Transaction :: HTTP对象。

因此,我无权使用该应用程序来使用插件以及Mojolicious提供的存储和其他内容。有没有办法将Mojolicious :: Controller与MojoX :: JSON :: RPC :: Service?

合并

我可以重写它以使用Mojolicious :: Controller,但如果可能的话,我试图避免这种情况。

1 个答案:

答案 0 :(得分:1)

您应该考虑使用MojoX::JSON::RPC::Dispatcher,因为它会继承Mojolicious::Controller的所有属性

<强>概要

# 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
        }
    );
}

[UPDATE] 钩子示例:

package Application;
use Mojo::Base 'Mojolicious';
use Application::Firewall;

# This method will run once at server start
sub startup {
    my $app = shift;

    # Routes
    my $r = $app->routes;

    # Validation Middleware
    $app->hook(
        before_dispatch => sub {
            my $self = shift;
            my $data = $self->req->params->to_hash;
            my $vald = Application::Firewall->new($data);

            # mojolicious bug at the time of coding
            delete $data->{""} if defined $data->{""};

            $app->{input} = {};

            if ( keys %{$data} ) {

                # validation the submitted data
                unless ( $vald->validate( keys %{$data} ) ) {
                    $self->render(
                        text   => join( "", @{ $vald->errors } ),
                        status => 500
                    );
                    return 0;
                }

                # Helper (optional)
                # create a helper to get access to the transformed data
                # if your validation rules had/has filters
                # Note! due to a bug in the params function we must do this
                # (... i know, so what)
                $app->{input} = {
                    map { $_ => $vald->{fields}->{$_}->{value} }
                      keys %{ $vald->{fields} }
                };

            }

            return 1;
        }
    );

    # Normal route to controller * with auto-matic input validation *
    $r->route('/')->to(
        cb => sub {
            my $self = shift;
            $self->render(
                text => 'Hello ' . ( $app->{input}->{foobar} || 'World' ),
                status => 200
            );
        }
    );
}

1;