45 lines
923 B
Go
45 lines
923 B
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
type Response struct {
|
|
Service string `json:"service"`
|
|
Message string `json:"message"`
|
|
Time string `json:"time"`
|
|
Hostname string `json:"hostname"`
|
|
Version string `json:"version"`
|
|
}
|
|
|
|
func main() {
|
|
port := os.Getenv("PORT")
|
|
if port == "" {
|
|
port = "8080"
|
|
}
|
|
|
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
hostname, _ := os.Hostname()
|
|
resp := Response{
|
|
Service: "demo-go-api",
|
|
Message: "Hello from Go API!",
|
|
Time: time.Now().Format(time.RFC3339),
|
|
Hostname: hostname,
|
|
Version: os.Getenv("VERSION"),
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(resp)
|
|
})
|
|
|
|
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
|
|
w.Write([]byte("ok"))
|
|
})
|
|
|
|
log.Printf("Listening on :%s", port)
|
|
log.Fatal(http.ListenAndServe(":"+port, nil))
|
|
}
|