I am only using Yaws to serve dynamic content via handlers (for example, serving API responses or dynamic pages via Erlang modules). I don't need a document root at all.
Here is supervisor where I am initializing Yawsusing
yaws_api:embedded_start_conf/1`.
I even tried to add the gconfig
manually again with yaws_api:setconf(GlobalConf, [[ServerConf]])
but global config are not set.
%%%-------------------------------------------------------------------
%% @doc myapp top level supervisor.
%% @end
%%%-------------------------------------------------------------------
-module(myapp_sup).
-behaviour(supervisor).
-export([start_link/0, init/1]).
-include_lib("yaws/include/yaws_api.hrl").
-include_lib("yaws/include/yaws.hrl").
-define(SERVER, ?MODULE).
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
init([]) ->
%% Start Yaws explicitly
Yaws_conf = [
{listen, {127, 0, 0, 1}},
{servername, "Myapp Backend"},
{docroot, none},
{ssl, false},
{port, 8080},
{dynamic_handlers, [{"/api", myapp_yaws_handler}]},
{max_connections, 1024},
{ebin_dir, "/mnt/c/Users/PankajMali/Project/myapp/_build/default/lib/myapp/ebin"},
{logdir, "/mnt/c/Users/PankajMali/Project/myapp/yawslogs"},
{keepalive_timeout, 60000}
],
case yaws_api:embedded_start_conf(Yaws_conf) of
{ok, GlobalConf, ServerConf, YawsChild} ->
io:format("Yaws started successfully with GlobalConf: ~p and ServerConf: ~p~n", [
GlobalConf, ServerConf
]),
%% Define global configuration explicitly
GlobalConfList =
[
{ebin_dir,
"/mnt/c/Users/PankajMali/Project/myapp/_build/default/lib/myapp/ebin"},
{logdir, "/mnt/c/Users/PankajMali/Project/myapp/yawslogs"},
{dynamic_handlers, [{"/api", myapp_yaws_handler}]}
],
%% Set Yaws configuration
yaws_api:setconf(GlobalConfList , [[ServerConf]]),
io:format("Yaws configuration set successfully~n"),
%% Define the myapp_app child specification
MyAppChild = #{
id => my_app,
start => {my_app, start_link, []},
restart => permanent,
shutdown => 5000,
type => worker,
modules => [my_app]
},
%% Combine Yaws child specifications with my_app child specification
ChildSpecs = YawsChild ++ [MyAppChild],
io:format("Child specifications: ~p~n", [ChildSpecs]),
{ok, {{one_for_one, 5, 10}, ChildSpecs}};
Reason ->
io:format("Failed to start Yaws: ~p~n", [Reason]),
{stop, Reason}
end.
terminal
Yaws started successfully with GlobalConf: [[]] and ServerConf: {gconf,
"/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/yaws",
false,100,
".",
["/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/yaws/examples/ebin"],
[],[],30000,
nolimit,400,
1000000,8000,
nolimit,[],
10240,[],
1000000,30,
["/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/yaws/examples/include"],
"/usr/bin/php-cgi",
"Yaws 2.2.0",
"default",
false,[],8,
undefined,
[inet],
yaws_session_server,
undefined,
120000,
3600000,
disable}
Before setting Yaws configuration
=CRASH REPORT==== 21-Mar-2025::10:22:42.198891 ===
crasher:
initial call: application_master:init/4
pid: <0.527.0>
registered_name: []
exception exit: {bad_return,
{{workchat_app,start,[normal,[]]},
{'EXIT',
{{badmatch,
{error,
{noproc,
{gen_server,call,
[yaws_server,check_certs,infinity]}}}},
[{workchat_app,start,2,
[{file,
"/mnt/c/Users/PankajMali/Project/workchat/src/workchat_app.erl"},
{line,37}]},
{application_master,start_it_old,4,
[{file,"application_master.erl"},{line,293}]}]}}}}
in function application_master:init/4 (application_master.erl, line 142)
ancestors: [<0.526.0>]
message_queue_len: 1
messages: [{'EXIT',<0.528.0>,normal}]
links: [<0.526.0>,<0.44.0>]
dictionary: []
trap_exit: true
status: running
heap_size: 987
stack_size: 28
reductions: 250
neighbours:
=CRASH REPORT==== 21-Mar-2025::10:22:42.198956 ===
crasher:
initial call: supervisor:workchat_sup/1
pid: <0.535.0>
registered_name: []
exception exit: {noproc,
{gen_server,call,[yaws_server,check_certs,infinity]}}
in function gen_server:call/3 (gen_server.erl, line 385)
in call from yaws_api:setconf/3 (/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/yaws/src/yaws_api.erl, line 2690)
in call from workchat_sup:init/1 (/mnt/c/Users/PankajMali/Project/workchat/src/workchat_sup.erl, line 52)
in call from supervisor:init/1 (supervisor.erl, line 330)
in call from gen_server:init_it/2 (gen_server.erl, line 851)
in call from gen_server:init_it/6 (gen_server.erl, line 814)
ancestors: [<0.528.0>]
message_queue_len: 1
messages: [{'EXIT',<0.528.0>,normal}]
links: []
dictionary: []
trap_exit: true
status: running
heap_size: 2586
stack_size: 28
reductions: 3494
neighbours:
=INFO REPORT==== 21-Mar-2025::10:22:42.594082 ===
The global configuration for Yaws is not set properly.
YAWS needs Docroot dir. Even yaws is working only as backend server.