如何在cro中的两个路由器模块之间共享变量?

STH*_*STH 6 perl6 cro

我尝试使用Cro创建一个Rest API,它将在rabbitMQ中发布消息.我想在不同的模块中拆分我的路线,并用"包含"组成它们.但我希望能够在每个模块中与rabbitMQ共享相同的连接.我尝试"我们的",但它不起作用:

档案1:

unit module XXX::YYY;
use Cro::HTTP::Router;
use Cro::HTTP::Server;
use Cro::HTTP::Log::File;
use XXX::YYY::Route1;

use Net::AMQP;

our $rabbitConnection is export = Net::AMQP.new;
await $rabbitConnection.connect;

my $application = route {
        include <api v1 run> => run-routes;
}
...
Run Code Online (Sandbox Code Playgroud)

文件2:

unit module XXX::YYY::Route1;
use UUID;
use Cro::HTTP::Router;
use JSON::Fast;
use Net::AMQP;
my $channel = $XXX::YYY::rabbitConnection.open-channel().result;
$channel.declare-queue("test_task", durable=> True );
sub run-routes() is export { ... }
Run Code Online (Sandbox Code Playgroud)

错误信息:

===SORRY!===
No such method 'open-channel' for invocant of type 'Any'
Run Code Online (Sandbox Code Playgroud)

谢谢!

Sci*_*mon 4

When you define your exportable route function you can specify arguments then in your composing module you can create the shared objects and pass them to the routes. For example in your router module :

sub run-routes ($rmq) is export{
    route {
       ... $rmq is available in here
    }
}
Run Code Online (Sandbox Code Playgroud)

Then in your main router you can create your Queue and pass it in when including

my $rmq = # Insert queue creation code here
include product => run-routes( $rmq );
Run Code Online (Sandbox Code Playgroud)

I've not tried this but I can't see any reason why it shouldn't work.