From c89511d24d74fc2bb7a2d6ed4642c556df8e6c44 Mon Sep 17 00:00:00 2001 From: Kieran Eglin Date: Tue, 2 Apr 2024 11:47:25 -0700 Subject: [PATCH] added a healthcheck --- lib/pinchflat_web/controllers/health_controller.ex | 9 +++++++++ lib/pinchflat_web/router.ex | 7 +++++++ selfhosted.Dockerfile | 3 +++ .../controllers/health_controller_test.exs | 10 ++++++++++ 4 files changed, 29 insertions(+) create mode 100644 lib/pinchflat_web/controllers/health_controller.ex create mode 100644 test/pinchflat_web/controllers/health_controller_test.exs diff --git a/lib/pinchflat_web/controllers/health_controller.ex b/lib/pinchflat_web/controllers/health_controller.ex new file mode 100644 index 0000000..3125c96 --- /dev/null +++ b/lib/pinchflat_web/controllers/health_controller.ex @@ -0,0 +1,9 @@ +defmodule PinchflatWeb.HealthController do + use PinchflatWeb, :controller + + def check(conn, _params) do + conn + |> put_status(:ok) + |> json(%{status: "ok"}) + end +end diff --git a/lib/pinchflat_web/router.ex b/lib/pinchflat_web/router.ex index d59d401..202f460 100644 --- a/lib/pinchflat_web/router.ex +++ b/lib/pinchflat_web/router.ex @@ -47,6 +47,13 @@ defmodule PinchflatWeb.Router do get "/media/:uuid/stream", MediaItems.MediaItemController, :stream end + # No auth or CSRF protection for the health check endpoint + scope "/", PinchflatWeb do + pipe_through :api + + get "/healthcheck", HealthController, :check + end + scope "/dev" do pipe_through :browser diff --git a/selfhosted.Dockerfile b/selfhosted.Dockerfile index 57a329f..5f30517 100644 --- a/selfhosted.Dockerfile +++ b/selfhosted.Dockerfile @@ -113,5 +113,8 @@ COPY --from=builder /app/_build/${MIX_ENV}/rel/pinchflat ./ # above and adding an entrypoint. See https://github.com/krallin/tini for details # ENTRYPOINT ["/tini", "--"] +HEALTHCHECK --interval=120s --start-period=10s \ + CMD curl --fail http://localhost:${PORT}/healthcheck || exit 1 + # Start the app CMD ["/app/bin/docker_start"] diff --git a/test/pinchflat_web/controllers/health_controller_test.exs b/test/pinchflat_web/controllers/health_controller_test.exs new file mode 100644 index 0000000..fb1a298 --- /dev/null +++ b/test/pinchflat_web/controllers/health_controller_test.exs @@ -0,0 +1,10 @@ +defmodule PinchflatWeb.HealthControllerTest do + use PinchflatWeb.ConnCase + + describe "GET /healthcheck" do + test "returns ok", %{conn: conn} do + conn = get(conn, "/healthcheck") + assert json_response(conn, 200) == %{"status" => "ok"} + end + end +end