应该在哪里定义“ Cro Endpoint HTTP”?

时间:2018-07-07 21:00:30

标签: perl6 cro

控制台在启动cro('cro run')之后显示此消息:

▶ Starting JoanPujol (JoanPujol)
 **Endpoint HTTP will be at http://localhost:20000/**
 JoanPujol Listening at http://localhost:3000
 JoanPujol Shutting down...
♻ Restarting JoanPujol (JoanPujol)
 JoanPujol Listening at http://localhost:3000

我无法弄清楚定义了“端点HTTP”的位置。这是我的“ service.p6 ”:

use Cro::HTTP::Log::File;
use Cro::HTTP::Server;
use Routes;

constant HOST = 'localhost';
constant PORT = 3000;

my Cro::Service $http = Cro::HTTP::Server.new(
    :http<1.1>,
    host => HOST, 
    port => PORT,
    application => routes(),
    after => [
      Cro::HTTP::Log::File.new(logs => $*OUT, errors => %*ERR)
    ]
);
$http.start;
say "Listening at http://{HOST}:{PORT}";
react {
    whenever signal(SIGINT) {
        say "Shutting down...";
        $http.stop;
        done;
    }
}

谢谢!

1 个答案:

答案 0 :(得分:6)

cro开发工具会自动为每个要求运行的服务中的每个端点选择一个空闲端口。当有多个服务并且在它们之间指定了链接时,它还会通过环境变量将所选端口注入到从属服务。这样就可以设置一堆服务,这些服务可以通过环境变量配置其端口,然后cro run为每个服务分配一个端口并传播该信息,以便这些服务可以相互通信

该消息指示自动选择了哪个端口号。但是,由于服务脚本无论如何都将覆盖它们,所以它没有任何作用。要禁用此功能并清除消息,remove the endpoint list entry in the .cro.yml file

如果希望将端口硬编码到服务脚本中,那么使用constant而不是使用%*ENV声明它们可能会更容易。