-
Notifications
You must be signed in to change notification settings - Fork 0
/
herokuherd_app.erl
78 lines (59 loc) · 2.1 KB
/
herokuherd_app.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
%%%-------------------------------------------------------------------
%% @copyright {{copyright_holder}} ({{copyright_year}})
%% @author {{author_name}} <{{author_email}}>
%% @doc {{appid}} OTP application callback module.
%% @end
%%%-------------------------------------------------------------------
-module({{appid}}_app).
-behaviour(application).
-define(APP, {{appid}}).
%% Application callbacks
-export([start_phase/3, start/2, stop/1]).
-export([config/0, config/1, config/2,
start/0]).
-export([update_dispatch_rules/0]).
%%%===================================================================
%%% Convenience Functions
%%%===================================================================
start() ->
application:ensure_all_started(?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).
update_dispatch_rules() ->
cowboy:set_env(?APP,
dispatch,
cowboy_router:compile(dispatch_rules())).
%% ===================================================================
%% Application callbacks
%% ===================================================================
start(_StartType, _StartArgs) ->
stillir:set_config(?APP, env_specs()),
{{appid}}_sup:start_link().
stop(_State) ->
ok.
start_phase(listen, _Type, _Args) ->
Dispatch = cowboy_router:compile(dispatch_rules()),
cowboy:start_http(?APP, config(http_acceptors),
[{port, config(http_listen_port)}],
[{env, [{dispatch, Dispatch}]}]),
ok.
dispatch_rules() ->
[{'_',
[{'_', {{appid}}_http_handler, []}]}
].
%%%===================================================================
%%% Internal functions
%%%===================================================================
env_specs() ->
[{http_listen_port, "PORT", [{transform, integer}]}
].