From 41f944c23743cf64d633179cbb331f7eafe74de0 Mon Sep 17 00:00:00 2001 From: Hunter Long Date: Mon, 5 Nov 2018 17:34:02 -0800 Subject: [PATCH] Docker healthcheck --- Dockerfile | 5 ++++- dev/Dockerfile-dev | 5 ++++- handlers/index.go | 10 ++++++++++ handlers/routes.go | 1 + 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2be8b66c..84bf4892 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,11 +2,14 @@ ARG VERSION FROM hunterlong/statup:base-v${VERSION} MAINTAINER "Hunter Long (https://github.com/hunterlong)" -# Locked version of Statup for 'latest' Docker tag +RUN apk --no-cache add curl jq ENV IS_DOCKER=true ENV STATUP_DIR=/app WORKDIR /app VOLUME /app EXPOSE 8080 + +HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD curl -s "http://localhost:8080/health" | jq -r -e ".online==true" + CMD ["statup"] diff --git a/dev/Dockerfile-dev b/dev/Dockerfile-dev index 81a201e8..5b4e93e0 100644 --- a/dev/Dockerfile-dev +++ b/dev/Dockerfile-dev @@ -4,7 +4,7 @@ MAINTAINER "Hunter Long (https://github.com/hunterlong)" # Statup 'test' image for running a full test using the production environment ENV DEP_VERSION=v0.5.0 -RUN apk add --no-cache libstdc++ gcc g++ make git ca-certificates linux-headers wget curl +RUN apk add --no-cache libstdc++ gcc g++ make git ca-certificates linux-headers wget curl jq RUN curl -L -s https://github.com/golang/dep/releases/download/$DEP_VERSION/dep-linux-amd64 -o /go/bin/dep && \ chmod +x /go/bin/dep @@ -25,4 +25,7 @@ RUN make install WORKDIR /app VOLUME /app EXPOSE 8080 + +HEALTHCHECK --interval=5s --timeout=5s --retries=5 CMD curl -s "http://localhost:8080/health" | jq -r -e ".online==true" + ENTRYPOINT statup diff --git a/handlers/index.go b/handlers/index.go index 3d9ead7c..e108519e 100644 --- a/handlers/index.go +++ b/handlers/index.go @@ -16,6 +16,7 @@ package handlers import ( + "encoding/json" "github.com/hunterlong/statup/core" "github.com/hunterlong/statup/types" "github.com/hunterlong/statup/utils" @@ -30,6 +31,15 @@ func indexHandler(w http.ResponseWriter, r *http.Request) { executeResponse(w, r, "index.html", core.CoreApp, nil) } +func healthCheckHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + health := map[string]interface{}{ + "services": len(core.Services()), + "online": core.Configs != nil, + } + json.NewEncoder(w).Encode(health) +} + func trayHandler(w http.ResponseWriter, r *http.Request) { executeResponse(w, r, "tray.html", core.CoreApp, nil) } diff --git a/handlers/routes.go b/handlers/routes.go index 0b37c782..eb03e800 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -119,6 +119,7 @@ func Router() *mux.Router { r.Handle("/api/notifier/{notifier}", http.HandlerFunc(apiNotifierGetHandler)).Methods("POST") r.Handle("/metrics", http.HandlerFunc(prometheusHandler)) + r.Handle("/health", http.HandlerFunc(healthCheckHandler)) r.Handle("/tray", http.HandlerFunc(trayHandler)) r.NotFoundHandler = http.HandlerFunc(error404Handler) return r