Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
80 lines (61 sloc) 2.18 KB
%%%-------------------------------------------------------------------
%% @copyright Geoff Cant
%% @author Geoff Cant <nem@erlang.geek.nz>
%% @version {@vsn}, {@date} {@time}
%% @doc ef OTP App callback module.
%% @end
%%%-------------------------------------------------------------------
-module(ef_app).
-behaviour(application).
-define(APP, ef).
%% Application callbacks
-export([start/2, stop/1, start_phase/3]).
-export([config/0, config/1, config/2,
start/0, a_start/2]).
%%%===================================================================
%%% Convenience Functions
%%%===================================================================
start() ->
a_start(?APP, permanent).
config(Key, Default) ->
case application:get_env(?APP, Key) of
undefined -> Default;
{ok, Val} -> Val
end.
config(Key) ->
case application:get_env(?APP, Key) of
undefined -> erlang:error({missing_config, Key});
{ok, Val} -> Val
end.
config() ->
application:get_all_env(?APP).
%% ===================================================================
%% Application callbacks
%% ===================================================================
start(_StartType, _StartArgs) ->
ef_sup:start_link().
stop(_State) ->
ok.
start_phase(listen, _Type, _Args) ->
Dispatch = [{'_', [
{'_', ef_http_handler, []}
]}
],
cowboy:start_listener(http, 100, cowboy_tcp_transport,
[{port, config(http_port)}],
cowboy_http_protocol,
[{dispatch, Dispatch}]
),
ok.
%%%===================================================================
%%% Internal functions
%%%===================================================================
a_start(App, Type) ->
start_ok(App, Type, application:start(App, Type)).
start_ok(_App, _Type, ok) -> ok;
start_ok(_App, _Type, {error, {already_started, _App}}) -> ok;
start_ok(App, Type, {error, {not_started, Dep}}) ->
ok = a_start(Dep, Type),
a_start(App, Type);
start_ok(App, _Type, {error, Reason}) ->
erlang:error({app_start_failed, App, Reason}).