Add notification channels and a provider abstraction
Users get a notification_channels table (replacing users.ntfy_topic); pages fan
out to every enabled channel through a notify.Provider registry, with ntfy as
the first provider. Fallbacks are typed {type, address}. Schema edited in place:
existing databases must be recreated.
Claude-Session: https://claude.ai/code/session_016mBLURvJoMuUEr9cB2RpUN
This commit is contained in:
+29
-9
@@ -11,6 +11,7 @@ import (
|
||||
"git.ryuvia.com/niklas/terdut-server/internal/api"
|
||||
"git.ryuvia.com/niklas/terdut-server/internal/config"
|
||||
"git.ryuvia.com/niklas/terdut-server/internal/db"
|
||||
"git.ryuvia.com/niklas/terdut-server/internal/notify"
|
||||
)
|
||||
|
||||
var version = "dev"
|
||||
@@ -31,12 +32,11 @@ func main() {
|
||||
log.Fatalf("migrate: %v", err)
|
||||
}
|
||||
|
||||
notify := api.NotifyConfig{
|
||||
BaseURL: cfg.NtfyURL,
|
||||
Token: cfg.NtfyToken,
|
||||
FallbackTopic: cfg.NtfyFallbackTopic,
|
||||
PublicURL: cfg.PublicURL,
|
||||
RepeatEvery: cfg.NotifyRepeat,
|
||||
notifyCfg := api.NotifyConfig{
|
||||
Providers: notifyProviders(cfg),
|
||||
Fallback: notifyFallback(cfg),
|
||||
PublicURL: cfg.PublicURL,
|
||||
RepeatEvery: cfg.NotifyRepeat,
|
||||
}
|
||||
|
||||
// The behaviour knobs move into the database on first start, after which an
|
||||
@@ -49,7 +49,7 @@ func main() {
|
||||
log.Fatalf("%v", err)
|
||||
}
|
||||
|
||||
router := api.NewRouter(database, notify, cfg, version)
|
||||
router := api.NewRouter(database, notifyCfg, cfg, version)
|
||||
|
||||
srv := &http.Server{
|
||||
Addr: cfg.Addr,
|
||||
@@ -62,8 +62,8 @@ func main() {
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
go api.StartArchiver(ctx, database, cfg.ArchiveAfter, cfg.StaleAfter, notify)
|
||||
go api.StartNotifier(ctx, database, notify)
|
||||
go api.StartArchiver(ctx, database, cfg.ArchiveAfter, cfg.StaleAfter, notifyCfg)
|
||||
go api.StartNotifier(ctx, database, notifyCfg)
|
||||
|
||||
go func() {
|
||||
log.Printf("terdut-server %s listening on %s", version, cfg.Addr)
|
||||
@@ -80,3 +80,23 @@ func main() {
|
||||
log.Printf("shutdown: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// notifyProviders registers the notification providers this install has
|
||||
// configuration for. A provider with no configuration is simply absent, which is
|
||||
// what keeps it out of the web UI's list of channel types.
|
||||
func notifyProviders(cfg config.Config) *notify.Registry {
|
||||
var providers []notify.Provider
|
||||
if cfg.NtfyURL != "" {
|
||||
providers = append(providers, notify.NewNtfy(cfg.NtfyURL, cfg.NtfyToken))
|
||||
}
|
||||
return notify.NewRegistry(providers...)
|
||||
}
|
||||
|
||||
// notifyFallback is where an incident goes when nobody on call can be reached.
|
||||
// Only ntfy has a fallback setting today; a second provider adds its own.
|
||||
func notifyFallback(cfg config.Config) notify.Target {
|
||||
if cfg.NtfyURL != "" && cfg.NtfyFallbackTopic != "" {
|
||||
return notify.Target{Type: notify.TypeNtfy, Address: cfg.NtfyFallbackTopic}
|
||||
}
|
||||
return notify.Target{}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user