0
votes

I'm exploring Yaws and I've read the Yaws manual and the Building Web Applications with Erlang book. I've followed every step in the book's last chapter where a multi_cast app is built, but I can't run it. I believe it's the Erlang code which isn't being found or used.

I've got a project dir in my home directory and I've pointed Yaws docroot for this particular Virtual Server to my project directory where a htdocs dir with the .yaws files reside.

<server localhost>                                                                                                                     
  port    = 8001
  listen  = 127.0.0.1
  docroot = <my_path_here>/erlang_yaws/multi_cast
</server>

and I've changed the ebin_dir to also point to the project´s ebin directory:

ebin_dir = <my_path_here>/erlang_yaws/multi_cast/_build/default/lib/multi_cast/ebin

Upon starting Yaws with yaws -i I can go to some simple .yaws files I've got which do not rely on any of the project´s .beam files. However, when I change my browser's location to a .yaws files which rely on the app's compiled files I get the following error:

=ERROR REPORT==== 21-Jan-2017::14:27:17 ===


ERROR erlang code threw an uncaught exception:
 File: <my_path_here>/erlang_yaws/multi_cast/htdocs/status.yaws:1
Class: exit
Exception: {noproc,{gen_server,call,[multi_cast_front,{get_etag}]}}
Req: {http_request,'GET',{abs_path,"/htdocs/status.yaws"},{1,1}}
Stack: [{gen_server,call,2,[{file,"gen_server.erl"},{line,204}]},
        {m_27449121_1,out,1,
                      [{file,"<my_path_here>/.yaws/yaws/default/m_27449121_1.erl"},
                       {line,35}]},
        {yaws_server,deliver_dyn_part,8,
                     [{file,"yaws_server.erl"},{line,2872}]},
        {yaws_server,aloop,4,[{file,"yaws_server.erl"},{line,1242}]},
        {yaws_server,acceptor0,2,[{file,"yaws_server.erl"},{line,1065}]},
        {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,247}]}]

I believe this is due to the fact that my gen_server app isn't starting correctly or not being found? The source code seems to compile successfully.

I'm on MacOS Sierra with Yaws 2.0.4, Erlang 19 and I did a regular Yaws install with homebrew.

Also, is there a resource which thoroughly explains how to structure Yaws and a simple application? Reading both the manual and the book and both fail at explaining this (or I'm really thick), and it's quite frustrating to get stuck from a user experience viewpoint.

1

1 Answers

0
votes

You've correctly added your modules to the load path using the ebin_dir configuration variable, but that's not enough to actually start and run your application. The book you're following doesn't address this — see page 101, where it says, "It is also possible to run Yaws as an OTP app inside an existing Erlang setup, but this is beyond the scope of this book."

An easy way to start your server is to use the runmod configuration variable, documented in the yaws_config man page:

runmod = ModuleName
    At startup Yaws will invoke ModuleName:start() in a separate process. It is possible
    to have several runmods. This is useful if we want to reuse the Yaws startup shell
    script for our own application.

Assuming your gen_server provides a start/0 function, set the name of your module as the value of runmod and Yaws will start it. A problem with this approach, though, is that it works fine for experimentation but not for production because there's nothing supervising your gen_server.

A better approach is to have a full OTP application running alongside Yaws. With this approach, your application's supervisors manage your workers. One way to do this is by using yapps (Yaws applications). Another way is to create a release containing both Yaws and your application. Still another way is to let Yaws supervise your workers.

Addressing the second part of your question requires defining what you mean by "a simple application". Is it a simple static page application? A simple application using .yaws pages? A simple SSE application? A simple appmod application? A simple websocket application? A simple application running as a sibling of Yaws? There are a variety of possible definitions. The best short answer is to always structure your application using OTP design principles. You might also consider sending questions on this topic to the Yaws mailing list.