Files
WeeWooWebhook/internal/server/webhook.go
Henri Burau 64f955c4a1
Some checks failed
Deploy and Push Docker Image / BuildAndPush (push) Failing after 14s
Basic functionality working
2025-06-19 13:57:36 +02:00

57 lines
1.3 KiB
Go

package server
import (
"encoding/json"
"fmt"
"log"
"net/http"
"gitea.henriburau.de/ace966/WeeWooWebhook/internal/model"
)
func (s *Server) webhookHandler() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var data model.UptimeKumaWebhookRequest
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
log.Printf("Received webhook msg: %s\n", data.Msg)
s.mu.Lock()
defer s.mu.Unlock()
wasDown := s.downServices[data.Monitor.ID]
switch data.Heartbeat.Status {
case model.MonitorStatusDown:
if !wasDown {
s.downServices[data.Heartbeat.MonitorID] = true
log.Printf("Service DOWN: %s", data.Monitor.Name)
go s.activateEmergencyLight()
}
case model.MonitorStatusUp:
if wasDown {
delete(s.downServices, data.Heartbeat.MonitorID)
log.Printf("Service UP: %s", data.Monitor.Name)
if len(s.downServices) == 0 {
go s.deactivateEmergencyLight()
}
}
}
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, "Received")
}
}
func (s *Server) activateEmergencyLight() {
s.OfficeLightClient.SetColor(255, 0, 0) // Set to red
s.OfficeLightClient.SetLightState(true) // Turn on
}
func (s *Server) deactivateEmergencyLight() {
s.OfficeLightClient.SetLightState(false) // Turn off
}