Search code examples
httpserlangmochiwebwebmachine

Webmachine with http and https?


What is the recommended way of getting https working with webmachine?

I see that there is an example for getting mochiweb working with https and http. I just can seem to translate that to webmachine. In particular how do you handle both http and https requests in one app.


Solution

  • i had some success getting multiple listeners with the following change to mywebdemo_sup.erl in the demo app. i haven't tested it much further than that, but hopefully enough to get you started.

    init([]) ->
        Ip = case os:getenv("WEBMACHINE_IP") of false -> "0.0.0.0"; Any -> Any end,
        {ok, Dispatch} = file:consult(filename:join(
                        [filename:dirname(code:which(?MODULE)),
                         "..", "priv", "dispatch.conf"])),
        WebConfig = [
             {name, one},
             {ip, Ip},
             {port, 8000},
             {log_dir, "priv/log"},
             {dispatch, Dispatch}],
        Web = {one,
           {webmachine_mochiweb, start, [WebConfig]},
           permanent, 5000, worker, dynamic},
        WebSSLConfig = [
                {name, two},
                {ip, Ip},
                {port, 8443},
                {ssl, true},
                {ssl_opts, [{certfile, "/tmp/api_server.crt"},
                    {cacertfile,"tmp/api_server.ca.crt"},
                    {keyfile, "/tmp/api_server.key"}]},
                {log_dir, "priv/log"},
                {dispatch, Dispatch}],
        WebSSL = {two,
              {webmachine_mochiweb, start, [WebSSLConfig]},
              permanent, 5000, worker, dynamic},
        Processes = [Web, WebSSL],
        {ok, { {one_for_one, 10, 10}, Processes} }.