erlangyawsrebar3

Configuration Essue while embeding yaws to rebar3 for serverside operatios


i am attempting to start the yaws via yaws_api:embedded_start_conf/1,2,3,4 in supervisor. but getting configuration issue. As i am only using Yaws to serve dynamic content via handlers (for example, serving API responses or dynamic pages via Erlang modules). not want to serve static files from a directory.

here is the supervisor,

%%%-------------------------------------------------------------------
%% @doc workchat top level supervisor.
%% @end
%%%-------------------------------------------------------------------

-module(workchat_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([]) ->
    ID = "Workchat Backend",
    ServerConf = [
        {listen, {127, 0, 0, 1}},
        {docroot, none},
        {port, 8080}
        % {dynamic_handlers, [{"/api", workchat_yaws_handler}]},
        % {max_connections, 1024},
        % {ebin_dir, "/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/workchat/ebin"},
        % {logdir, "/mnt/c/Users/PankajMali/Project/workchat/yawslogs"},
        % {keepalive_timeout, 60000}
    ],

    GlobalConf =
        [
            {id, ID},
            {ebin_dir, "/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/workchat/ebin"},
            {logdir, "/mnt/c/Users/PankajMali/Project/workchat/yawslogs"},
            {dynamic_handlers, [{"/api", workchat_yaws_handler}]}
        ],
        %% Start Yaws explicitly
    case
        yaws_api:embedded_start_conf(none, ServerConf, GlobalConf, ID
        )
    of
        {ok, GlobalConf, ServerConf, Child} ->
            io:format("Yaws started successfully with GlobalConf: ~p and ServerConf: ~p~n", [
                GlobalConf, ServerConf
            ]),
            io:format("Yaws configuration set successfully~n"),

            %% Define the workchat_app child specification
            WorkchatAppChild = #{
                id => workchat_app,
                start => {workchat_app, start_link, []},
                restart => permanent,
                shutdown => 5000,
                type => worker,
                modules => [workchat_app]
            },

            %% Combine Yaws child specifications with workchat_app child specification
            ChildSpecs = Child ++ [WorkchatAppChild],
            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.

HERE is the error iam facing

  crasher:
    initial call: supervisor:workchat_sup/1
    pid: <0.535.0>
    registered_name: []
    exception error: no function clause matching
                     yaws_api:embedded_start_conf(none,
                                                  [{listen,{127,0,0,1}},
                                                   {docroot,none},
                                                   {port,8080}],
                                                  [{id,"Workchat Backend"},
                                                   {ebin_dir,
                                                    "/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/workchat/ebin"},
                                                   {logdir,
                                                    "/mnt/c/Users/PankajMali/Project/workchat/yawslogs"},
                                                   {dynamic_handlers,
                                                    [{"/api",
                                                      workchat_yaws_handler}]}],
                                                  "Workchat Backend") (/mnt/c/Users/PankajMali/Project/workchat/_build/default/lib/yaws/src/yaws_api.erl, line 2732)
      in function  workchat_sup:init/1 (/mnt/c/Users/PankajMali/Project/workchat/src/workchat_sup.erl, line 42)     
      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: 376
    stack_size: 28
    reductions: 184
  neighbours:

Solution

  • The function yaws_api:embedded_start_conf requires a string as its first argument (see the source), but none is an atom. As far as I can tell, you have to specify a docroot even if your application doesn't need one.