aboutsummaryrefslogtreecommitdiffstats
path: root/src/cowboy.erl
diff options
context:
space:
mode:
Diffstat (limited to 'src/cowboy.erl')
-rw-r--r--src/cowboy.erl14
1 files changed, 14 insertions, 0 deletions
diff --git a/src/cowboy.erl b/src/cowboy.erl
index 79dbb71..257172d 100644
--- a/src/cowboy.erl
+++ b/src/cowboy.erl
@@ -18,6 +18,7 @@
-export([start_http/4]).
-export([start_https/4]).
-export([stop_listener/1]).
+-export([set_env/3]).
%% @doc Start an HTTP listener.
-spec start_http(any(), non_neg_integer(), any(), any()) -> {ok, pid()}.
@@ -37,3 +38,16 @@ start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
-spec stop_listener(any()) -> ok.
stop_listener(Ref) ->
ranch:stop_listener(Ref).
+
+%% @doc Convenience function for setting an environment value.
+%%
+%% Allows you to update live an environment value used by middlewares.
+%% This function is primarily intended to simplify updating the dispatch
+%% list used for routing.
+-spec set_env(any(), atom(), any()) -> ok.
+set_env(Ref, Name, Value) ->
+ Opts = ranch:get_protocol_options(Ref),
+ {_, Env} = lists:keyfind(env, 1, Opts),
+ Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)],
+ Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}),
+ ok = ranch:set_protocol_options(Ref, Opts2).