Switch to using nats for notifications

This commit is contained in:
2025-02-11 22:10:11 +01:00
parent d941260e38
commit 360109d684
6 changed files with 125 additions and 99 deletions

View File

@@ -7,13 +7,11 @@ import (
"net/http"
"strings"
"time"
"git.t-juice.club/torjus/alerttonotify/bus"
)
type Server struct {
logger *slog.Logger
nbus *bus.NotifyBus
ns *NotificationService
http.Server
}
@@ -39,9 +37,8 @@ type AlertMessage struct {
Alerts []Alert `json:"alerts"`
}
func NewServer(nbus *bus.NotifyBus, logger *slog.Logger) *Server {
func NewServer(ns *NotificationService, logger *slog.Logger) *Server {
srv := &Server{
nbus: nbus,
logger: logger,
}
@@ -82,10 +79,14 @@ func (s *Server) handleAlert(w http.ResponseWriter, r *http.Request) {
}
}
notification := bus.BusNotification{
msg := &BusNotification{
Summary: fmt.Sprintf("%d alerts %s", len(alertMessage.Alerts), alertMessage.Status),
Body: sb.String(),
}
s.logger.Debug("Sending notification", "notification", notification)
s.nbus.Notify(notification)
s.logger.Debug("Sending notification", "message", msg)
if err := s.ns.Notify(msg); err != nil {
s.logger.Error("Failed to send notification", "error", err)
w.WriteHeader(http.StatusInternalServerError)
}
}