refactor: clean code

This commit is contained in:
Fu Diwei
2024-12-27 19:35:50 +08:00
parent 047479426a
commit 86133ba52b
41 changed files with 396 additions and 544 deletions

View File

@@ -16,25 +16,25 @@ import (
"github.com/usual2970/certimate/internal/pkg/utils/maps"
)
func createNotifier(channel string, channelConfig map[string]any) (notifier.Notifier, error) {
func createNotifier(channel domain.NotifyChannelType, channelConfig map[string]any) (notifier.Notifier, error) {
/*
注意:如果追加新的常量值,请保持以 ASCII 排序。
NOTICE: If you add new constant, please keep ASCII order.
*/
switch channel {
case domain.NotifyChannelBark:
case domain.NOTIFY_CHANNEL_BARK:
return providerBark.New(&providerBark.BarkNotifierConfig{
DeviceKey: maps.GetValueAsString(channelConfig, "deviceKey"),
ServerUrl: maps.GetValueAsString(channelConfig, "serverUrl"),
})
case domain.NotifyChannelDingtalk:
case domain.NOTIFY_CHANNEL_DINGTALK:
return providerDingTalk.New(&providerDingTalk.DingTalkNotifierConfig{
AccessToken: maps.GetValueAsString(channelConfig, "accessToken"),
Secret: maps.GetValueAsString(channelConfig, "secret"),
})
case domain.NotifyChannelEmail:
case domain.NOTIFY_CHANNEL_EMAIL:
return providerEmail.New(&providerEmail.EmailNotifierConfig{
SmtpHost: maps.GetValueAsString(channelConfig, "smtpHost"),
SmtpPort: maps.GetValueAsInt32(channelConfig, "smtpPort"),
@@ -45,28 +45,28 @@ func createNotifier(channel string, channelConfig map[string]any) (notifier.Noti
ReceiverAddress: maps.GetValueAsString(channelConfig, "receiverAddress"),
})
case domain.NotifyChannelLark:
case domain.NOTIFY_CHANNEL_LARK:
return providerLark.New(&providerLark.LarkNotifierConfig{
WebhookUrl: maps.GetValueAsString(channelConfig, "webhookUrl"),
})
case domain.NotifyChannelServerChan:
case domain.NOTIFY_CHANNEL_SERVERCHAN:
return providerServerChan.New(&providerServerChan.ServerChanNotifierConfig{
Url: maps.GetValueAsString(channelConfig, "url"),
})
case domain.NotifyChannelTelegram:
case domain.NOTIFY_CHANNEL_TELEGRAM:
return providerTelegram.New(&providerTelegram.TelegramNotifierConfig{
ApiToken: maps.GetValueAsString(channelConfig, "apiToken"),
ChatId: maps.GetValueAsInt64(channelConfig, "chatId"),
})
case domain.NotifyChannelWebhook:
case domain.NOTIFY_CHANNEL_WEBHOOK:
return providerWebhook.New(&providerWebhook.WebhookNotifierConfig{
Url: maps.GetValueAsString(channelConfig, "url"),
})
case domain.NotifyChannelWeCom:
case domain.NOTIFY_CHANNEL_WECOM:
return providerWeCom.New(&providerWeCom.WeComNotifierConfig{
WebhookUrl: maps.GetValueAsString(channelConfig, "webhookUrl"),
})

View File

@@ -7,6 +7,7 @@ import (
"golang.org/x/sync/errgroup"
"github.com/usual2970/certimate/internal/app"
"github.com/usual2970/certimate/internal/domain"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
"github.com/usual2970/certimate/internal/pkg/utils/maps"
)
@@ -37,7 +38,7 @@ func SendToAllChannels(subject, message string) error {
}
func SendToChannel(subject, message string, channel string, channelConfig map[string]any) error {
notifier, err := createNotifier(channel, channelConfig)
notifier, err := createNotifier(domain.NotifyChannelType(channel), channelConfig)
if err != nil {
return err
}
@@ -63,7 +64,7 @@ func getEnabledNotifiers() ([]notifier.Notifier, error) {
continue
}
notifier, err := createNotifier(k, v)
notifier, err := createNotifier(domain.NotifyChannelType(k), v)
if err != nil {
continue
}

View File

@@ -13,7 +13,7 @@ const (
)
type SettingRepository interface {
GetByName(ctx context.Context, name string) (*domain.Setting, error)
GetByName(ctx context.Context, name string) (*domain.Settings, error)
}
type NotifyService struct {