refactor: normalize providers constructors
This commit is contained in:
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type BarkNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// Bark 服务地址。
|
||||
// 零值时默认使用官方服务器。
|
||||
ServerUrl string `json:"serverUrl"`
|
||||
@@ -17,23 +17,23 @@ type BarkNotifierConfig struct {
|
||||
DeviceKey string `json:"deviceKey"`
|
||||
}
|
||||
|
||||
type BarkNotifier struct {
|
||||
config *BarkNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*BarkNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *BarkNotifierConfig) (*BarkNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &BarkNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *BarkNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) 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)
|
||||
|
||||
@@ -44,7 +44,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("DEVICEKEY: %v", fDeviceKey),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.BarkNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
ServerUrl: fServerUrl,
|
||||
DeviceKey: fDeviceKey,
|
||||
})
|
||||
|
||||
@@ -8,30 +8,30 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type DingTalkNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// 钉钉机器人的 Token。
|
||||
AccessToken string `json:"accessToken"`
|
||||
// 钉钉机器人的 Secret。
|
||||
Secret string `json:"secret"`
|
||||
}
|
||||
|
||||
type DingTalkNotifier struct {
|
||||
config *DingTalkNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*DingTalkNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *DingTalkNotifierConfig) (*DingTalkNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &DingTalkNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *DingTalkNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := dingding.New(&dingding.Config{
|
||||
Token: n.config.AccessToken,
|
||||
Secret: n.config.Secret,
|
||||
|
||||
@@ -43,7 +43,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("SECRET: %v", fSecret),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.DingTalkNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
AccessToken: fAccessToken,
|
||||
Secret: fSecret,
|
||||
})
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type EmailNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// SMTP 服务器地址。
|
||||
SmtpHost string `json:"smtpHost"`
|
||||
// SMTP 服务器端口。
|
||||
@@ -29,23 +29,23 @@ type EmailNotifierConfig struct {
|
||||
ReceiverAddress string `json:"receiverAddress"`
|
||||
}
|
||||
|
||||
type EmailNotifier struct {
|
||||
config *EmailNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*EmailNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *EmailNotifierConfig) (*EmailNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &EmailNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *EmailNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
var smtpAuth smtp.Auth
|
||||
if n.config.Username != "" || n.config.Password != "" {
|
||||
smtpAuth = smtp.PlainAuth("", n.config.Username, n.config.Password, n.config.SmtpHost)
|
||||
|
||||
@@ -64,7 +64,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("RECEIVERADDRESS: %v", fReceiverAddress),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.EmailNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
SmtpHost: fSmtpHost,
|
||||
SmtpPort: int32(fSmtpPort),
|
||||
SmtpTLS: fSmtpTLS,
|
||||
|
||||
@@ -8,28 +8,28 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type LarkNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// 飞书机器人 Webhook 地址。
|
||||
WebhookUrl string `json:"webhookUrl"`
|
||||
}
|
||||
|
||||
type LarkNotifier struct {
|
||||
config *LarkNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*LarkNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *LarkNotifierConfig) (*LarkNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &LarkNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *LarkNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := lark.NewWebhookService(n.config.WebhookUrl)
|
||||
|
||||
err = srv.Send(ctx, subject, message)
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("WEBHOOKURL: %v", fWebhookUrl),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.LarkNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
WebhookUrl: fWebhookUrl,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -9,28 +9,28 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type ServerChanNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// ServerChan 服务地址。
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type ServerChanNotifier struct {
|
||||
config *ServerChanNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*ServerChanNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *ServerChanNotifierConfig) (*ServerChanNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &ServerChanNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *ServerChanNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := notifyHttp.New()
|
||||
|
||||
srv.AddReceivers(¬ifyHttp.Webhook{
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("URL: %v", fUrl),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.ServerChanNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
Url: fUrl,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -8,30 +8,30 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type TelegramNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// Telegram API Token。
|
||||
ApiToken string `json:"apiToken"`
|
||||
// Telegram Chat ID。
|
||||
ChatId int64 `json:"chatId"`
|
||||
}
|
||||
|
||||
type TelegramNotifier struct {
|
||||
config *TelegramNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*TelegramNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *TelegramNotifierConfig) (*TelegramNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &TelegramNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *TelegramNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv, err := telegram.New(n.config.ApiToken)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -44,7 +44,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("CHATID: %v", fChartId),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.TelegramNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
ApiToken: fApiToken,
|
||||
ChatId: fChartId,
|
||||
})
|
||||
|
||||
@@ -8,28 +8,28 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type WebhookNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// Webhook URL。
|
||||
Url string `json:"url"`
|
||||
}
|
||||
|
||||
type WebhookNotifier struct {
|
||||
config *WebhookNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*WebhookNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *WebhookNotifierConfig) (*WebhookNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &WebhookNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *WebhookNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := http.New()
|
||||
|
||||
srv.AddReceiversURLs(n.config.Url)
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("URL: %v", fUrl),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.WebhookNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
Url: fUrl,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -9,28 +9,28 @@ import (
|
||||
"github.com/usual2970/certimate/internal/pkg/core/notifier"
|
||||
)
|
||||
|
||||
type WeComNotifierConfig struct {
|
||||
type NotifierConfig struct {
|
||||
// 企业微信机器人 Webhook 地址。
|
||||
WebhookUrl string `json:"webhookUrl"`
|
||||
}
|
||||
|
||||
type WeComNotifier struct {
|
||||
config *WeComNotifierConfig
|
||||
type NotifierProvider struct {
|
||||
config *NotifierConfig
|
||||
}
|
||||
|
||||
var _ notifier.Notifier = (*WeComNotifier)(nil)
|
||||
var _ notifier.Notifier = (*NotifierProvider)(nil)
|
||||
|
||||
func New(config *WeComNotifierConfig) (*WeComNotifier, error) {
|
||||
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
|
||||
if config == nil {
|
||||
panic("config is nil")
|
||||
}
|
||||
|
||||
return &WeComNotifier{
|
||||
return &NotifierProvider{
|
||||
config: config,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (n *WeComNotifier) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
|
||||
srv := notifyHttp.New()
|
||||
|
||||
srv.AddReceivers(¬ifyHttp.Webhook{
|
||||
|
||||
@@ -38,7 +38,7 @@ func TestNotify(t *testing.T) {
|
||||
fmt.Sprintf("WEBHOOKURL: %v", fWebhookUrl),
|
||||
}, "\n"))
|
||||
|
||||
notifier, err := provider.New(&provider.WeComNotifierConfig{
|
||||
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
|
||||
WebhookUrl: fWebhookUrl,
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user