49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func main() {
|
|
sseBroker := NewSSEBroker()
|
|
checker := NewHealthChecker()
|
|
|
|
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")
|
|
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) {
|
|
sseBroker.Handler(w, r)
|
|
})
|
|
|
|
log.Println("Serving on :8080")
|
|
log.Fatal(http.ListenAndServe(":8080", nil))
|
|
}
|