refactor: clean code

This commit is contained in:
Fu Diwei
2025-04-24 23:14:17 +08:00
parent 794695c313
commit a117fd7d93
19 changed files with 175 additions and 107 deletions

View File

@@ -19,7 +19,7 @@ type NotifierConfig struct {
// 零值时根据是否启用 TLS 决定。
SmtpPort int32 `json:"smtpPort"`
// 是否启用 TLS。
SmtpTLS bool `json:"smtpTLS"`
SmtpTls bool `json:"smtpTls"`
// 用户名。
Username string `json:"username"`
// 密码。
@@ -64,7 +64,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
var smtpAddr string
if n.config.SmtpPort == 0 {
if n.config.SmtpTLS {
if n.config.SmtpTls {
smtpAddr = fmt.Sprintf("%s:465", n.config.SmtpHost)
} else {
smtpAddr = fmt.Sprintf("%s:25", n.config.SmtpHost)
@@ -74,7 +74,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
}
var yak *mailyak.MailYak
if n.config.SmtpTLS {
if n.config.SmtpTls {
yak, err = mailyak.NewWithTLS(smtpAddr, smtpAuth, newTlsConfig())
if err != nil {
return nil, err

View File

@@ -67,7 +67,7 @@ func TestNotify(t *testing.T) {
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
SmtpHost: fSmtpHost,
SmtpPort: int32(fSmtpPort),
SmtpTLS: fSmtpTLS,
SmtpTls: fSmtpTLS,
Username: fUsername,
Password: fPassword,
SenderAddress: fSenderAddress,

View File

@@ -7,20 +7,21 @@ import (
"io"
"log/slog"
"net/http"
"strings"
"github.com/nikoksr/notify/service/mattermost"
"github.com/usual2970/certimate/internal/pkg/core/notifier"
)
type NotifierConfig struct {
// Mattermost 服务地址。
// 服务地址。
ServerUrl string `json:"serverUrl"`
// 频道ID
ChannelId string `json:"channelId"`
// 用户名
// 用户名。
Username string `json:"username"`
// 密码
// 密码
Password string `json:"password"`
// 频道 ID。
ChannelId string `json:"channelId"`
}
type NotifierProvider struct {
@@ -50,7 +51,7 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
}
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
srv := mattermost.New(n.config.ServerUrl)
srv := mattermost.New(strings.TrimRight(n.config.ServerUrl, "/"))
if err := srv.LoginWithCredentials(ctx, n.config.Username, n.config.Password); err != nil {
return nil, err

View File

@@ -10,8 +10,8 @@ import (
)
type NotifierConfig struct {
// Telegram API Token。
ApiToken string `json:"apiToken"`
// Telegram Bot API Token。
BotToken string `json:"botToken"`
// Telegram Chat ID。
ChatId int64 `json:"chatId"`
}
@@ -43,7 +43,7 @@ func (n *NotifierProvider) WithLogger(logger *slog.Logger) notifier.Notifier {
}
func (n *NotifierProvider) Notify(ctx context.Context, subject string, message string) (res *notifier.NotifyResult, err error) {
srv, err := telegram.New(n.config.ApiToken)
srv, err := telegram.New(n.config.BotToken)
if err != nil {
return nil, err
}

View File

@@ -45,7 +45,7 @@ func TestNotify(t *testing.T) {
}, "\n"))
notifier, err := provider.NewNotifier(&provider.NotifierConfig{
ApiToken: fApiToken,
BotToken: fApiToken,
ChatId: fChartId,
})
if err != nil {