Permalink
Browse files

Merge pull request #274 from webmachine/try-lager3

Cleaner configuration management
  • Loading branch information...
2 parents 4822a49 + a06256e commit 6c4265808d8d307d76f28483f5eaada7d261ea18 @joedevivo joedevivo committed on GitHub Oct 17, 2016
Showing with 63 additions and 25 deletions.
  1. +12 −1 src/webmachine.app.src
  2. +35 −6 src/webmachine_app.erl
  3. +13 −12 src/webmachine_mochiweb.erl
  4. +3 −6 src/webmachine_request.erl
View
@@ -10,7 +10,18 @@
crypto,
mochiweb]},
{mod, {webmachine_app, []}},
- {env, []},
+ {env,
+ %% env is THE place for expected defaults. Even if it's just
+ %% comments, it's worthwhile
+ [
+ {log_handlers, []}
+ ,{error_handler, webmachine_error_handler}
+ %% error_handler is a module that implements the function render_error/3
+ ,{rewrite_module, undefined}
+ %% module that has rewrite/5
+ ,{server_name, undefined}
+ %% string() for the "Server" response header
+ ]},
{contributors,["Sean Cribbs", "Joe DeVivo" "Bryan Fink",
"Kelly McLaughlin", "Jared Morrow", "Andy Gross",
View
@@ -27,17 +27,19 @@
-include("webmachine_logger.hrl").
+-define(QUIP, "cafe not found").
+
%% @spec start(_Type, _StartArgs) -> ServerRet
%% @doc application start callback for webmachine.
start(_Type, _StartArgs) ->
webmachine_deps:ensure(),
+
+ %% Populate dynamic defaults on load:
+ load_default_app_config(),
+
{ok, _Pid} = SupLinkRes = webmachine_sup:start_link(),
- Handlers = case application:get_env(webmachine, log_handlers) of
- undefined ->
- [];
- {ok, Val} ->
- Val
- end,
+ Handlers = application:get_env(webmachine, log_handlers, []),
+
%% handlers failing to start are handled in the handler_watcher
_ = [supervisor:start_child(webmachine_logger_watcher_sup,
[?EVENT_LOGGER, Module, Config]) ||
@@ -48,3 +50,30 @@ start(_Type, _StartArgs) ->
%% @doc application stop callback for webmachine.
stop(_State) ->
ok.
+
+-spec load_default_app_config() -> ok.
+load_default_app_config() ->
+
+ case application:get_env(webmachine, server_name, undefined) of
+ Name when is_list(Name) ->
+ ok;
+ _ ->
+ set_default_server_name()
+ end,
+ ok.
+
+
+set_default_server_name() ->
+ {mochiweb, _, MochiVersion} =
+ lists:keyfind(mochiweb, 1, application:loaded_applications()),
+
+ {webmachine, _, WMVersion} =
+ lists:keyfind(webmachine, 1, application:loaded_applications()),
+ ServerName =
+ lists:flatten(
+ io_lib:format(
+ "MochiWeb/~s WebMachine/~s (~s)",
+ [MochiVersion, WMVersion, ?QUIP])),
+ application:set_env(
+ webmachine, server_name, ServerName),
+ ok.
@@ -121,20 +121,21 @@ new_webmachine_req(Request) ->
Method = mochiweb_request:get(method, Request),
Scheme = mochiweb_request:get(scheme, Request),
Version = mochiweb_request:get(version, Request),
- {Headers, RawPath} = case application:get_env(webmachine, rewrite_module) of
- {ok, RewriteMod} ->
- do_rewrite(RewriteMod,
- Method,
- Scheme,
- Version,
- mochiweb_request:get(headers, Request),
- mochiweb_request:get(raw_path, Request));
- undefined ->
- {
+ {Headers, RawPath} =
+ case application:get_env(webmachine, rewrite_module) of
+ {ok, undefined} ->
+ {
mochiweb_request:get(headers, Request),
mochiweb_request:get(raw_path, Request)
- }
- end,
+ };
+ {ok, RewriteMod} ->
+ do_rewrite(RewriteMod,
+ Method,
+ Scheme,
+ Version,
+ mochiweb_request:get(headers, Request),
+ mochiweb_request:get(raw_path, Request))
+ end,
Socket = mochiweb_request:get(socket, Request),
InitialReqData = wrq:create(Method,Scheme,Version,RawPath,Headers),
@@ -100,8 +100,6 @@
-include("wm_reqstate.hrl").
-include("wm_reqdata.hrl").
--define(WMVSN, "1.10.9").
--define(QUIP, "cafe not found").
-define(IDLE_TIMEOUT, infinity).
-type t() :: {?MODULE, #wm_reqstate{}}.
@@ -744,10 +742,9 @@ make_headers(Code, Length, RD) when is_integer(Code) ->
mochiweb_headers:make(wrq:resp_headers(RD)))
end
end,
- case application:get_env(webmachine, server_name) of
- undefined -> ServerHeader = "MochiWeb/1.1 WebMachine/" ++ ?WMVSN ++ " (" ++ ?QUIP ++ ")";
- {ok, ServerHeader} when is_list(ServerHeader) -> ok
- end,
+ %% server_name is guaranteed to be set by
+ %% webmachine_app:load_default_app_config/0
+ {ok, ServerHeader} = application:get_env(webmachine, server_name),
WithSrv = mochiweb_headers:enter("Server", ServerHeader, Hdrs0),
Hdrs = case mochiweb_headers:get_value("date", WithSrv) of
undefined ->

0 comments on commit 6c42658

Please sign in to comment.