From 38fcfa9d79c60d9fb57ddd283b202bca6446d909 Mon Sep 17 00:00:00 2001 From: nisbus Date: Sun, 30 Dec 2012 19:50:33 +0000 Subject: [PATCH 1/5] Changed the ok = application:start(app) to ignore errors if the application was already started. --- src/ezwebframe.erl | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ezwebframe.erl b/src/ezwebframe.erl index 0a2c37b..7f3113c 100644 --- a/src/ezwebframe.erl +++ b/src/ezwebframe.erl @@ -9,7 +9,8 @@ websocket_info/3, append_div/3, pre/1, - fill_div/3 + fill_div/3, + start_if_not_running/1 ]). -import(ezwebframe_mochijson2, [encode/1, decode/1]). @@ -20,8 +21,8 @@ -record(env, {dispatch}). start_embedded(Port) -> - ok = application:start(ranch), - ok = application:start(cowboy), + ok = start_if_not_running(ranch), + ok = start_if_not_running(cowboy), web_server_start(Port, "zip"), receive after @@ -36,9 +37,9 @@ 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 @@ -263,6 +264,11 @@ atomize(L) when is_list(L) -> atomize(X) -> X. +start_if_not_running(App) -> + case application:get_application(App) of + {ok, App} -> ok; + undefined -> application:start(App) + end. %%---------------------------------------------------------------------- %% these are to be called from the gui client code From c2b601d8a375da91521c3698c001aa53fd25b1b3 Mon Sep 17 00:00:00 2001 From: nisbus Date: Sun, 30 Dec 2012 19:52:48 +0000 Subject: [PATCH 2/5] Removed the start_if_not_running function from the export section --- src/ezwebframe.erl | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/ezwebframe.erl b/src/ezwebframe.erl index 7f3113c..9025998 100644 --- a/src/ezwebframe.erl +++ b/src/ezwebframe.erl @@ -9,8 +9,7 @@ websocket_info/3, append_div/3, pre/1, - fill_div/3, - start_if_not_running/1 + fill_div/3 ]). -import(ezwebframe_mochijson2, [encode/1, decode/1]). From f51f46378fc5009e8f000ce68d304d861b388830 Mon Sep 17 00:00:00 2001 From: nisbus Date: Sun, 30 Dec 2012 20:41:19 +0000 Subject: [PATCH 3/5] Fixed the loading of index.html if it's located in the priv_dir of the application. Also removed the recevie after infinity in the start functions so the shell is now operational when starting the app. --- src/ezwebframe.erl | 49 +++++++++++++++++++++++----------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/src/ezwebframe.erl b/src/ezwebframe.erl index 9025998..85d8861 100644 --- a/src/ezwebframe.erl +++ b/src/ezwebframe.erl @@ -22,12 +22,7 @@ start_embedded(Port) -> ok = start_if_not_running(ranch), ok = start_if_not_running(cowboy), - web_server_start(Port, "zip"), - receive - after - infinity -> - true - end. + web_server_start(Port, "zip"). start_link([PortAtom, DirAtom]) -> Port = list_to_integer(atom_to_list(PortAtom)), @@ -36,33 +31,31 @@ start_link([PortAtom, DirAtom]) -> start_link(Dir, Port). start_link(Dispatch, Port) -> + io:format("starting crypto ranch and cowboy~n"), 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) -> + io:format("starting webserver ~n"), 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) -> @@ -90,7 +83,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); _ -> @@ -264,9 +260,12 @@ atomize(X) -> X. start_if_not_running(App) -> - case application:get_application(App) of - {ok, App} -> ok; - undefined -> application:start(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 From f9d3d3a3955f8fefda65ebf56db997b1cc75ce51 Mon Sep 17 00:00:00 2001 From: nisbus Date: Sun, 30 Dec 2012 19:50:33 +0000 Subject: [PATCH 4/5] Changed the ok = application:start(app) to ignore errors if the application was already started. Fixed the loading of index.html if it's located in the priv_dir of the application. Also removed the recevie after infinity in the start functions so the shell is now operational when starting the app. --- src/ezwebframe.erl | 58 +++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/src/ezwebframe.erl b/src/ezwebframe.erl index 0a2c37b..85d8861 100644 --- a/src/ezwebframe.erl +++ b/src/ezwebframe.erl @@ -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)), @@ -36,33 +31,31 @@ start_link([PortAtom, DirAtom]) -> start_link(Dir, Port). start_link(Dispatch, Port) -> - ok = application:start(crypto), - ok = application:start(ranch), - ok = application:start(cowboy), + io:format("starting crypto ranch and cowboy~n"), + 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) -> + io:format("starting webserver ~n"), 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) -> @@ -90,7 +83,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); _ -> @@ -263,6 +259,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 From ef875a62490e2a06eb87f2a415ee2140718286d9 Mon Sep 17 00:00:00 2001 From: nisbus Date: Sun, 30 Dec 2012 21:14:52 +0000 Subject: [PATCH 5/5] Removed some io:formats introduced by earlier debug efforts. --- src/ezwebframe.erl | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/ezwebframe.erl b/src/ezwebframe.erl index 85d8861..4d1d4e4 100644 --- a/src/ezwebframe.erl +++ b/src/ezwebframe.erl @@ -31,7 +31,6 @@ start_link([PortAtom, DirAtom]) -> start_link(Dir, Port). start_link(Dispatch, Port) -> - io:format("starting crypto ranch and cowboy~n"), ok = start_if_not_running(crypto), ok = start_if_not_running(ranch), ok = start_if_not_running(cowboy), @@ -39,7 +38,6 @@ start_link(Dispatch, Port) -> ok. web_server_start(Port, Dispatcher) -> - io:format("starting webserver ~n"), E0 = #env{dispatch=Dispatcher}, Dispatch = [{'_', [{'_', ?MODULE, E0}]}], %% server is the name of this module