Switch to using nats for notifications
This commit is contained in:
63
server/notify.go
Normal file
63
server/notify.go
Normal file
@@ -0,0 +1,63 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/nats-io/nats.go"
|
||||
"github.com/nats-io/nkeys"
|
||||
)
|
||||
|
||||
const DEFAULT_SUBJECT = "home2rjusnet.notifications"
|
||||
|
||||
type BusNotification struct {
|
||||
ID uint32 `json:"id,omitempty"`
|
||||
Summary string `json:"summary"`
|
||||
Body string `json:"body,omitempty"`
|
||||
Timeout time.Duration `json:"timeout,omitempty"`
|
||||
}
|
||||
|
||||
type NotificationService struct {
|
||||
Subject string
|
||||
nc *nats.Conn
|
||||
}
|
||||
|
||||
func NewNotificationService(url, nkey string) (*NotificationService, error) {
|
||||
kp, err := nkeys.FromSeed([]byte(nkey))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error creating nkey: %v", err)
|
||||
}
|
||||
|
||||
pub, err := kp.PublicKey()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error getting public key: %v", err)
|
||||
}
|
||||
|
||||
opt := nats.Nkey(pub, kp.Sign)
|
||||
|
||||
nc, err := nats.Connect(url, opt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error connecting to nats: %v", err)
|
||||
}
|
||||
|
||||
return &NotificationService{nc: nc, Subject: DEFAULT_SUBJECT}, nil
|
||||
}
|
||||
|
||||
func (n *NotificationService) Notify(msg *BusNotification) error {
|
||||
data, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshalling notification: %v", err)
|
||||
}
|
||||
|
||||
err = n.nc.Publish(n.Subject, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error publishing notification: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (n *NotificationService) Close() {
|
||||
n.nc.Close()
|
||||
}
|
@@ -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)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user