Skip to content

Commit

Permalink
Makce docroot/1 as exported fun to DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
tgrk committed Mar 18, 2014
1 parent fd1d2e2 commit 3b8e018
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/web/web_admin.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
-behaviour(gen_server).

-export([start_link/0]).
-export([docroot/1]).

%% gen_server callbacks
-export([init/1,
Expand All @@ -20,13 +21,18 @@

-record(state, {}).


%% ===================================================================
%% API functions
%% ===================================================================

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

docroot(Append) ->
priv_dir() ++ "webadmin/" ++ Append.


%% ===================================================================
%% web admin process callbacks
%% ===================================================================
Expand Down Expand Up @@ -80,11 +86,10 @@ terminate(_Reason, _State) ->
code_change(_OldVsn, State, _Extra) ->
{ok, State}.


%% ===================================================================
%% Internal functions
%% ===================================================================
docroot(Append) ->
priv_dir() ++ "webadmin/" ++ Append.

priv_dir() ->
case code:priv_dir(ybot) of
Expand Down
17 changes: 4 additions & 13 deletions src/web/web_admin_req_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-export([handle/2]).
-export([terminate/3]).


%%=============================================================================
%% Cowboy handler callback
%%=============================================================================
Expand Down Expand Up @@ -39,14 +40,16 @@ handle(Req, State) ->
terminate(_Reason, _Req, _State) ->
ok.


%%=============================================================================
%% Internal functions
%%=============================================================================

authorized(Req, State) ->
{Path, Req1} = cowboy_req:path(Req),
case Path of
<<"/">> ->
{ok, Bin} = file:read_file(docroot("index.html")),
{ok, Bin} = file:read_file(web_admin:docroot("index.html")),
{ok, Req2} = cowboy_req:reply(200,
[
{<<"content-type">>, <<"text/html">>}
Expand Down Expand Up @@ -248,18 +251,6 @@ unauthorized_body() ->
</html>
">>.

docroot(Append) ->
priv_dir() ++ "webadmin/" ++ Append.

priv_dir() ->
case code:priv_dir(ybot) of
{error, bad_name} ->
{ok, Cwd} = file:get_cwd(),
Cwd ++ "/" ++ "priv/";
Priv ->
Priv ++ "/"
end.

config_option(Key, Options) ->
case lists:keyfind(Key, 1, Options) of
{Key, Value} -> Value;
Expand Down

0 comments on commit 3b8e018

Please sign in to comment.