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.
35 lines
816 B
Elixir
35 lines
816 B
Elixir
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
|