cowboy.start_tls.asciidoc 2.7 KB

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