Switch to using nats for notifications
This commit is contained in:
47
main.go
47
main.go
@@ -6,12 +6,12 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
|
||||
"git.t-juice.club/torjus/alerttonotify/bus"
|
||||
"git.t-juice.club/torjus/alerttonotify/server"
|
||||
)
|
||||
|
||||
const Version = "v0.1.0"
|
||||
const Version = "v0.1.1"
|
||||
|
||||
func main() {
|
||||
// Setup logging
|
||||
@@ -20,33 +20,50 @@ func main() {
|
||||
}))
|
||||
logger.Info("Starting alerttonotify", "version", Version)
|
||||
|
||||
// Setup dbus connection
|
||||
nbus, err := bus.NewNotifyBus()
|
||||
if err != nil {
|
||||
logger.Error("Failed to create notify bus", "error", err)
|
||||
// Setup nats connection
|
||||
natsURL, ok := os.LookupEnv("NATS_URL")
|
||||
if !ok {
|
||||
logger.Error("NATS_URL not set")
|
||||
os.Exit(1)
|
||||
}
|
||||
defer nbus.Close()
|
||||
nkey, ok := os.LookupEnv("NATS_NKEY")
|
||||
if !ok {
|
||||
path, ok := os.LookupEnv("NATS_NKEY_FILE")
|
||||
if !ok {
|
||||
logger.Error("NATS_NKEY and NATS_NKEY_FILE not set")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// Verify connection and server
|
||||
info, err := nbus.ServerInfo()
|
||||
if err != nil {
|
||||
logger.Error("Failed to get notification server info", "error", err)
|
||||
os.Exit(1)
|
||||
return
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
logger.Error("unable to read NATS_NKEY_FILE", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
nkey = strings.TrimSpace(string(data))
|
||||
}
|
||||
|
||||
ns, err := server.NewNotificationService(natsURL, nkey)
|
||||
if err != nil {
|
||||
logger.Error("Failed to create notification service", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger.Info("Connected to notification daemon", "server", info.Name, "version", info.Version)
|
||||
|
||||
shutdownCtx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
|
||||
// Setup http server
|
||||
srv := server.NewServer(nbus, logger)
|
||||
srv := server.NewServer(ns, logger)
|
||||
srv.Addr = ":5001"
|
||||
|
||||
addr, ok := os.LookupEnv("ALERTTONOTIFY_ADDR")
|
||||
if ok {
|
||||
srv.Addr = addr
|
||||
}
|
||||
|
||||
// Listen for shutdown signal
|
||||
go func() {
|
||||
<-shutdownCtx.Done()
|
||||
srv.Close()
|
||||
srv.Shutdown(context.Background())
|
||||
}()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user