Packages
bandit
0.7.0
1.12.0
1.11.1
1.11.0
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
retired
1.9.0
1.8.0
1.7.0
1.6.11
1.6.10
1.6.9
1.6.8
1.6.7
1.6.6
1.6.5
1.6.4
1.6.3
1.6.2
1.6.1
1.6.0
1.5.7
1.5.6
1.5.5
1.5.4
1.5.3
1.5.2
1.5.1
1.5.0
1.4.2
1.4.1
1.4.0
1.3.0
1.2.3
1.2.2
1.2.1
1.2.0
1.1.3
1.1.2
1.1.1
1.1.0
1.0.0
1.0.0-pre.18
1.0.0-pre.17
1.0.0-pre.16
1.0.0-pre.15
1.0.0-pre.14
1.0.0-pre.13
1.0.0-pre.12
1.0.0-pre.11
1.0.0-pre.10
1.0.0-pre.9
1.0.0-pre.8
1.0.0-pre.7
1.0.0-pre.6
1.0.0-pre.5
1.0.0-pre.4
1.0.0-pre.3
1.0.0-pre.2
1.0.0-pre.1
0.7.7
0.7.6
0.7.5
0.7.4
0.7.3
0.7.2
0.7.1
0.7.0
0.6.11
0.6.10
0.6.9
0.6.8
0.6.7
0.6.6
0.6.5
0.6.4
0.6.3
0.6.2
0.6.1
0.6.0
0.5.11
0.5.10
0.5.9
0.5.8
0.5.7
0.5.6
0.5.5
0.5.4
0.5.3
0.5.2
0.5.1
0.5.0
0.4.10
0.4.9
0.4.8
0.4.7
0.4.6
0.4.5
0.4.4
0.4.3
0.4.2
0.4.1
0.4.0
0.3.9
0.3.8
0.3.7
0.3.6
0.3.5
0.3.4
0.3.3
0.3.2
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
A pure-Elixir HTTP server built for Plug & WebSock apps
Security advisory:
This version has known vulnerabilities.
View advisories
Current section
Files
Jump to
Current section
Files
lib/bandit/phoenix_adapter.ex
defmodule Bandit.PhoenixAdapter do
@moduledoc """
A Bandit adapter for Phoenix.
WebSocket support requires a version of Phoenix with Plug upgrade support, which is available
as part of Phoenix 1.7 and later. This module will work fine on earlier versions of Phoenix,
just without WebSocket support.
To use this adapter, your project will need to include Bandit as a dependency; see
https://hex.pm/bandit for details on the currently supported version of Bandit to include. Once
Bandit is included as a dependency of your Phoenix project, add the following to your endpoint
configuration in `config/config.exs`:
```
config :your_app, YourAppWeb.Endpoint,
adapter: Bandit.PhoenixAdapter
```
## Endpoint configuration
Configuring Bandit within your Phoenix app is done in largely the same way as configuration for
Cowboy works. For the most part, your existing configuration within your `/config/*.exs` files
will work unchanged, although some of the more exotic options are different. Bandit supports the
following parameters within the `:http` and `:https` parameters:
* `:http`: the configuration for the HTTP server. Accepts the following options:
* `port`: The port to run on. Defaults to 4000. Note that if a Unix domain socket is
specified in the `ip` option, the value of `port` **must** be `0`.
* `ip`: The address to bind to. Can be specified as a 4-element tuple such as `{127, 0, 0, 1}`
for IPv4 addresses, an 8-element tuple for IPv6 addresses, or using `{:local, path}` to bind
to a Unix domain socket. Defaults to the Bandit default of `{0, 0, 0, 0, 0, 0, 0, 0}`.
* `transport_options`: Any valid value from `ThousandIsland.Transports.TCP`
Defaults to `false`, which will cause Bandit to not start an HTTP server.
* `:https`: the configuration for the HTTPS server. Accepts the following options:
* `port`: The port to run on. Defaults to 4040. Note that if a Unix domain socket is
specified in the `ip` option, the value of `port` **must** be `0`.
* `ip`: The address to bind to. Can be specified as a 4-element tuple such as `{127, 0, 0, 1}`
for IPv4 addresses, an 8-element tuple for IPv6 addresses, or using `{:local, path}` to bind
to a Unix domain socket. Defaults to the Bandit default of `{0, 0, 0, 0, 0, 0, 0, 0}`.
* `transport_options`: Any valid value from `ThousandIsland.Transports.SSL`
Defaults to `false`, which will cause Bandit to not start an HTTPS server.
"""
@doc false
def child_specs(endpoint, config) do
for {scheme, default_port} <- [http: 4000, https: 4040], opts = config[scheme] do
port = Keyword.get(opts, :port, default_port)
ip_opt = Keyword.take(opts, [:ip])
transport_options = Keyword.get(opts, :transport_options, [])
opts = [port: port_to_integer(port), transport_options: ip_opt ++ transport_options]
plug =
if config[:code_reloader] &&
Code.ensure_loaded?(Phoenix.Endpoint.SyncCodeReloadPlug) &&
function_exported?(Phoenix.Endpoint.SyncCodeReloadPlug, :call, 2) do
{Phoenix.Endpoint.SyncCodeReloadPlug, {endpoint, []}}
else
endpoint
end
[plug: plug, display_plug: endpoint, scheme: scheme, options: opts]
|> Bandit.child_spec()
|> Supervisor.child_spec(id: {endpoint, scheme})
end
end
defp port_to_integer(port) when is_binary(port), do: String.to_integer(port)
defp port_to_integer(port) when is_integer(port), do: port
end