From bbfa5c914f5967d1d0530bdc762bafbbe220f29e Mon Sep 17 00:00:00 2001 From: kacarmichael Date: Tue, 27 May 2025 06:15:32 -0500 Subject: [PATCH] Added singular service check method --- healthchecker.go | 8 ++++++++ main.go | 19 ++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/healthchecker.go b/healthchecker.go index eaa17e6..5a4a98e 100644 --- a/healthchecker.go +++ b/healthchecker.go @@ -73,3 +73,11 @@ func (hc *HealthChecker) GetStatuses() []ServiceStatus { return results } + +func (hc *HealthChecker) GetStatus(url string) (ServiceStatus, bool) { + hc.mu.RLock() + defer hc.mu.RUnlock() + + status, ok := hc.status[url] + return status, ok +} diff --git a/main.go b/main.go index 4bcaa03..f65ed4d 100644 --- a/main.go +++ b/main.go @@ -4,6 +4,7 @@ import ( "encoding/json" "log" "net/http" + "strings" "time" ) @@ -21,9 +22,21 @@ func main() { }() http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) { - statuses := checker.GetStatuses() - w.Header().Set("Content-Type", "application/json") - json.NewEncoder(w).Encode(statuses) + if r.URL.Path == "/status" { + statuses := checker.GetStatuses() + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(statuses) + } 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") + json.NewEncoder(w).Encode(status) + } + }) http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {