Simple health check for Phoenix apps

almirsarajcic

almirsarajcic

Created 6 months ago

When deploying a Phoenix app, you want to make sure your app works: the server is started, and the app is connected to the database.

Here’s a simple health check that ensures that your app can query the database and other nodes in the cluster. If you aren’t clustering (yet), you can keep the same code without any changes.

def index(conn, _params) do
  # Return status 500 if unable to connect to DB
  SQL.query!(MyApp.Repo, "SELECT 1")

  {:ok, hostname} = :inet.gethostname()

  json(conn, %{
    connected_to: Node.list(),
    hostname: to_string(hostname),
    node: Node.self(),
    status: :ok
  })
end

This is a part of the code generated by optimum_gen_infra.