Skip to content

Commit

Permalink
feat: use Phoenix with Bandit HTTP server
Browse files Browse the repository at this point in the history
  • Loading branch information
jyeshe committed Feb 10, 2025
1 parent 14e4074 commit e8446bf
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
erlang 26.2.4
elixir 1.16.2-otp-26
erlang 26.2.5
elixir 1.17.3-otp-26
12 changes: 11 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,24 @@ config :ae_mdw,
config :ae_mdw, AeMdwWeb.Endpoint,
url: [host: "localhost"],
secret_key_base: "kATf71kudJsgA1dgCQKcmgelicqJHG8EID8rwROwJHpWHb53EdzW7YDclJZ8mxLP",
render_errors: [view: AeMdwWeb.ErrorView, accepts: ~w(html json)],
adapter: Bandit.PhoenixAdapter,
render_errors: [
formats: [json: UpdocsWeb.ErrorJSON],
layout: false
],
pubsub_server: AeMdw.PubSub,
live_view: [signing_salt: "Oy680JAN"],
code_reloader: false,
watchers: [],
check_origin: false

config :ae_mdw, AeMdwWeb.WebsocketEndpoint,
url: [host: "localhost"],
adapter: Bandit.PhoenixAdapter,
render_errors: [
formats: [json: UpdocsWeb.ErrorJSON],
layout: false
],
code_reloader: false,
watchers: [],
check_origin: false
Expand Down
24 changes: 13 additions & 11 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,6 @@ config :ae_mdw, AeMdw.APM.TelemetryPoller, period: period
config :ae_mdw, :enable_v3?, true
config :ae_mdw, memstore_lifetime_secs: 60

ip =
if System.get_env("DISABLE_IPV6", "false") in ["true", "1"] do
{0, 0, 0, 0}
else
{0, 0, 0, 0, 0, 0, 0, 0}
end

log_level =
case System.get_env("LOG_LEVEL") do
"none" -> :none
Expand All @@ -47,26 +40,35 @@ logger_backends =

# Endpoint
if env != :test do
ip =
if System.get_env("DISABLE_IPV6") in ["true", "1"] do
{0, 0, 0, 0}
else
{0, 0, 0, 0, 0, 0, 0, 0}
end

port = String.to_integer(System.get_env("PORT") || "4000")
protocol_opts = [max_request_line_length: 1_024, max_skip_body_length: 1_024]
protocol_opts = [max_request_line_length: 1_024]

config :ae_mdw, AeMdwWeb.Endpoint,
http: [
ip: ip,
port: port,
protocol_options: protocol_opts
http_1_options: protocol_opts,
http_2_options: [enabled: true]
],
debug_errors: env == :dev,
cache_static_manifest: "priv/static/cache_manifest.json"

ws_port = String.to_integer(System.get_env("WS_PORT") || "4001")
timeout_opts = [inactivity_timeout: 30 * 60_000, idle_timeout: 30 * 60_000]

config :ae_mdw, AeMdwWeb.WebsocketEndpoint,
http: [
ip: ip,
port: ws_port,
protocol_options: protocol_opts ++ timeout_opts
http_1_options: protocol_opts,
http_2_options: [enabled: true],
thousand_island_options: [read_timeout: 30 * 60_000]
]

config :logger,
Expand Down
21 changes: 21 additions & 0 deletions lib/ae_mdw_web/controllers/error_json.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
defmodule AeMdwWeb.ErrorJSON do
@moduledoc """
This module is invoked by your endpoint in case of errors on JSON requests.
See config/config.exs.
"""

# If you want to customize a particular status code,
# you may add your own clauses, such as:
#
# def render("500.json", _assigns) do
# %{errors: %{detail: "Internal Server Error"}}
# end

# By default, Phoenix returns the status message from
# the template name. For example, "404.json" becomes
# "Not Found".
def render(template, _assigns) do

Check warning on line 18 in lib/ae_mdw_web/controllers/error_json.ex

View workflow job for this annotation

GitHub Actions / Automated linting

Functions should have a @SPEC type specification.
%{errors: %{detail: Phoenix.Controller.status_message_from_template(template)}}
end
end
11 changes: 0 additions & 11 deletions lib/ae_mdw_web/views/error_view.ex

This file was deleted.

14 changes: 8 additions & 6 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,19 @@ defmodule AeMdw.MixProject do
{:ex2ms, "~> 1.6.0"},
{:logger_file_backend, "~> 0.0.11"},
{:logger_json, "~> 5.0"},
{:phoenix, "~> 1.6.12"},
{:phoenix_html, "~> 3.0"},
{:phoenix, "~> 1.7.18"},
{:phoenix_html, "~> 4.1"},
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 1.0.0"},
{:floki, ">= 0.30.0", only: :test},
{:phoenix_live_dashboard, "~> 0.8.3"},
{:bandit, "~> 1.5"},
{:cors_plug, "~> 3.0"},
{:telemetry, "~> 1.0"},
{:telemetry_poller, "~> 1.0"},
{:telemetry_metrics, "~> 0.6"},
{:telemetry_metrics_statsd, "~> 0.6"},
{:phoenix_live_view, "~> 0.18"},
{:phoenix_live_dashboard, "~> 0.7"},
{:esbuild, "~> 0.3", runtime: Mix.env() == :dev},
{:plug, "~> 1.13"},
{:cors_plug, "~> 2.0"},
{:gettext, "~> 0.20"},
{:jason, "~> 1.4"},
{:jsx, "~> 2.8.0"},
Expand Down
Loading

0 comments on commit e8446bf

Please sign in to comment.