From 6e766a78a581cb4e561211389d40385e5925bbc5 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jul 2023 20:17:15 -0600 Subject: [PATCH] Updated go module with healthcheck --- Dockerfile | 4 +--- go/api-example.go | 21 +++++++++++++++++++++ go/go.mod | 3 +++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 go/go.mod diff --git a/Dockerfile b/Dockerfile index 4994613..34bd7f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,7 @@ FROM quay01.ipa.endofday.com/everythingkubernetes/golang as build-env WORKDIR /app -COPY . . - -RUN go get -d -v ./... +COPY /go/. . RUN CGO_ENABLED=0 GOOS=linux go build -o app diff --git a/go/api-example.go b/go/api-example.go index 54b34f8..1810fb0 100644 --- a/go/api-example.go +++ b/go/api-example.go @@ -7,10 +7,30 @@ import ( "os" ) +type HealthcheckResponse struct { + Healthcheck string `json:"status"` +} + type HostnameResponse struct { Hostname string `json:"hostname"` } +func getHealthcheckHandler(w http.ResponseWriter, r *http.Request) { + response := HealthcheckResponse{ + Healthcheck: "healthy", + } + + jsonData, err := json.Marshal(response) + if err != nil { + http.Error(w, "Error encoding response", http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + w.Write(jsonData) +} + func getHostnameHandler(w http.ResponseWriter, r *http.Request) { hostname, err := os.Hostname() if err != nil { @@ -34,6 +54,7 @@ func getHostnameHandler(w http.ResponseWriter, r *http.Request) { } func main() { + http.HandleFunc("/healthz", getHealthcheckHandler) http.HandleFunc("/api/v1/getHostname", getHostnameHandler) port := 8080 diff --git a/go/go.mod b/go/go.mod new file mode 100644 index 0000000..86628c1 --- /dev/null +++ b/go/go.mod @@ -0,0 +1,3 @@ +module everythingkubernetes.info/api-example + +go 1.19