Skip to content

Commit

Permalink
Make Bandit the default web server for new Phoenix apps (#5706)
Browse files Browse the repository at this point in the history
* Generator now uses Bandit by default

(Also bump bandit dep to ~> 1.0)

* Add bandit to integration_test mixfile & unlock cowboy

* Update phoenix docs to include Bandit as an equal citizen & clarify opts

* Bump swoosh to a bandit-aware version

* Update installer/lib/phx_new/generator.ex

* Bump integration test bandit lock

---------

Co-authored-by: Chris McCord <chris@chrismccord.com>
  • Loading branch information
mtrudel and chrismccord committed Feb 2, 2024
1 parent e42453b commit a4a761c
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 37 deletions.
2 changes: 1 addition & 1 deletion guides/howto/using_ssl.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Without the `otp_app:` key, we need to provide absolute paths to the files where
Path.expand("../../../some/path/to/ssl/key.pem", __DIR__)
```

The options under the `https:` key are passed to the Plug adapter, typically `Plug.Cowboy`, which in turn uses `Plug.SSL` to select the TLS socket options. Please refer to the documentation for [Plug.SSL.configure/1](https://hexdocs.pm/plug/Plug.SSL.html#configure/1) for more information on the available options and their defaults. The [Plug HTTPS Guide](https://hexdocs.pm/plug/https.html) and the [Erlang/OTP ssl](https://www.erlang.org/doc/man/ssl.html) documentation also provide valuable information.
The options under the `https:` key are passed to the Plug adapter, typically `Bandit`, which in turn uses `Plug.SSL` to select the TLS socket options. Please refer to the documentation for [Plug.SSL.configure/1](https://hexdocs.pm/plug/Plug.SSL.html#configure/1) for more information on the available options and their defaults. The [Plug HTTPS Guide](https://hexdocs.pm/plug/https.html) and the [Erlang/OTP ssl](https://www.erlang.org/doc/man/ssl.html) documentation also provide valuable information.

## SSL in Development

Expand Down
2 changes: 1 addition & 1 deletion installer/lib/mix/tasks/phx.new.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ defmodule Mix.Tasks.Phx.New do
* `bandit` - via https://github.com/mtrudel/bandit
Please check the adapter docs for more information
and requirements. Defaults to "cowboy".
and requirements. Defaults to "bandit".
* `--no-assets` - equivalent to `--no-esbuild` and `--no-tailwind`
Expand Down
5 changes: 2 additions & 3 deletions installer/lib/phx_new/generator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ defmodule Phx.New.Generator do

def put_binding(%Project{opts: opts} = project) do
db = Keyword.get(opts, :database, "postgres")
web_adapter = Keyword.get(opts, :adapter, "cowboy")
web_adapter = Keyword.get(opts, :adapter, "bandit")
ecto = Keyword.get(opts, :ecto, true)
html = Keyword.get(opts, :html, true)
live = html && Keyword.get(opts, :live, true)
Expand Down Expand Up @@ -304,8 +304,7 @@ defmodule Phx.New.Generator do
end

defp get_web_adapter("cowboy"), do: {:plug_cowboy, "~> 2.7", Phoenix.Endpoint.Cowboy2Adapter}
# TODO bump bandit to 1.0 when it's released
defp get_web_adapter("bandit"), do: {:bandit, ">= 0.0.0", Bandit.PhoenixAdapter}
defp get_web_adapter("bandit"), do: {:bandit, "~> 1.2", Bandit.PhoenixAdapter}
defp get_web_adapter(other), do: Mix.raise("Unknown web adapter #{inspect(other)}")

defp fs_db_config(app, module) do
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_single/config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ if config_env() == :prod do
http: [
# Enable IPv6 and bind on all interfaces.
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# See the documentation on https://hexdocs.pm/bandit/Bandit.html#t:options/0
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_single/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule <%= @app_module %>.MixProject do
app: false,
compile: false,
depth: 1},<% end %><%= if @mailer do %>
{:swoosh, "~> 1.3"},
{:swoosh, "~> 1.5"},
{:finch, "~> 0.13"},<% end %>
{:telemetry_metrics, "~> 0.6"},
{:telemetry_poller, "~> 1.0"},<%= if @gettext do %>
Expand Down
2 changes: 1 addition & 1 deletion installer/templates/phx_umbrella/apps/app_name/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule <%= @app_module %>.MixProject do
{:ecto_sql, "~> 3.10"},
{:<%= @adapter_app %>, ">= 0.0.0"},
{:jason, "~> 1.2"}<% end %><%= if @mailer do %>,
{:swoosh, "~> 1.3"},
{:swoosh, "~> 1.5"},
{:finch, "~> 0.13"}<% end %>
]
end
Expand Down
4 changes: 2 additions & 2 deletions installer/test/phx_new_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ defmodule Mix.Tasks.Phx.NewTest do

# Mailer
assert_file("phx_blog/mix.exs", fn file ->
assert file =~ "{:swoosh, \"~> 1.3\"}"
assert file =~ "{:swoosh, \"~> 1.5\"}"
assert file =~ "{:finch, \"~> 0.13\"}"
end)

Expand Down Expand Up @@ -417,7 +417,7 @@ defmodule Mix.Tasks.Phx.NewTest do

# No mailer or emails
assert_file("phx_blog/mix.exs", fn file ->
refute file =~ "{:swoosh, \"~> 1.3\"}"
refute file =~ "{:swoosh, \"~> 1.5\"}"
refute file =~ "{:finch, \"~> 0.13\"}"
end)

Expand Down
16 changes: 8 additions & 8 deletions installer/test/phx_new_umbrella_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
assert file =~ "{:phoenix,"
assert file =~ "{:phoenix_live_view,"
assert file =~ "{:gettext,"
assert file =~ "{:plug_cowboy,"
assert file =~ "{:bandit,"
end)

# app deps
Expand Down Expand Up @@ -277,7 +277,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do

# Mailer
assert_file(app_path(@app, "mix.exs"), fn file ->
assert file =~ "{:swoosh, \"~> 1.3\"}"
assert file =~ "{:swoosh, \"~> 1.5\"}"
assert file =~ "{:finch, \"~> 0.13\"}"
end)

Expand Down Expand Up @@ -413,7 +413,7 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do

# Without mailer
assert_file(web_path(@app, "mix.exs"), fn file ->
refute file =~ "{:swoosh, \"~> 1.3\"}"
refute file =~ "{:swoosh, \"~> 1.5\"}"
refute file =~ "{:finch, \"~> 0.13\"}"
end)

Expand Down Expand Up @@ -709,14 +709,14 @@ defmodule Mix.Tasks.Phx.New.UmbrellaTest do
end)
end

test "new with bandit web adapter" do
in_tmp("new with bandit web adapter", fn ->
test "new with cowboy web adapter" do
in_tmp("new with cowboy web adapter", fn ->
app = "custom_path"
project_path = Path.join(File.cwd!(), app)
Mix.Tasks.Phx.New.run([project_path, "--umbrella", "--adapter", "bandit"])
assert_file(web_path(app, "mix.exs"), ":bandit")
Mix.Tasks.Phx.New.run([project_path, "--umbrella", "--adapter", "cowboy"])
assert_file(web_path(app, "mix.exs"), ":plug_cowboy")

assert_file(root_path(app, "config/config.exs"), "adapter: Bandit.PhoenixAdapter")
assert_file(root_path(app, "config/config.exs"), "adapter: Phoenix.Endpoint.Cowboy2Adapter")
end)
end

Expand Down
2 changes: 1 addition & 1 deletion integration_test/mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Phoenix.Integration.MixProject do
{:gettext, "~> 0.20"},
{:jason, "~> 1.2"},
{:swoosh, "~> 1.3"},
{:plug_cowboy, "~> 2.7"},
{:bandit, "~> 1.0"},
{:bcrypt_elixir, "~> 3.0"},
{:argon2_elixir, "~> 3.0"},
{:pbkdf2_elixir, "~> 2.0"},
Expand Down

0 comments on commit a4a761c

Please sign in to comment.