Files
demo-go-api/main.go
joe 229009bd3c
All checks were successful
Build & Deploy Go API / build (push) Successful in 33s
init: go api with CI/CD
2026-07-19 02:43:08 +08:00

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