保持https客户端在erlang webmachine服务器应用程序中运行的好方法是什么?

时间:2012-07-11 13:16:08

标签: https erlang httpclient webmachine

在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), 

1 个答案:

答案 0 :(得分: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}]})。 通常,您可以使用钢筋生成所需的所有骨架文件。