gpt4 book ai didi

erlang - 如何使用 rebar3 配置 httpc 配置文件?

转载 作者:行者123 更新时间:2023-12-01 11:22:50 25 4
gpt4 key购买 nike

如何为 httpc 设置配置选项使用 rebar3 时的配置文件?

这是唯一的example通过 erl -config inets.config 看起来像这样:

[{inets, 
[{services,[{httpc,[{profile, server1}]},
{httpc, [{profile, server2}]}]}]
}].

我尝试将它应用到我的 rebar3 项目结构中。

代码

项目是使用 rebar3 创建的,具有标准的 OTP 布局:

rebar3 new release myapp

这是我的myapp/config/sys.config:

[
{ myapp, []},
{inets, [{services, [{httpc, [{profile, myapp}]}]}]}
].

rebar.config:

{erl_opts, [debug_info]}.
{deps, []}.

{relx, [{release, { myapp, "0.1.0" },
[myapp,
sasl]},

{sys_config, "./config/sys.config"},
{vm_args, "./config/vm.args"},

{dev_mode, true},
{include_erts, false},

{extended_start_script, true}]
}.

{profiles, [{prod, [{relx, [{dev_mode, false},
{include_erts, true}]}]
}]
}.

为了完整性,这是我的 myapp.app.src 文件:

{application, myapp,
[{description, "An OTP application"},
{vsn, "0.1.0"},
{registered, []},
{mod, { myapp_app, []}},
{applications,
[kernel,
stdlib
]},
{env,[]},
{modules, []},

{maintainers, []},
{licenses, []},
{links, []}
]}.

请求

这是我试图从 rebar 的外壳发出的请求:

$ ./rebar3 shell
1> ===> Booted myapp
1> ===> Booted sasl
...
1> httpc:request( "http://reddit.com", myapp).
** exception exit: {noproc,
{gen_server,call,
[httpc_myapp,
{request,
{request,undefined,<0.88.0>,0,http,
{"reddit.com",80},
"/",[],get,
{http_request_h,undefined,"keep-alive",undefined,
undefined,undefined,undefined,undefined,undefined,
undefined,...},
{[],[]},
{http_options,"HTTP/1.1",infinity,true,
{essl,[]},
undefined,false,infinity,...},
"http://reddit.com",[],none,[],1478280329839,
undefined,undefined,false}},
infinity]}}
in function gen_server:call/3 (gen_server.erl, line 212)
in call from httpc:handle_request/9 (httpc.erl, line 574)

这是没有配置文件的请求,用于检查 inets 是否确实有效:

2> httpc:request( "http://reddit.com").

=PROGRESS REPORT==== 4-Nov-2016::13:25:51 ===
supervisor: {local,inet_gethost_native_sup}
started: [{pid,<0.107.0>},{mfa,{inet_gethost_native,init,[[]]}}]

=PROGRESS REPORT==== 4-Nov-2016::13:25:51 ===
supervisor: {local,kernel_safe_sup}
started: [{pid,<0.106.0>},
{id,inet_gethost_native_sup},
{mfargs,{inet_gethost_native,start_link,[]}},
{restart_type,temporary},
{shutdown,1000},
{child_type,worker}]
{ok,{{"HTTP/1.1",200,"OK"},...

最佳答案

rebar3 本身使用 inets http 客户端,因此当它在 shell 中启动您的应用程序时,inets 已经启动并配置。一种解决方法是在您的应用程序启动之前停止 inets,as It's suggested by rebar3 developer (复制如下)。另一个是在控制台模式下启动您的版本:

./_build/default/rel/myapp/bin/myapp console

除此之外,您的项目还有另一个问题。您没有告诉您要为您启动 inets。 myapp.src 中应该有这一行:

{applications, [kernel, stdlib, inets]}

或者你可以在 rebar.config 发布部分列出 inets,告诉 relx 这个应用应该包含在发布中并在启动时启动。

{relx, [{release, { myapp, "0.1.0" }, [inets, myapp, sasl]} ]}

阻止 Inets 在 rebar3 shell 启动时加载

这是 full answer 的副本来自 rebar3 邮件列表的 Fred Hebert:

We do need inets for package fetching and will likely not turn it off automatically for all use cases as this could compromise general usage of the rebar3 agent in case where the user application does not use inets, but still asks to fetch packages in a subsequent run. The workaround I would suggest would be to use a hook script for it. Hook scripts run before we boot user applications and are regular escripts:

#!/usr/bin/env escript 
main(_) -> application:stop(inets).

You can then hook the script in with:

{shell, [{script_file, "path/to/file"}]} 

in rebar3.config, or with

rebar3 shell --script_file test/check_env.escript

关于erlang - 如何使用 rebar3 配置 httpc 配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40291335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com