Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes in the starting of dependencies and loading of index.html from priv_dir #6

Open
wants to merge 6 commits into from

1 participant

@nisbus

The commit's are a bit of a mess (a rebase gone haywire) but I think the changes are good.

I was testing this for my own site (not running there yet) and I found that when reltool included crypto, ezwebframe would crash since it would already have been started.

Also I created my own application using ezwebframe as a dependency and put all my html/css/js into the priv_dir. Calling for index.html didn't work that way but it works with these changes.

nisbus added some commits
nisbus Changed the ok = application:start(app) to ignore errors if the appli…
…cation was already started.
38fcfa9
nisbus Removed the start_if_not_running function from the export section c2b601d
nisbus 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.
f51f463
nisbus Changed the ok = application:start(app) to ignore errors if the appli…
…cation 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.
f9d3d3a
nisbus Merge branch 'master' of github.com:nisbus/ezwebframe ee819b3
nisbus Removed some io:formats introduced by earlier debug efforts. ef875a6
@nisbus

This is my home.erl dispatch method:

dispatch('/') ->
Dir = code:priv_dir(eznisbus),
Dir++"index.html";
dispatch(F) ->
Dir = code:priv_dir(eznisbus),
Dir++ F.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 30, 2012
  1. Changed the ok = application:start(app) to ignore errors if the appli…

    nisbus authored
    …cation was already started.
  2. Fixed the loading of index.html if it's located in the priv_dir

    nisbus authored
    of the application.
    Also removed the recevie after infinity in the start functions so the shell is now
    operational when starting the app.
  3. Changed the ok = application:start(app) to ignore errors if the appli…

    nisbus authored
    …cation 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.
This page is out of date. Refresh to see the latest.
Showing with 29 additions and 27 deletions.
  1. +29 −27 src/ezwebframe.erl
View
56 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,15 +31,11 @@ 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},
@@ -52,17 +43,17 @@ web_server_start(Port, Dispatcher) ->
%% 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 +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);
_ ->
@@ -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
Something went wrong with that request. Please try again.