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
|
||||
}
|
||||
|
||||
func (hc *HealthChecker) GetStatus(url string) (ServiceStatus, bool) {
|
||||
hc.mu.RLock()
|
||||
defer hc.mu.RUnlock()
|
||||
|
||||
status, ok := hc.status[url]
|
||||
return status, ok
|
||||
}
|
||||
|
||||
13
main.go
13
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) {
|
||||
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) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user