init: go api with CI/CD
All checks were successful
Build & Deploy Go API / build (push) Successful in 33s

This commit is contained in:
joe
2026-07-19 02:43:07 +08:00
committed by Joe
parent 3c2acadc3f
commit 229009bd3c
7 changed files with 243 additions and 0 deletions

44
main.go Normal file
View File

@@ -0,0 +1,44 @@
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))
}