Debian Codegraph @

doc/src/manual/cowboy.start_tls.asciidoc

raw
1 = cowboy:start_tls(3)
2
3 == Name
4
5 cowboy:start_tls - Listen for connections using TLS
6
7 == Description
8
9 [source,erlang]
10 ----
11 start_tls(Name :: ranch:ref(),
12 TransportOpts :: ranch_ssl:opts(),
13 ProtocolOpts :: opts())
14 -> {ok, ListenerPid :: pid()}
15 | {error, any()}
16 ----
17
18 Start listening for connections over a secure TLS channel.
19
20 Both HTTP/1.1 and HTTP/2 are supported on this listener.
21 The ALPN TLS extension must be used to initiate an HTTP/2
22 connection.
23
24 == Arguments
25
26 Name::
27
28 The listener name is used to refer to this listener in
29 future calls, for example when stopping it or when
30 updating the routes defined.
31 +
32 It can be any Erlang term. An atom is generally good enough,
33 for example `api`, `my_app_clear` or `my_app_tls`.
34
35 TransportOpts::
36
37 The transport options are where the TCP options, including
38 the listener's port number, are defined. They also contain
39 the TLS options, like the server's certificate. Transport options
40 are provided as a list of keys and values, for example
41 `[{port, 8443}, {certfile, "path/to/cert.pem"}]`.
42 +
43 The available options are documented in the
44 link:man:ranch_ssl(3)[ranch_ssl(3)] manual.
45
46 ProtocolOpts::
47
48 The protocol options are in a map containing all the options for
49 the different protocols that may be involved when connecting
50 to the listener, including HTTP/1.1 and HTTP/2.
51 +
52 The HTTP/1.1 options are documented in the
53 link:man:cowboy_http(3)[cowboy_http(3)] manual;
54 and the HTTP/2 options in
55 link:man:cowboy_http2(3)[cowboy_http2(3)]. Stream handlers
56 such as link:man:cowboy_stream_h(3)[cowboy_stream_h(3)]
57 (which is enabled by default) may also define options.
58
59 == Return value
60
61 An ok tuple is returned on success. It contains the pid of
62 the top-level supervisor for the listener.
63
64 An error tuple is returned on error. The error reason may
65 be any Erlang term.
66
67 A common error is `eaddrinuse`. It indicates that the port
68 configured for Cowboy is already in use.
69
70 == Changelog
71
72 * *2.0*: HTTP/2 support added.
73 * *2.0*: Function introduced. Replaces `cowboy:start_https/4`.
74
75 == Examples
76
77 .Start a listener
78 [source,erlang]
79 ----
80 Dispatch = cowboy_router:compile([
81 {'_', [
82 {"/", toppage_h, []}
83 ]}
84 ]),
85
86 {ok, _} = cowboy:start_tls(example, [
87 {port, 8443},
88 {certfile, "path/to/cert.pem"}
89 ], #{
90 env => #{dispatch => Dispatch}
91 }).
92 ----
93
94 .Start a listener on a random port
95 [source,erlang]
96 ----
97 Name = example,
98
99 {ok, _} = cowboy:start_tls(Name, [
100 {certfile, "path/to/cert.pem"}
101 ], #{
102 env => #{dispatch => Dispatch}
103 }),
104
105 Port = ranch:get_port(Name).
106 ----
107
108 == See also
109
110 link:man:cowboy(3)[cowboy(3)],
111 link:man:cowboy:start_clear(3)[cowboy:start_clear(3)],
112 link:man:cowboy:stop_listener(3)[cowboy:stop_listener(3)],
113 link:man:ranch(3)[ranch(3)]