diff --git a/lib/plausible_web/controllers/api/external_controller.ex b/lib/plausible_web/controllers/api/external_controller.ex index c9dcc5c00..3924a83da 100644 --- a/lib/plausible_web/controllers/api/external_controller.ex +++ b/lib/plausible_web/controllers/api/external_controller.ex @@ -28,6 +28,29 @@ defmodule PlausibleWeb.Api.ExternalController do send_resp(conn, 200, "") end + def health(conn, _params) do + postgres_health = case Ecto.Adapters.SQL.query(Plausible.Repo, "SELECT 1", []) do + {:ok, _} -> "ok" + e -> "error: #{inspect e}" + end + + clickhouse_health = case Clickhousex.query(:clickhouse, "SELECT 1", []) do + {:ok, _} -> "ok" + e -> "error: #{inspect e}" + end + + status = case {postgres_health, clickhouse_health} do + {"ok", "ok"} ->200 + _ -> 500 + end + + put_status(conn, status) + |> json(%{ + postgres: postgres_health, + clickhouse: clickhouse_health, + }) + end + defp create_event(conn, params) do uri = params["url"] && URI.parse(params["url"]) user_agent = Plug.Conn.get_req_header(conn, "user-agent") |> List.first() diff --git a/lib/plausible_web/router.ex b/lib/plausible_web/router.ex index 529af527e..a48eb00a3 100644 --- a/lib/plausible_web/router.ex +++ b/lib/plausible_web/router.ex @@ -59,6 +59,7 @@ defmodule PlausibleWeb.Router do post "/event", Api.ExternalController, :event get "/error", Api.ExternalController, :error + get "/health", Api.ExternalController, :health post "/paddle/webhook", Api.PaddleController, :webhook diff --git a/test/plausible_web/controllers/api/external_controller_test.exs b/test/plausible_web/controllers/api/external_controller_test.exs index 8f7a705ed..a9c71bea5 100644 --- a/test/plausible_web/controllers/api/external_controller_test.exs +++ b/test/plausible_web/controllers/api/external_controller_test.exs @@ -410,4 +410,12 @@ defmodule PlausibleWeb.Api.ExternalControllerTest do assert response(conn, 400) == "" end + + describe "GET /api/health" do + test "returns 200 OK", %{conn: conn} do + conn = get(conn, "/api/health") + + assert conn.status == 200 + end + end end