1

在 webmachine 项目中,我还从其他服务器请求 https 页面。

在原型中,我设法以这种方式做到了:

to_html(ReqData, State) ->
    OtherResource = "https://example.com",
    inets:start(),
    ssl:start(),
    {ok, {{Version, 200, ReasonPhrase}, Headers, Body}} =
      httpc:request(get, {OtherResource, []}, [], []), 
    %% building the HTML response here...
    {HTML, ReqData, State}.

它作为原型工作,现在我想知道如何以及在哪里启动 inets 和 ssl 并让它们以正确的方式运行。

我已经看到在 src/myapp.erl 中也有启动 inets,但是这个 inets 实例在我上面的页面渲染中不可用:

start() ->
    ensure_started(inets), 
4

1 回答 1

2

您可以将 inets 和 ssl 应用程序作为标准启动脚本的一部分来启动(或者您正在使用的任何东西 - 因为您可能会为此使用 reltool)。此外,如果您在请求期间需要一些状态(来自 webmachine 的状态),您可以在 init/1 函数中启动任何您想要的状态(如果您想在请求结束时停止它,您可以在 finish_request/2 中调用任何停止过程 - “如果导出此函数,则在构造和发送最终响应之前调用此函数。忽略结果,因此此函数的任何效果必须通过返回修改后的 ReqData。”) :

这是 reltool.config 的一个片段:

{sys, [
       {lib_dirs, []},
       {erts, [{mod_cond, derived}, {app_file, strip}]},
       {app_file, strip},
       {rel, "myapp", "1",
        [
         kernel,
         stdlib,
         sasl,
         myapp
        ]},
       {rel, "start_clean", "",
        [
         kernel,
         stdlib
        ]},
       {boot_rel, "myapp"},
       {profile, embedded},
       {incl_cond, exclude},
       {excl_archive_filters, [".*"]}, %% Do not archive built libs
       {excl_sys_filters, ["^bin/.*", "^erts.*/bin/(dialyzer|typer)",
                           "^erts.*/(doc|info|include|lib|man|src)"]},
       {excl_app_filters, ["\.gitignore"]},
       {app, sasl,   [{incl_cond, include}]},
       {app, stdlib, [{incl_cond, include}]},
       {app, kernel, [{incl_cond, include}]},
       {app, mnesia, [{incl_cond, include}]},
       {app, inets, [{incl_cond, include}]}
      ]}.

您可以为 ssl 添加另一个条目,与 inets ({app, inets, [{incl_cond, include}]} ) 相同。通常,您可以使用 rebar 生成所需的所有骨架文件。

于 2012-07-11T18:09:19.220 回答