package main import ( "encoding/json" "log" "net/http" "strings" "time" ) func main() { sseBroker := NewSSEBroker() checker := NewHealthChecker(sseBroker) go func() { ticker := time.NewTicker(30 * time.Second) defer ticker.Stop() for { checker.CheckAll() <-ticker.C } }() http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/status" { statuses := checker.GetStatuses() w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(statuses) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else if strings.HasPrefix(r.URL.Path, "/status/") { url := strings.TrimPrefix(r.URL.Path, "/status/") status, ok := checker.GetStatus(url) if !ok { http.Error(w, "Not found", http.StatusNotFound) return } w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(w).Encode(status) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } }) http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) { sseBroker.Handler(w, r) }) log.Println("Serving on :8080") log.Fatal(http.ListenAndServe(":8080", nil)) }