Initial submission of API demo

This commit is contained in:
randal 2023-07-22 14:45:11 -06:00
parent 13ee3bbf94
commit e86a906f28
2 changed files with 59 additions and 0 deletions

13
Dockerfile Normal file
View File

@ -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"]

46
go/api-example.go Normal file
View File

@ -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)
}
}