package main import ( "fmt" "io" "log" "net/http" "os" "time" ) func env(key, fallback string) string { if v := os.Getenv(key); v != "" { return v } return fallback } type status struct { Service string `json:"service"` OK bool `json:"ok"` Data string `json:"data"` } func fetchUsers(usersURL string) status { client := &http.Client{Timeout: 5 * time.Second} resp, err := client.Get(usersURL + "/users") if err != nil { return status{"users-api", false, fmt.Sprintf("error: %v", err)} } defer resp.Body.Close() body, _ := io.ReadAll(resp.Body) if resp.StatusCode != 200 { return status{"users-api", false, fmt.Sprintf("status %d: %s", resp.StatusCode, body)} } return status{"users-api", true, string(body)} } func main() { usersURL := env("USERS_API_URL", "http://users-api:8080") http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(200) }) http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/healthz" { w.WriteHeader(200) return } users := fetchUsers(usersURL) w.Header().Set("Content-Type", "text/html; charset=utf-8") fmt.Fprintf(w, `
Service-to-service communication test
`) cls := "ok" if !users.OK { cls = "fail" } fmt.Fprintf(w, `