Added singular service check method
This commit is contained in:
parent
f23e0bc362
commit
bbfa5c914f
@ -73,3 +73,11 @@ func (hc *HealthChecker) GetStatuses() []ServiceStatus {
|
|||||||
|
|
||||||
return results
|
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
|
||||||
|
}
|
||||||
|
|||||||
19
main.go
19
main.go
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,9 +22,21 @@ func main() {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/status", func(w http.ResponseWriter, r *http.Request) {
|
||||||
statuses := checker.GetStatuses()
|
if r.URL.Path == "/status" {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
statuses := checker.GetStatuses()
|
||||||
json.NewEncoder(w).Encode(statuses)
|
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) {
|
http.HandleFunc("/events", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user