2024-12-02 01:03:39 +00:00
|
|
|
package main
|
|
|
|
|
2024-12-02 23:47:40 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log/slog"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
|
|
|
|
"git.t-juice.club/torjus/alerttonotify/bus"
|
|
|
|
"git.t-juice.club/torjus/alerttonotify/server"
|
|
|
|
)
|
2024-12-02 01:03:39 +00:00
|
|
|
|
|
|
|
const Version = "v0.1.0"
|
|
|
|
|
|
|
|
func main() {
|
2024-12-02 23:47:40 +00:00
|
|
|
// Setup logging
|
|
|
|
logger := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{
|
|
|
|
Level: slog.LevelDebug,
|
|
|
|
}))
|
|
|
|
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)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
defer nbus.Close()
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
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.Addr = ":5001"
|
|
|
|
|
|
|
|
// Listen for shutdown signal
|
|
|
|
go func() {
|
|
|
|
<-shutdownCtx.Done()
|
|
|
|
srv.Shutdown(context.Background())
|
|
|
|
}()
|
|
|
|
|
|
|
|
logger.Info("Starting http server", "addr", srv.Addr)
|
|
|
|
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
|
|
logger.Error("Failed to start http server", "error", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
logger.Info("Shutting down")
|
2024-12-02 01:03:39 +00:00
|
|
|
}
|