From e86a906f28864ea7a45426f89ab1959d7f7aff01 Mon Sep 17 00:00:00 2001 From: "Randal S. Harisch" Date: Sat, 22 Jul 2023 14:45:11 -0600 Subject: [PATCH] Initial submission of API demo --- Dockerfile | 13 +++++++++++++ go/api-example.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 Dockerfile create mode 100644 go/api-example.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4994613 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,13 @@ +FROM quay01.ipa.endofday.com/everythingkubernetes/golang as build-env + +WORKDIR /app +COPY . . + +RUN go get -d -v ./... + +RUN CGO_ENABLED=0 GOOS=linux go build -o app + +FROM gcr.io/distroless/base +COPY --from=build-env /app/app /app +CMD ["/app"] + diff --git a/go/api-example.go b/go/api-example.go new file mode 100644 index 0000000..54b34f8 --- /dev/null +++ b/go/api-example.go @@ -0,0 +1,46 @@ +package main + +import ( + "encoding/json" + "fmt" + "net/http" + "os" +) + +type HostnameResponse struct { + Hostname string `json:"hostname"` +} + +func getHostnameHandler(w http.ResponseWriter, r *http.Request) { + hostname, err := os.Hostname() + if err != nil { + http.Error(w, "Error getting hostname", http.StatusInternalServerError) + return + } + + response := HostnameResponse{ + Hostname: hostname, + } + + 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 main() { + http.HandleFunc("/api/v1/getHostname", getHostnameHandler) + + port := 8080 + fmt.Printf("Server listening on port %d...\n", port) + err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil) + if err != nil { + fmt.Println("Error starting server:", err) + } +} +