feat: notifier

This commit is contained in:
Fu Diwei
2024-11-09 20:06:22 +08:00
parent 76fc47a274
commit 551b06b4e8
14 changed files with 350 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package email
import (
"context"
"errors"
"github.com/nikoksr/notify"
"github.com/nikoksr/notify/service/bark"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
)
type BarkNotifierConfig struct {
ServerUrl string `json:"serverUrl"`
DeviceKey string `json:"deviceKey"`
}
type BarkNotifier struct {
config *BarkNotifierConfig
}
var _ notifier.Notifier = (*BarkNotifier)(nil)
func New(config *BarkNotifierConfig) (*BarkNotifier, error) {
if config == nil {
return nil, errors.New("config is nil")
}
return &BarkNotifier{
config: config,
}, nil
}
func (n *BarkNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
var srv notify.Notifier
if n.config.ServerUrl == "" {
srv = bark.New(n.config.DeviceKey)
} else {
srv = bark.NewWithServers(n.config.DeviceKey, n.config.ServerUrl)
}
err = srv.Send(ctx, subject, message)
if err != nil {
return nil, err
}
return &notifier.NotifyResult{}, nil
}