From 3b8e01885b90edec14e76a53800c7a91f19ea33f Mon Sep 17 00:00:00 2001 From: Martin Wiso Date: Tue, 18 Mar 2014 10:09:08 +0100 Subject: [PATCH] Makce docroot/1 as exported fun to DRY --- src/web/web_admin.erl | 9 +++++++-- src/web/web_admin_req_handler.erl | 17 ++++------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/web/web_admin.erl b/src/web/web_admin.erl index 041a346..32c9e39 100644 --- a/src/web/web_admin.erl +++ b/src/web/web_admin.erl @@ -9,6 +9,7 @@ -behaviour(gen_server). -export([start_link/0]). +-export([docroot/1]). %% gen_server callbacks -export([init/1, @@ -20,6 +21,7 @@ -record(state, {}). + %% =================================================================== %% API functions %% =================================================================== @@ -27,6 +29,10 @@ start_link() -> gen_server:start_link({local, ?MODULE}, ?MODULE, [], []). +docroot(Append) -> + priv_dir() ++ "webadmin/" ++ Append. + + %% =================================================================== %% web admin process callbacks %% =================================================================== @@ -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 diff --git a/src/web/web_admin_req_handler.erl b/src/web/web_admin_req_handler.erl index 0d5c764..aeab345 100644 --- a/src/web/web_admin_req_handler.erl +++ b/src/web/web_admin_req_handler.erl @@ -10,6 +10,7 @@ -export([handle/2]). -export([terminate/3]). + %%============================================================================= %% Cowboy handler callback %%============================================================================= @@ -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">>} @@ -248,18 +251,6 @@ unauthorized_body() -> ">>. -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;