Skip to content

Commit

Permalink
/_health endpoint (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
skyqrose authored May 13, 2024
1 parent 5a394b4 commit f1ece34
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
17 changes: 17 additions & 0 deletions lib/orbit_web/controllers/db_health_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
defmodule OrbitWeb.HealthDbController do
@moduledoc """
Controller that runs a database query to check whether the webserver can connect to the database.
"""
use OrbitWeb, :controller
import Ecto.Query

alias Orbit.Repo

def index(conn, _params) do
count = Repo.one(from(migration in "schema_migrations", select: count(migration)))

conn
|> put_status(200)
|> text("#{count}")
end
end
11 changes: 11 additions & 0 deletions lib/orbit_web/controllers/health_controller.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
defmodule OrbitWeb.HealthController do
@moduledoc """
Simple controller to return 200 OK when the website is healthy.
"""
use OrbitWeb, :controller

def index(conn, _params) do
conn
|> send_resp(200, "")
end
end
9 changes: 5 additions & 4 deletions lib/orbit_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ defmodule OrbitWeb.Router do
get "/", PageController, :home
end

# Other scopes may use custom stacks.
# scope "/api", OrbitWeb do
# pipe_through :api
# end
scope "/", OrbitWeb do
# no pipe
get "/_health", HealthController, :index
get "/_health_db", HealthDbController, :index
end

# Enable LiveDashboard in development
if Application.compile_env(:orbit, :dev_routes) do
Expand Down
8 changes: 8 additions & 0 deletions test/orbit_web/controllers/health_controller_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
defmodule OrbitWeb.HealthControllerTest do
use OrbitWeb.ConnCase

test "GET /_health", %{conn: conn} do
conn = get(conn, ~p"/_health")
assert response(conn, 200)
end
end

0 comments on commit f1ece34

Please sign in to comment.