From 0ca8944cf3cdc902a4b098c785dd9f523ce1a652 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment?= Date: Mon, 2 Sep 2024 16:50:59 +0200 Subject: [PATCH] feat: add healthcheck --- flake.nix | 13 ++++++++++++- main.go | 2 ++ pkg/routes/health/health.go | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 pkg/routes/health/health.go diff --git a/flake.nix b/flake.nix index 86b97eb..b278872 100644 --- a/flake.nix +++ b/flake.nix @@ -53,7 +53,18 @@ name = "image-root"; paths = [packages.app]; }; - config.Cmd = ["app"]; + config = let + healthcheck = pkgs.writeShellApplication { + name = "healthcheck"; + runtimeInputs = [pkgs.curl]; + text = '' + test "$(curl --fail localhost:8080/health)" = "OK" + ''; + }; + in { + Healthcheck.Test = ["CMD" "${pkgs.lib.getExe healthcheck}"]; + Entrypoint = ["app"]; + }; }; }; diff --git a/main.go b/main.go index 64d5172..2834d7a 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "app/pkg/app" "app/pkg/routes/blog" "app/pkg/routes/contact" + "app/pkg/routes/health" "app/pkg/routes/resume" "app/pkg/routes/root" ) @@ -12,6 +13,7 @@ func main() { serv := app.Server{Port: 8080} serv.AddRoute("GET /", root.Handler) + serv.AddRoute("GET /health", health.Handler) serv.AddRoute("GET /blog/{article...}", blog.Handler) serv.AddRoute("GET /contact", contact.Handler) serv.AddRoute("GET /resume", resume.Handler) diff --git a/pkg/routes/health/health.go b/pkg/routes/health/health.go new file mode 100644 index 0000000..9cb1f5c --- /dev/null +++ b/pkg/routes/health/health.go @@ -0,0 +1,15 @@ +package health + +import ( + "io" + "net/http" + + "app/pkg/app" + + "github.com/a-h/templ" +) + +func Handler(serv *app.Server, w http.ResponseWriter, r *http.Request) (templ.Component, error) { + io.WriteString(w, "OK") + return nil, nil +}