PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/ucengine/src/core/config.erl

http://github.com/AF83/ucengine
Erlang | 125 lines | 73 code | 17 blank | 35 comment | 0 complexity | 34d127fb2fb5689309f88e8b7d11b189 MD5 | raw file
  1. %%
  2. %% U.C.Engine - Unified Collaboration Engine
  3. %% Copyright (C) 2011 af83
  4. %%
  5. %% This program is free software: you can redistribute it and/or modify
  6. %% it under the terms of the GNU Affero General Public License as published by
  7. %% the Free Software Foundation, either version 3 of the License, or
  8. %% (at your option) any later version.
  9. %%
  10. %% This program is distributed in the hope that it will be useful,
  11. %% but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. %% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. %% GNU Affero General Public License for more details.
  14. %%
  15. %% You should have received a copy of the GNU Affero General Public License
  16. %% along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. %%
  18. -module(config).
  19. -include("uce.hrl").
  20. -export([init/1,
  21. get/1,
  22. get/2,
  23. set/2,
  24. set/3]).
  25. %%
  26. %% Init the config module with path to the config file
  27. %%
  28. init(Path) ->
  29. case file:consult(Path) of
  30. {ok, Config} ->
  31. init2(Config),
  32. ok;
  33. {error, Reason} ->
  34. ?ERROR_MSG("config file error: ~p~n", [Reason]),
  35. error
  36. end.
  37. %%
  38. %% Get a global config value
  39. %%
  40. get(Key) ->
  41. get(global, Key).
  42. %%
  43. %% Get a config value for the specified host
  44. %%
  45. get(Domain, Key) ->
  46. case ets:lookup(uce_config, {Domain, Key}) of
  47. [{{Domain, Key}, Value}] ->
  48. Value;
  49. _ ->
  50. undefined
  51. end.
  52. %%
  53. %% Set a global config value
  54. %%
  55. set(Key, Value) ->
  56. set(global, Key, Value).
  57. %%
  58. %% Set a config value for the specified host
  59. %%
  60. set(Domain, Key, Value) ->
  61. ets:insert(uce_config, {{Domain, Key}, Value}).
  62. %
  63. % Private functions
  64. %
  65. init2(Config) ->
  66. DB = ets:new(uce_config, [set, public, {keypos, 1}, named_table]),
  67. Hosts = proplists:get_value(hosts, Config),
  68. lists:foreach(fun({Domain, HostConfig}) ->
  69. insert_in(Domain, merge(HostConfig, Config), DB)
  70. end, Hosts),
  71. insert_in(global, Config, DB).
  72. merge(Config, Default) ->
  73. Config ++ merge_keys(Config, Default).
  74. merge_keys(_Config, []) ->
  75. [];
  76. merge_keys(Config, [{Key, Value}|R]) ->
  77. case proplists:lookup(Key, Config) of
  78. none ->
  79. [{Key, Value} | merge_keys(Config, R)];
  80. _ ->
  81. merge_keys(Config, R)
  82. end.
  83. insert_in(Domain, Config, DB) ->
  84. lists:foreach(fun({Key, Value}) ->
  85. ets:insert(DB, {{Domain, Key}, Value})
  86. end, Config).
  87. -ifdef(TEST).
  88. -include_lib("eunit/include/eunit.hrl").
  89. merge_simple_test() ->
  90. Default = [{bricks, [{"translation", "1"}]}],
  91. Config = [{bricks, [{"erlyvideo", "2"}]}],
  92. Result = [{bricks, [{"erlyvideo", "2"}]}],
  93. ?assertEqual(Result, merge(Config, Default)).
  94. merge_complex_test() ->
  95. Default = [{bricks, [{"translation", "1"}]}, {admin, []}],
  96. Config = [{bricks, [{"erlyvideo", "2"}]}],
  97. Result = [{bricks, [{"erlyvideo", "2"}]}, {admin, []}],
  98. ?assertEqual(Result, merge(Config, Default)).
  99. config_test() ->
  100. Configs = [{bricks, [{"erlyvideo", "2"}]},
  101. {wwwroot, "/var/www"},
  102. {hosts, [{"localhost", [{bricks, [{"translation", "1"}]}]},
  103. {"example.com", [{data, "/var/spool"}]}]}],
  104. init2(Configs),
  105. ?assertEqual([{"translation", "1"}], config:get("localhost", bricks)),
  106. ?assertEqual([{"erlyvideo", "2"}], config:get("example.com", bricks)),
  107. ?assertEqual("/var/www", config:get(wwwroot)).
  108. -endif.