cowboy.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. %% Copyright (c) 2011-2013, Loïc Hoguin <essen@ninenines.eu>
  2. %%
  3. %% Permission to use, copy, modify, and/or distribute this software for any
  4. %% purpose with or without fee is hereby granted, provided that the above
  5. %% copyright notice and this permission notice appear in all copies.
  6. %%
  7. %% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. %% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. %% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  10. %% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. %% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  12. %% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  13. %% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. %% @doc Convenience API to start and stop HTTP/HTTPS listeners.
  15. -module(cowboy).
  16. -export([start_http/4]).
  17. -export([start_https/4]).
  18. -export([stop_listener/1]).
  19. -export([set_env/3]).
  20. %% @doc Start an HTTP listener.
  21. -spec start_http(any(), non_neg_integer(), any(), any()) -> {ok, pid()}.
  22. start_http(Ref, NbAcceptors, TransOpts, ProtoOpts)
  23. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  24. ranch:start_listener(Ref, NbAcceptors,
  25. ranch_tcp, TransOpts, cowboy_protocol, ProtoOpts).
  26. %% @doc Start an HTTPS listener.
  27. -spec start_https(any(), non_neg_integer(), any(), any()) -> {ok, pid()}.
  28. start_https(Ref, NbAcceptors, TransOpts, ProtoOpts)
  29. when is_integer(NbAcceptors), NbAcceptors > 0 ->
  30. ranch:start_listener(Ref, NbAcceptors,
  31. ranch_ssl, TransOpts, cowboy_protocol, ProtoOpts).
  32. %% @doc Stop a listener.
  33. -spec stop_listener(any()) -> ok.
  34. stop_listener(Ref) ->
  35. ranch:stop_listener(Ref).
  36. %% @doc Convenience function for setting an environment value.
  37. %%
  38. %% Allows you to update live an environment value used by middlewares.
  39. %% This function is primarily intended to simplify updating the dispatch
  40. %% list used for routing.
  41. -spec set_env(any(), atom(), any()) -> ok.
  42. set_env(Ref, Name, Value) ->
  43. Opts = ranch:get_protocol_options(Ref),
  44. {_, Env} = lists:keyfind(env, 1, Opts),
  45. Env2 = [{Name, Value}|lists:keydelete(Name, 1, Env)],
  46. Opts2 = lists:keyreplace(env, 1, Opts, {env, Env2}),
  47. ok = ranch:set_protocol_options(Ref, Opts2).