Skip to content

Commit

Permalink
Merge pull request #3409 from esl/config-defaults-general
Browse files Browse the repository at this point in the history
Introduce config defaults for the 'general' section
  • Loading branch information
Premwoik committed Nov 23, 2021
2 parents 15541f0 + c75b417 commit e270ef1
Show file tree
Hide file tree
Showing 37 changed files with 296 additions and 140 deletions.
6 changes: 3 additions & 3 deletions include/mongoose.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
%% If the ejabberd application description isn't loaded, returns atom: undefined
-define(MONGOOSE_VERSION, element(2, application:get_key(mongooseim,vsn))).

-define(MYHOSTS, mongoose_config:get_opt(hosts, [])).
-define(ALL_HOST_TYPES, mongoose_config:get_opt(hosts, []) ++ mongoose_config:get_opt(host_types, [])).
-define(MYHOSTS, mongoose_config:get_opt(hosts)).
-define(ALL_HOST_TYPES, mongoose_config:get_opt(hosts) ++ mongoose_config:get_opt(host_types)).
-define(MYNAME, mongoose_config:get_opt(default_server_domain)).
-define(MYLANG, mongoose_config:get_opt(language, <<"en">>)).
-define(MYLANG, mongoose_config:get_opt(language)).

-define(CONFIG_PATH, "etc/mongooseim.toml").

Expand Down
4 changes: 3 additions & 1 deletion include/mongoose_config_spec.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
required = [] :: [mongoose_config_parser_toml:toml_key()] | all,
validate = any :: mongoose_config_validator:section_validator(),
process :: undefined | mongoose_config_parser_toml:list_processor(),
format = default :: mongoose_config_spec:format()}).
format = default :: mongoose_config_spec:format(),
defaults = #{} :: #{mongoose_config_parser_toml:toml_key() =>
mongoose_config_parser_toml:config_part()}}).

-record(list, {items :: mongoose_config_spec:config_node(),
validate = any :: mongoose_config_validator:list_validator(),
Expand Down
27 changes: 19 additions & 8 deletions src/config/mongoose_config_parser_toml.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,13 @@ ensure_keys(Keys, Section) ->

-spec parse_section(path(), toml_section(), mongoose_config_spec:config_section()) ->
[config_part()].
parse_section(Path, M, #section{items = Items}) ->
lists:flatmap(fun({K, V}) ->
handle([K|Path], V, get_spec_for_key(K, Items))
end, lists:sort(maps:to_list(M))).
parse_section(Path, M, #section{items = Items, defaults = Defaults}) ->
FilteredDefaults = maps:filter(fun(K, _V) -> not maps:is_key(K, M) end, Defaults),
ProcessedConfig = maps:map(fun(K, V) -> handle([K|Path], V, get_spec_for_key(K, Items)) end, M),
ProcessedDefaults = maps:map(fun(K, V) -> handle_default([K|Path], V, maps:get(K, Items)) end,
FilteredDefaults),
lists:flatmap(fun({_K, ConfigParts}) -> ConfigParts end,
lists:keysort(1, maps:to_list(maps:merge(ProcessedDefaults, ProcessedConfig)))).

-spec get_spec_for_key(toml_key(), map()) -> mongoose_config_spec:config_node().
get_spec_for_key(Key, Items) ->
Expand All @@ -122,19 +125,27 @@ parse_list(Path, L, #list{items = ItemSpec}) ->
handle([Key|Path], Elem, ItemSpec)
end, L).

-spec handle(path(), toml_value(), mongoose_config_spec:config_node()) -> config_part().
-spec handle(path(), toml_value(), mongoose_config_spec:config_node()) -> [config_part()].
handle(Path, Value, Spec) ->
handle(Path, Value, Spec, [parse, validate, process, format]).

-spec handle_default(path(), toml_value(), mongoose_config_spec:config_node()) -> [config_part()].
handle_default(Path, Value, Spec) ->
handle(Path, Value, Spec, [format]).

-spec handle(path(), toml_value(), mongoose_config_spec:config_node(), [step()]) -> [config_part()].
handle(Path, Value, Spec, Steps) ->
lists:foldl(fun(_, [#{what := _, class := error}] = Error) ->
Error;
(Step, Acc) ->
try_step(Step, Path, Value, Acc, Spec)
end, Value, [parse, validate, process, format]).
end, Value, Steps).

-spec handle_step(step(), path(), toml_value(), mongoose_config_spec:config_node()) ->
config_part().
handle_step(parse, Path, Value, Spec) ->
ParsedValue = case Spec of
#section{} = Spec when is_map(Value) ->
#section{} when is_map(Value) ->
check_required_keys(Spec, Value),
validate_keys(Spec, Value),
parse_section(Path, Value, Spec);
Expand Down Expand Up @@ -204,7 +215,7 @@ format_spec(#section{format = Format}) -> Format;
format_spec(#list{format = Format}) -> Format;
format_spec(#option{format = Format}) -> Format.

-spec format(path(), config_part(), mongoose_config_spec:format()) -> config_part().
-spec format(path(), config_part(), mongoose_config_spec:format()) -> [config_part()].
format(Path, KVs, {foreach, Format}) when is_atom(Format) ->
Keys = lists:map(fun({K, _}) -> K end, KVs),
mongoose_config_validator:validate_list(Keys, unique),
Expand Down
17 changes: 16 additions & 1 deletion src/config/mongoose_config_spec.erl
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ root() ->
General = general(),
#section{
items = #{<<"general">> => General#section{required = [<<"default_server_domain">>],
process = fun ?MODULE:process_general/1},
process = fun ?MODULE:process_general/1,
defaults = general_defaults()},
<<"listen">> => listen(),
<<"auth">> => auth(),
<<"outgoing_pools">> => outgoing_pools(),
Expand Down Expand Up @@ -226,6 +227,20 @@ general() ->
format = none
}.

general_defaults() ->
#{<<"loglevel">> => warning,
<<"hosts">> => [],
<<"host_types">> => [],
<<"registration_timeout">> => 600,
<<"language">> => <<"en">>,
<<"all_metrics_are_global">> => false,
<<"sm_backend">> => {mnesia, []},
<<"rdbms_server_type">> => generic,
<<"mongooseimctl_access_commands">> => [],
<<"routing_modules">> => ejabberd_router:default_routing_modules(),
<<"replaced_wait_timeout">> => 2000,
<<"hide_service_name">> => false}.

ctl_access_rule() ->
#section{
items = #{<<"commands">> => #list{items = #option{type = string}},
Expand Down
4 changes: 2 additions & 2 deletions src/domain/mongoose_domain_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
-spec init() -> ok | {error, term()}.
init() ->
Pairs = get_static_pairs(),
AllowedHostTypes = mongoose_config:get_opt(host_types, []),
AllowedHostTypes = mongoose_config:get_opt(host_types),
mongoose_domain_core:start(Pairs, AllowedHostTypes),
mongoose_subdomain_core:start(),
mongoose_lazy_routing:start().
Expand Down Expand Up @@ -177,7 +177,7 @@ check_domain(Domain, HostType) ->
%% Domains should be nameprepped using `jid:nameprep'
-spec get_static_pairs() -> [pair()].
get_static_pairs() ->
[{H, H} || H <- mongoose_config:get_opt(hosts, [])].
[{H, H} || H <- mongoose_config:get_opt(hosts)].

-spec register_subdomain(host_type(), subdomain_pattern(),
mongoose_packet_handler:t()) ->
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_app.erl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ start(normal, _Args) ->
mongoose_service:start(),
gen_mod:start(),
mongoose_config:start(),
mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel, warning)),
mongoose_logs:set_global_loglevel(mongoose_config:get_opt(loglevel)),
mongoose_deprecations:start(),
{ok, _} = Sup = ejabberd_sup:start_link(),
mongoose_domain_api:init(),
Expand Down
5 changes: 1 addition & 4 deletions src/ejabberd_c2s.erl
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,7 @@ do_open_session_common(Acc, JID, #state{host_type = HostType,
{established, Acc1, NewStateData}.

get_replaced_wait_timeout(HostType) ->
mongoose_config:get_opt({replaced_wait_timeout, HostType}, default_replaced_wait_timeout()).

default_replaced_wait_timeout() ->
2000.
mongoose_config:get_opt({replaced_wait_timeout, HostType}).

-spec session_established(Item :: ejabberd:xml_stream_item(),
State :: state()) -> fsm_return().
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_ctl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ process2(Args, Auth, AccessCommands) ->

-spec get_accesscommands() -> [char() | tuple()].
get_accesscommands() ->
mongoose_config:get_opt(mongooseimctl_access_commands, []).
mongoose_config:get_opt(mongooseimctl_access_commands).


%%-----------------------------
Expand Down
5 changes: 3 additions & 2 deletions src/ejabberd_router.erl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
unregister_component/1,
unregister_component/2,
unregister_components/1,
unregister_components/2
unregister_components/2,
default_routing_modules/0
]).

-export([start_link/0]).
Expand Down Expand Up @@ -509,7 +510,7 @@ code_change(_OldVsn, State, _Extra) ->
%%--------------------------------------------------------------------

routing_modules_list() ->
mongoose_config:get_opt(routing_modules, default_routing_modules()).
mongoose_config:get_opt(routing_modules).

default_routing_modules() ->
[mongoose_router_global,
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_service.erl
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,7 @@ unregister_routes(StateData) ->
ejabberd_router:unregister_components(Routes).

get_routes(#state{host=Subdomain, is_subdomain=true}) ->
Hosts = mongoose_config:get_opt(hosts, []),
Hosts = mongoose_config:get_opt(hosts),
component_routes(Subdomain, Hosts);
get_routes(#state{host=Host}) ->
[Host].
Expand Down
2 changes: 1 addition & 1 deletion src/ejabberd_sm.erl
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ node_cleanup(Acc, Node) ->
%%--------------------------------------------------------------------
-spec init(_) -> {ok, state()}.
init([]) ->
{Backend, Opts} = mongoose_config:get_opt(sm_backend, {mnesia, []}),
{Backend, Opts} = mongoose_config:get_opt(sm_backend),
ejabberd_sm_backend:init([{backend, Backend}|Opts]),

ets:new(sm_iqtable, [named_table, protected, {read_concurrency, true}]),
Expand Down
4 changes: 2 additions & 2 deletions src/metrics/mongoose_metrics.erl
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ remove_all_metrics() ->

-spec all_metrics_are_global() -> boolean().
all_metrics_are_global() ->
mongoose_config:get_opt(all_metrics_are_global, false).
mongoose_config:get_opt(all_metrics_are_global).

pick_by_all_metrics_are_global(WhenGlobal, WhenNot) ->
case all_metrics_are_global() of
true -> WhenGlobal;
_ -> WhenNot
false -> WhenNot
end.

-spec name_by_all_metrics_are_global(HostType :: mongooseim:host_type() | global,
Expand Down
2 changes: 1 addition & 1 deletion src/mod_register.erl
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ send_registration_notification(JIDBin, Domain, Body) ->
check_timeout(undefined) ->
true;
check_timeout(Source) ->
Timeout = mongoose_config:get_opt(registration_timeout, 600),
Timeout = mongoose_config:get_opt(registration_timeout),
case is_integer(Timeout) of
true ->
Priority = -(erlang:system_time(second)),
Expand Down
2 changes: 1 addition & 1 deletion src/rdbms/mongoose_rdbms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ db_engine(_HostType) ->
%% Also, this parameter should not be global, but pool-name parameterized
-spec db_type() -> mssql | generic.
db_type() ->
case mongoose_config:get_opt(rdbms_server_type, undefined) of
case mongoose_config:get_opt(rdbms_server_type) of
mssql -> mssql;
_ -> generic
end.
Expand Down
2 changes: 1 addition & 1 deletion src/rdbms/mongoose_rdbms_odbc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ map_param(Param, Mapper) ->

-spec server_type() -> atom().
server_type() ->
mongoose_config:get_opt(rdbms_server_type, generic).
mongoose_config:get_opt(rdbms_server_type).

-spec escape_binary(ServerType :: atom(), binary()) -> iodata().
escape_binary(pgsql, Bin) ->
Expand Down
8 changes: 6 additions & 2 deletions test/component_reg_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ init_per_suite(C) ->
{ok, _} = application:ensure_all_started(jid),
ok = mnesia:create_schema([node()]),
ok = mnesia:start(),
mongoose_config:set_opt(routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]),
[mongoose_config:set_opt(Key, Value) || {Key, Value} <- opts()],
meck:new(mongoose_domain_api, [no_link]),
meck:expect(mongoose_domain_api, get_host_type,
fun(_) -> {error, not_found} end),
Expand All @@ -30,9 +30,13 @@ end_per_suite(_C) ->
mnesia:stop(),
mnesia:delete_schema([node()]),
meck:unload(),
mongoose_config:unset_opt(routing_modules),
[mongoose_config:unset_opt(Key) || {Key, _Value} <- opts()],
ok.

opts() ->
[{all_metrics_are_global, false},
{routing_modules, [xmpp_router_a, xmpp_router_b, xmpp_router_c]}].

registering(_C) ->
Dom = <<"aaa.bbb.com">>,
ejabberd_router:register_component(Dom, mongoose_packet_handler:new(?MODULE)),
Expand Down
Loading

0 comments on commit e270ef1

Please sign in to comment.