From 876602fb4a1446e726b739470fd34e304ab8d9b5 Mon Sep 17 00:00:00 2001 From: Alex Mickelson Date: Mon, 9 Mar 2026 15:38:49 -0600 Subject: [PATCH] healthcheck improvements --- .../controllers/health_controller.ex | 7 ------- lib/elixir_ai_web/endpoint.ex | 3 +++ lib/elixir_ai_web/plugs/health_check.ex | 17 +++++++++++++++++ lib/elixir_ai_web/router.ex | 6 ------ 4 files changed, 20 insertions(+), 13 deletions(-) delete mode 100644 lib/elixir_ai_web/controllers/health_controller.ex create mode 100644 lib/elixir_ai_web/plugs/health_check.ex diff --git a/lib/elixir_ai_web/controllers/health_controller.ex b/lib/elixir_ai_web/controllers/health_controller.ex deleted file mode 100644 index 52bc43e..0000000 --- a/lib/elixir_ai_web/controllers/health_controller.ex +++ /dev/null @@ -1,7 +0,0 @@ -defmodule ElixirAiWeb.HealthController do - use ElixirAiWeb, :controller - - def index(conn, _params) do - json(conn, %{status: "ok"}) - end -end diff --git a/lib/elixir_ai_web/endpoint.ex b/lib/elixir_ai_web/endpoint.ex index f5bd0c5..561273d 100644 --- a/lib/elixir_ai_web/endpoint.ex +++ b/lib/elixir_ai_web/endpoint.ex @@ -1,6 +1,9 @@ defmodule ElixirAiWeb.Endpoint do use Phoenix.Endpoint, otp_app: :elixir_ai + # Health check plug - runs before everything else to avoid logging and unnecessary processing + plug ElixirAiWeb.Plugs.HealthCheck + # The session will be stored in the cookie and signed, # this means its contents can be read but not tampered with. # Set :encryption_salt if you would also like to encrypt it. diff --git a/lib/elixir_ai_web/plugs/health_check.ex b/lib/elixir_ai_web/plugs/health_check.ex new file mode 100644 index 0000000..067fb3e --- /dev/null +++ b/lib/elixir_ai_web/plugs/health_check.ex @@ -0,0 +1,17 @@ +defmodule ElixirAiWeb.Plugs.HealthCheck do + @moduledoc """ + A lightweight health check plug that responds before hitting the router. + This avoids unnecessary processing and logging for health check requests. + """ + import Plug.Conn + + def init(opts), do: opts + + def call(%Plug.Conn{request_path: "/health"} = conn, _opts) do + conn + |> send_resp(200, ~s({"status":"ok"})) + |> halt() + end + + def call(conn, _opts), do: conn +end diff --git a/lib/elixir_ai_web/router.ex b/lib/elixir_ai_web/router.ex index 22a4d8c..d091cb3 100644 --- a/lib/elixir_ai_web/router.ex +++ b/lib/elixir_ai_web/router.ex @@ -14,12 +14,6 @@ defmodule ElixirAiWeb.Router do plug :accepts, ["json"] end - scope "/", ElixirAiWeb, log: false do - pipe_through :api - - get "/health", HealthController, :index - end - scope "/", ElixirAiWeb do pipe_through :browser