1

In a webmachine project I am also requesting https pages from other servers.

In the prototype I managed to do it in this way:

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}.

It works as a prototype and now I wonder how and where to start the inets and ssl and keep them running in a proper way.

I have seen that there is also started inets in src/myapp.erl, but this inets instance was not available in my page rendering above:

start() ->
    ensure_started(inets), 
SHernandez
  • 1,060
  • 1
  • 14
  • 21

1 Answers1

2

You could start the inets and ssl applications as part of the standard startup script (or whatever you're using - as you might use reltool for that). Also, if you need some state for the while of the request (the one from webmachine), you could start whatever you want as part of the init/1 function (and if you want to stop it at the end of the request, you can call whatever stop procedure within finish_request/2 - "This function, if exported, is called just before the final response is constructed and sent. The Result is ignored, so any effect of this function must be by returning a modified ReqData."):

Here is a snippet from 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}]}
      ]}.

You could add another entry for ssl, the same as inets ({app, inets, [{incl_cond, include}]} ). Usually, you can generate all the skeleton files that you need, by using rebar.

Alin
  • 818
  • 5
  • 11
  • I will go and find out about reltool. But I have already a stack here that is not very easy to understand for me as an erlang newbie, which is a webmachine app that was created by the webmachine wizard. I am hesitating learn and mix in new tools like "reltool" and I would rather try to stick to to the existing structure. I would accept your answer soon as it is the only one and I trust it would work and I will understand it later at some point. – SHernandez Jul 18 '12 at 09:09
  • Ok, so in this case, you can start all the modules that you need, manually. But, I'll go with application:start(my_app). So, your applications can be started like this: application:start(inets), application:start(crypto), application:start(public_key), application:start(ssl). Yeah, you need to start crypto and public_key as they're dependencies of ssl. HTH, Alin. – Alin Jul 18 '12 at 18:04