mirror of
https://github.com/ninenines/cowboy.git
synced 2025-07-14 20:30:23 +00:00
Anonymize and improve the cowboy supervision tree.
* Cowboy isn't an OTP application anymore; just a supervisor. * All processes started by Cowboy are now anonymous. * All processes related to a listener are now part of its supervision tree.
This commit is contained in:
parent
e6e5b1767f
commit
e4da6956fc
7 changed files with 78 additions and 86 deletions
42
README.md
42
README.md
|
@ -33,30 +33,40 @@ Embedding Cowboy
|
|||
Getting Started
|
||||
---------------
|
||||
|
||||
Cowboy can be started and stopped like any other application. However the
|
||||
Cowboy application do not start any listener, those must be started manually.
|
||||
Cowboy provides an anonymous listener supervisor that you can directly embed
|
||||
in your application's supervision tree.
|
||||
|
||||
A listener is a special kind of supervisor that handles a pool of acceptor
|
||||
processes. An acceptor simply accept connections and forward them to a
|
||||
protocol module, for example HTTP. You must thus define the transport and
|
||||
protocol module to use for the listener, their options and the number of
|
||||
acceptors in the pool before you can start a listener supervisor.
|
||||
processes. It also manages all its associated request processes. This allows
|
||||
you to shutdown all processes related to a listener by stopping the supervisor.
|
||||
|
||||
An acceptor simply accepts connections and forwards them to a protocol module,
|
||||
for example HTTP. You must thus define the transport and protocol module to
|
||||
use for the listener, their options and the number of acceptors in the pool
|
||||
before you can start a listener supervisor.
|
||||
|
||||
For HTTP applications the transport can be either TCP or SSL for HTTP and
|
||||
HTTPS respectively. On the other hand, the protocol is of course HTTP.
|
||||
|
||||
Code speaks more than words:
|
||||
|
||||
application:start(cowboy),
|
||||
Dispatch = [
|
||||
%% {Host, list({Path, Handler, Opts})}
|
||||
{'_', [{'_', my_handler, []}]}
|
||||
],
|
||||
%% NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
|
||||
cowboy_listener_sup:start_link(100,
|
||||
cowboy_tcp_transport, [{port, 8080}],
|
||||
cowboy_http_protocol, [{dispatch, Dispatch}]
|
||||
).
|
||||
-module(my_app).
|
||||
-behaviour(application).
|
||||
-export([start/2, stop/1]).
|
||||
|
||||
start(_Type, _Args) ->
|
||||
Dispatch = [
|
||||
%% {Host, list({Path, Handler, Opts})}
|
||||
{'_', [{'_', my_handler, []}]}
|
||||
],
|
||||
%% NbAcceptors, Transport, TransOpts, Protocol, ProtoOpts
|
||||
cowboy_listener_sup:start_link(100,
|
||||
cowboy_tcp_transport, [{port, 8080}],
|
||||
cowboy_http_protocol, [{dispatch, Dispatch}]
|
||||
).
|
||||
|
||||
stop(_State) ->
|
||||
ok.
|
||||
|
||||
You must also write the `my_handler` module to process requests. You can
|
||||
use one of the predefined handlers or write your own. An hello world HTTP
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue