aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesper Louis Andersen <[email protected]>2011-10-08 20:27:26 +0200
committerLoïc Hoguin <[email protected]>2011-10-10 08:18:26 +0200
commita800425131e2a4f8468722d69e2bcb50d6ae9932 (patch)
tree832b7dfd56cb593f94c5ae71225be18b156816b4
parentfd786ef2334626434737034ce32bcc8c588a7e1f (diff)
downloadcowboy-a800425131e2a4f8468722d69e2bcb50d6ae9932.tar.gz
cowboy-a800425131e2a4f8468722d69e2bcb50d6ae9932.tar.bz2
cowboy-a800425131e2a4f8468722d69e2bcb50d6ae9932.zip
Add cowboy:child_spec/6
This new exported function returns a Child Spec suitable for embedding cowboy in another applications supervisor structure. While here, implement `start_listener/6` in terms of it.
-rw-r--r--src/cowboy.erl24
1 files changed, 18 insertions, 6 deletions
diff --git a/src/cowboy.erl b/src/cowboy.erl
index 30730cb..9b07921 100644
--- a/src/cowboy.erl
+++ b/src/cowboy.erl
@@ -15,7 +15,7 @@
%% @doc Cowboy API to start and stop listeners.
-module(cowboy).
--export([start_listener/6, stop_listener/1]).
+-export([start_listener/6, stop_listener/1, child_spec/6]).
%% @doc Start a listener for the given transport and protocol.
%%
@@ -46,11 +46,8 @@
start_listener(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
when is_integer(NbAcceptors) andalso is_atom(Transport)
andalso is_atom(Protocol) ->
- supervisor:start_child(cowboy_sup,
- {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
- NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
- ]},
- permanent, 5000, supervisor, [cowboy_listener_sup]}).
+ supervisor:start_child(cowboy_sup, child_spec(Ref, NbAcceptors,
+ Transport, TransOpts, Protocol, ProtoOpts)).
%% @doc Stop a listener identified by <em>Ref</em>.
%% @todo Currently request processes aren't terminated with the listener.
@@ -62,3 +59,18 @@ stop_listener(Ref) ->
{error, Reason} ->
{error, Reason}
end.
+
+%% @doc Return a child spec suitable for embedding.
+%% When you want to embed cowboy in another application, you can use this
+%% function to create a <em>ChildSpec</em> suitable for use in a supervisor.
+%% The parameters are the same as in <em>start_listener/6</em> but rather
+%% than hooking the listener to the cowboy internal supervisor, it just returns
+%% the spec.
+-spec child_spec(any(), non_neg_integer(), module(), any(), module(), any())
+ -> supervisor:child_spec().
+child_spec(Ref, NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts)
+ when is_integer(NbAcceptors) andalso is_atom(Transport)
+ andalso is_atom(Protocol) ->
+ {{cowboy_listener_sup, Ref}, {cowboy_listener_sup, start_link, [
+ NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
+ ]}, permanent, 5000, supervisor, [cowboy_listener_sup]}.