Slight data structure edit

This commit is contained in:
kacarmichael 2025-05-28 01:18:32 -05:00
parent bbfa5c914f
commit 5781ab7a97
2 changed files with 24 additions and 12 deletions

View File

@ -8,11 +8,16 @@ import (
"time"
)
var services = []string{
"https://git.aaronic.cc",
"https://portainer.aaronic.cc",
"https://jellyfin.aaronic.cc",
"https://dnd.aaronic.cc",
type Domain struct {
Name string
Protocol string
}
var ServiceItems = map[string]Domain{
"git": {Name: "git.aaronic.cc", Protocol: "https"},
"jellyfin": {Name: "jellyfin.aaronic.cc", Protocol: "https"},
"dnd": {Name: "dnd.aaronic.cc", Protocol: "https"},
"portainer": {Name: "portainer.aaronic.cc", Protocol: "https"},
}
type HealthChecker struct {
@ -22,23 +27,24 @@ type HealthChecker struct {
sse *SSEBroker
}
func NewHealthChecker() *HealthChecker {
func NewHealthChecker(broker *SSEBroker) *HealthChecker {
return &HealthChecker{
status: make(map[string]ServiceStatus),
client: &http.Client{
Timeout: 5 * time.Second,
},
sse: NewSSEBroker(),
sse: broker,
}
}
func (hc *HealthChecker) CheckAll() {
for _, service := range services {
go hc.checkService(service)
for _, service := range ServiceItems {
go hc.checkService(service.Protocol + "://" + service.Name)
}
}
func (hc *HealthChecker) checkService(url string) {
//var url = ServiceItems[service].Protocol + "://" + ServiceItems[service].Name
resp, err := hc.client.Get(url)
status := ServiceStatus{
URL: url,

12
main.go
View File

@ -10,7 +10,7 @@ import (
func main() {
sseBroker := NewSSEBroker()
checker := NewHealthChecker()
checker := NewHealthChecker(sseBroker)
go func() {
ticker := time.NewTicker(30 * time.Second)
@ -25,7 +25,10 @@ func main() {
if r.URL.Path == "/status" {
statuses := checker.GetStatuses()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(statuses)
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)
@ -34,7 +37,10 @@ func main() {
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(status)
err := json.NewEncoder(w).Encode(status)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
})