PageRenderTime 83ms CodeModel.GetById 41ms app.highlight 14ms RepoModel.GetById 26ms 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
 20-include("uce.hrl").
 21
 22-export([init/1,
 23         get/1,
 24         get/2,
 25         set/2,
 26         set/3]).
 27
 28%%
 29%% Init the config module with path to the config file
 30%%
 31init(Path) ->
 32    case file:consult(Path) of
 33        {ok, Config} ->
 34            init2(Config),
 35            ok;
 36        {error, Reason} ->
 37            ?ERROR_MSG("config file error: ~p~n", [Reason]),
 38            error
 39    end.
 40
 41%%
 42%% Get a global config value
 43%%
 44get(Key) ->
 45    get(global, Key).
 46
 47%%
 48%% Get a config value for the specified host
 49%%
 50get(Domain, Key) ->
 51    case ets:lookup(uce_config, {Domain, Key}) of
 52        [{{Domain, Key}, Value}] ->
 53            Value;
 54        _ ->
 55            undefined
 56    end.
 57
 58%%
 59%% Set a global config value
 60%%
 61set(Key, Value) ->
 62    set(global, Key, Value).
 63
 64%%
 65%% Set a config value for the specified host
 66%%
 67set(Domain, Key, Value) ->
 68    ets:insert(uce_config, {{Domain, Key}, Value}).
 69
 70%
 71% Private functions
 72%
 73
 74init2(Config) ->
 75    DB = ets:new(uce_config, [set, public, {keypos, 1}, named_table]),
 76    Hosts = proplists:get_value(hosts, Config),
 77    lists:foreach(fun({Domain, HostConfig}) ->
 78                          insert_in(Domain, merge(HostConfig, Config), DB)
 79                  end, Hosts),
 80    insert_in(global, Config, DB).
 81
 82merge(Config, Default) ->
 83    Config ++ merge_keys(Config, Default).
 84
 85merge_keys(_Config, []) ->
 86    [];
 87merge_keys(Config, [{Key, Value}|R]) ->
 88    case proplists:lookup(Key, Config) of
 89        none ->
 90            [{Key, Value} | merge_keys(Config, R)];
 91        _ ->
 92            merge_keys(Config, R)
 93    end.
 94
 95insert_in(Domain, Config, DB) ->
 96    lists:foreach(fun({Key, Value}) ->
 97                          ets:insert(DB, {{Domain, Key}, Value})
 98                  end, Config).
 99
100-ifdef(TEST).
101-include_lib("eunit/include/eunit.hrl").
102
103merge_simple_test() ->
104    Default = [{bricks, [{"translation", "1"}]}],
105    Config = [{bricks, [{"erlyvideo", "2"}]}],
106    Result = [{bricks, [{"erlyvideo", "2"}]}],
107    ?assertEqual(Result, merge(Config, Default)).
108
109merge_complex_test() ->
110    Default = [{bricks, [{"translation", "1"}]}, {admin, []}],
111    Config = [{bricks, [{"erlyvideo", "2"}]}],
112    Result = [{bricks, [{"erlyvideo", "2"}]}, {admin, []}],
113    ?assertEqual(Result, merge(Config, Default)).
114
115config_test() ->
116    Configs = [{bricks, [{"erlyvideo", "2"}]},
117               {wwwroot, "/var/www"},
118               {hosts, [{"localhost", [{bricks, [{"translation", "1"}]}]},
119                        {"example.com", [{data, "/var/spool"}]}]}],
120    init2(Configs),
121    ?assertEqual([{"translation", "1"}], config:get("localhost", bricks)),
122    ?assertEqual([{"erlyvideo", "2"}], config:get("example.com", bricks)),
123    ?assertEqual("/var/www", config:get(wwwroot)).
124
125-endif.