Updated go module with healthcheck

This commit is contained in:
root 2023-07-22 20:17:15 -06:00
parent e86a906f28
commit 6e766a78a5
3 changed files with 25 additions and 3 deletions

View File

@ -1,9 +1,7 @@
FROM quay01.ipa.endofday.com/everythingkubernetes/golang as build-env FROM quay01.ipa.endofday.com/everythingkubernetes/golang as build-env
WORKDIR /app WORKDIR /app
COPY . . COPY /go/. .
RUN go get -d -v ./...
RUN CGO_ENABLED=0 GOOS=linux go build -o app RUN CGO_ENABLED=0 GOOS=linux go build -o app

View File

@ -7,10 +7,30 @@ import (
"os" "os"
) )
type HealthcheckResponse struct {
Healthcheck string `json:"status"`
}
type HostnameResponse struct { type HostnameResponse struct {
Hostname string `json:"hostname"` 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) { func getHostnameHandler(w http.ResponseWriter, r *http.Request) {
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
@ -34,6 +54,7 @@ func getHostnameHandler(w http.ResponseWriter, r *http.Request) {
} }
func main() { func main() {
http.HandleFunc("/healthz", getHealthcheckHandler)
http.HandleFunc("/api/v1/getHostname", getHostnameHandler) http.HandleFunc("/api/v1/getHostname", getHostnameHandler)
port := 8080 port := 8080

3
go/go.mod Normal file
View File

@ -0,0 +1,3 @@
module everythingkubernetes.info/api-example
go 1.19