main.go (768B)
1 package main 2 3 import ( 4 "encoding/json" 5 "log" 6 "net/http" 7 "os" 8 ) 9 10 // HealthResponse is the JSON payload returned by the health endpoint. 11 type HealthResponse struct { 12 Status string `json:"status"` 13 } 14 15 // healthHandler writes a JSON health-check response. 16 func healthHandler(w http.ResponseWriter, r *http.Request) { 17 w.Header().Set("Content-Type", "application/json") 18 w.WriteHeader(http.StatusOK) 19 _ = json.NewEncoder(w).Encode(HealthResponse{Status: "ok"}) 20 } 21 22 func main() { 23 addr := ":8080" 24 if port := os.Getenv("PORT"); port != "" { 25 addr = ":" + port 26 } 27 28 mux := http.NewServeMux() 29 mux.HandleFunc("/health", healthHandler) 30 31 log.Printf("listening on %s", addr) 32 if err := http.ListenAndServe(addr, mux); err != nil { 33 log.Fatalf("server error: %v", err) 34 } 35 }