Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/ezwebframe.erl
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,9 @@
-record(env, {dispatch}).

start_embedded(Port) ->
ok = application:start(ranch),
ok = application:start(cowboy),
web_server_start(Port, "zip"),
receive
after
infinity ->
true
end.
ok = start_if_not_running(ranch),
ok = start_if_not_running(cowboy),
web_server_start(Port, "zip").

start_link([PortAtom, DirAtom]) ->
Port = list_to_integer(atom_to_list(PortAtom)),
Expand All @@ -36,33 +31,29 @@ start_link([PortAtom, DirAtom]) ->
start_link(Dir, Port).

start_link(Dispatch, Port) ->
ok = application:start(crypto),
ok = application:start(ranch),
ok = application:start(cowboy),
ok = start_if_not_running(crypto),
ok = start_if_not_running(ranch),
ok = start_if_not_running(cowboy),
ok = web_server_start(Port, Dispatch),
receive
after
infinity ->
true
end.
ok.

web_server_start(Port, Dispatcher) ->
E0 = #env{dispatch=Dispatcher},
Dispatch = [{'_', [{'_', ?MODULE, E0}]}],
%% server is the name of this module
NumberOfAcceptors = 100,
Status =
cowboy:start_http(my_named_thing,
NumberOfAcceptors,
[{port, Port}],
[{dispatch, Dispatch}]),
cowboy:start_http(my_named_thing,
NumberOfAcceptors,
[{port, Port}],
[{dispatch, Dispatch}]),
case Status of
{error, _} ->
io:format("websockets could not be started -- "
"port ~p probably in use~n", [Port]),
init:stop();
{ok, _Pid} ->
io:format("websockets started on port:~p~n",[Port])
{error, _} ->
io:format("websockets could not be started -- "
"port ~p probably in use~n", [Port]),
init:stop();
{ok, _Pid} ->
io:format("websockets started on port:~p~n",[Port])
end.

init(_, Req, E0) ->
Expand Down Expand Up @@ -90,7 +81,10 @@ handle(Req, Env) ->
io:format("mapped to:~p~n",[Res1]),
case Resource of
"/" ->
serve_file("index.html", Req, Env);
case filelib:is_file("index.html") of
true -> serve_file("index.html", Req, Env);
false -> serve_file(Res1,Req,Env)
end;
"/files" ->
list_dir(F("/"), Req, Env);
_ ->
Expand Down Expand Up @@ -263,6 +257,14 @@ atomize(L) when is_list(L) ->
atomize(X) ->
X.

start_if_not_running(App) ->
Running = [A || {A,_Name,_Version} <- application:which_applications(), A =:= App],
case Running of
[] ->
application:start(App);
_ ->
ok
end.
%%----------------------------------------------------------------------
%% these are to be called from the gui client code

Expand Down