feat(server): public GET /health endpoint for uptime monitors
Returns 200 with {status: ok, version, db: ok} when SQLite is reachable,
503 when the DB probe fails. Unauthenticated so external monitors can
poll without credentials.
This commit is contained in:
parent
3ce2940094
commit
579d7fc6e8
3 changed files with 52 additions and 0 deletions
35
server/lib/server_web/controllers/health_controller.ex
Normal file
35
server/lib/server_web/controllers/health_controller.ex
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
defmodule ServerWeb.HealthController do
|
||||||
|
use ServerWeb, :controller
|
||||||
|
|
||||||
|
@moduledoc """
|
||||||
|
Public health check for uptime monitors.
|
||||||
|
Returns 200 when the process is up AND SQLite is reachable, 503 otherwise.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def show(conn, _params) do
|
||||||
|
case probe_db() do
|
||||||
|
:ok ->
|
||||||
|
json(conn, %{
|
||||||
|
status: "ok",
|
||||||
|
version: Application.spec(:server, :vsn) |> to_string(),
|
||||||
|
db: "ok"
|
||||||
|
})
|
||||||
|
|
||||||
|
{:error, reason} ->
|
||||||
|
conn
|
||||||
|
|> put_status(:service_unavailable)
|
||||||
|
|> json(%{status: "degraded", db: "error", reason: inspect(reason)})
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp probe_db do
|
||||||
|
try do
|
||||||
|
case Ecto.Adapters.SQL.query(Server.Repo, "SELECT 1", []) do
|
||||||
|
{:ok, _} -> :ok
|
||||||
|
{:error, e} -> {:error, e}
|
||||||
|
end
|
||||||
|
rescue
|
||||||
|
e -> {:error, e}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -45,6 +45,12 @@ defmodule ServerWeb.Router do
|
||||||
get "/hosts/:name", HostController, :show
|
get "/hosts/:name", HostController, :show
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scope "/", ServerWeb do
|
||||||
|
pipe_through :api
|
||||||
|
|
||||||
|
get "/health", HealthController, :show
|
||||||
|
end
|
||||||
|
|
||||||
if Application.compile_env(:server, :dev_routes) do
|
if Application.compile_env(:server, :dev_routes) do
|
||||||
import Phoenix.LiveDashboard.Router
|
import Phoenix.LiveDashboard.Router
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
defmodule ServerWeb.HealthControllerTest do
|
||||||
|
use ServerWeb.ConnCase, async: true
|
||||||
|
|
||||||
|
test "GET /health returns 200 with status=ok", %{conn: conn} do
|
||||||
|
conn = get(conn, ~p"/health")
|
||||||
|
body = json_response(conn, 200)
|
||||||
|
assert body["status"] == "ok"
|
||||||
|
assert body["db"] == "ok"
|
||||||
|
assert is_binary(body["version"])
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue