Skip to content

Commit 0f6fda1

Browse files
author
Prashanth Menon
committed
Setup common configuration support
1 parent e7b1a44 commit 0f6fda1

File tree

6 files changed

+94
-11
lines changed

6 files changed

+94
-11
lines changed

conf/storage.properties

Whitespace-only changes.

conf/thor.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{port, 8080}.

ebin/thor.app

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77
thor_socket ]},
88
{registered, [ thor_sup ]},
99
{applications, [kernel, stdlib]},
10-
{mod, {thor, []}},
11-
{env, [{conf, "/conf/storage.properties"}]}
10+
{mod, {thor, [ "/Users/menonp/development/thor/conf/thor.conf" ]}},
11+
{env, [{conf, "/conf/thor.conf"}]}
1212
]}.

src/thor.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ start() ->
1111
io:format("in thor:start/0~n", []),
1212
application:start(thor).
1313

14-
start(_Type, _StartArgs) ->
14+
start(_Type, DefaultConfigFile) ->
1515
print_banner(),
1616
io:format("Starting Thor ...~n"),
17-
case thor_sup:start_link() of
17+
case thor_sup:start_link(DefaultConfigFile) of
1818
{ok, Pid} ->
1919
%%alarm_handler:clear_alarm({application_stopped, thor}),
2020
{ok, Pid};

src/thor_config.erl

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
-module(thor_config).
2+
-behaviour(gen_server).
3+
4+
-include("thor.hrl").
5+
6+
%%API
7+
-export([start_link/1, get/1, set/2]).
8+
9+
%% gen_server callbacks
10+
-export([init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2, code_change/3]).
11+
12+
-define(SERVER, ?MODULE).
13+
14+
-record(conf, {conf}).
15+
16+
start_link(ConfFiles) ->
17+
gen_server:start_link({local, ?SERVER}, ?MODULE, [ConfFiles], []).
18+
19+
get(Key) ->
20+
gen_server:call(?SERVER, {get, Key}).
21+
22+
set(Key, Value) ->
23+
gen_server:cast(?SERVER, {set, Key, Value}).
24+
25+
init(ConfFiles) ->
26+
io:format("Reading configuration file: ~p~n", [ConfFiles]),
27+
Terms = lists:foldl(fun(ConfFile, Acc) ->
28+
Terms = case file:consult(ConfFile) of
29+
{ok, T} -> T;
30+
_ -> []
31+
end,
32+
lists:append(Acc, Terms)
33+
end, [], ConfFiles),
34+
{ok, #conf{conf = Terms}}.
35+
36+
get_key(Key, []) ->
37+
{error, not_found};
38+
get_key(Key, [{Key, Value} | Rest]) ->
39+
{ok, Value};
40+
get_key(Key, [{_Key, _Value} | Rest]) ->
41+
get_key(Key, Rest).
42+
43+
handle_call({get, Key}, From, State) ->
44+
Reply = get_key(Key, State#conf.conf),
45+
{reply, Reply, State};
46+
47+
handle_call(_Request, _From, State) ->
48+
{reply, ok, State}.
49+
50+
handle_cast({set, Key, Value}, State) ->
51+
{noreply, State};
52+
53+
handle_cast(_Request, State) ->
54+
{noreply, State}.
55+
56+
handle_info(_Info, State) ->
57+
{noreply, State}.
58+
59+
terminate(_Reason, _State) ->
60+
ok.
61+
62+
code_change(_OldVsn, State, _Extra) ->
63+
{ok, State}.
64+

src/thor_sup.erl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,39 @@
44
-include("thor.hrl").
55

66
%% supervisor behaviour callbacks
7-
-export([start_link/0, init/1]).
7+
-export([start_link/1, init/1]).
88

99
-define(SERVER, ?MODULE).
1010

11-
start_link() ->
12-
supervisor:start_link({local, ?SERVER}, ?MODULE, []).
11+
start_link(DefaultConfigFile) ->
12+
supervisor:start_link({local, ?SERVER}, ?MODULE, [DefaultConfigFile]).
1313

14-
init([]) ->
15-
Port = 8080,
14+
init(DefaultConfigFiles) ->
15+
ConfigFile = get_config_file(DefaultConfigFiles),
16+
thor_config:start_link(ConfigFile),
17+
18+
%%Config = {thor_config, {thor_config, start_link, [ConfigFile]},
19+
%% permanent, 2000, worker, [thor_config]},
20+
Log = {thor_log, {thor_log, start_link, [?DEFAULT_LOGGER]},
21+
permanent, 2000, worker, [thor_log]},
22+
23+
{ok, Port} = thor_config:get(port),
24+
io:format("port = ~p~n", [Port]),
1625
HttpServer = {thor_server, {thor_server, start_link, [Port]},
1726
permanent, 2000, worker, [thor_server]},
27+
1828
ChannelServer = {thor_channel_server, {thor_channel_server, start_link, []},
1929
permanent, 2000, worker, [thor_channel_server]},
20-
Log = {thor_log, {thor_log, start_link, [?DEFAULT_LOGGER]},
21-
permanent, 2000, worker, [thor_log]},
2230

2331
{ok, {{one_for_one, 10, 1}, [Log, HttpServer, ChannelServer]}}.
2432

33+
get_config_file(Default) ->
34+
case init:get_argument(conf) of
35+
error ->
36+
Default;
37+
{ok, [[]]} ->
38+
Default;
39+
{ok, [Values]} ->
40+
Values
41+
end.
42+

0 commit comments

Comments
 (0)