refactor: modify directory structure

This commit is contained in:
Fu Diwei
2025-06-17 14:01:34 +08:00
parent 299a722aa9
commit 30840bbba5
346 changed files with 5051 additions and 5086 deletions

View File

@@ -2,16 +2,17 @@ package dingtalkbot
import (
"context"
"errors"
"fmt"
"log/slog"
"net/url"
"github.com/blinkbean/dingtalk"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
"github.com/usual2970/certimate/internal/pkg/core"
)
type NotifierConfig struct {
type NotifierProviderConfig struct {
// 钉钉机器人的 Webhook 地址。
WebhookUrl string `json:"webhookUrl"`
// 钉钉机器人的 Secret。
@@ -19,15 +20,15 @@ type NotifierConfig struct {
}
type NotifierProvider struct {
config *NotifierConfig
config *NotifierProviderConfig
logger *slog.Logger
}
var _ notifier.Notifier = (*NotifierProvider)(nil)
var _ core.Notifier = (*NotifierProvider)(nil)
func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
func NewNotifierProvider(config *NotifierProviderConfig) (*NotifierProvider, error) {
if config == nil {
panic("config is nil")
return nil, errors.New("the configuration of the notifier provider is nil")
}
return &NotifierProvider{
@@ -36,16 +37,15 @@ func NewNotifier(config *NotifierConfig) (*NotifierProvider, error) {
}, nil
}
func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
func (n *NotifierProvider) SetLogger(logger *slog.Logger) {
if logger == nil {
n.logger = slog.New(slog.DiscardHandler)
} else {
n.logger = logger
}
return n
}
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (*notifier.NotifyResult, error) {
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (*core.NotifyResult, error) {
webhookUrl, err := url.Parse(n.config.WebhookUrl)
if err != nil {
return nil, fmt.Errorf("dingtalk api error: invalid webhook url: %w", err)
@@ -62,5 +62,5 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
return nil, fmt.Errorf("dingtalk api error: %w", err)
}
return &notifier.NotifyResult{}, nil
return &core.NotifyResult{}, nil
}

View File

@@ -44,7 +44,7 @@ func TestNotify(t *testing.T) {
fmt.Sprintf("SECRET: %v", fSecret),
}, "\n"))
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
notifier, err := provider.NewNotifierProvider(&provider.NotifierProviderConfig{
WebhookUrl: fWebhookUrl,
Secret: fSecret,
})