From 7e376071f563fa994e8c80044395c34d88d50982 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 7 Jan 2025 01:10:26 +0800 Subject: [PATCH] fix: nil pointer --- internal/certificate/service.go | 32 ++++++++++--------- internal/domain/settings.go | 5 --- .../core/deployer/providers/ssh/ssh_test.go | 2 -- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/internal/certificate/service.go b/internal/certificate/service.go index 193de534..e2bdb0f1 100644 --- a/internal/certificate/service.go +++ b/internal/certificate/service.go @@ -33,16 +33,19 @@ func NewCertificateService(repo CertificateRepository) *certificateService { func (s *certificateService) InitSchedule(ctx context.Context) error { scheduler := app.GetScheduler() - err := scheduler.Add("certificate", "0 0 * * *", func() { certs, err := s.repo.ListExpireSoon(context.Background()) if err != nil { app.GetLogger().Error("failed to get expire soon certificate", "err", err) return } - msg := buildMsg(certs) - // TODO: 空指针 Bug - if err := notify.SendToAllChannels(msg.Subject, msg.Message); err != nil { + + notification := buildExpireSoonNotification(certs) + if notification == nil { + return + } + + if err := notify.SendToAllChannels(notification.Subject, notification.Message); err != nil { app.GetLogger().Error("failed to send expire soon certificate", "err", err) } }) @@ -55,21 +58,24 @@ func (s *certificateService) InitSchedule(ctx context.Context) error { return nil } -func buildMsg(records []domain.Certificate) *domain.NotifyMessage { +type certificateNotification struct { + Subject string `json:"subject"` + Message string `json:"message"` +} + +func buildExpireSoonNotification(records []domain.Certificate) *certificateNotification { if len(records) == 0 { return nil } - // 查询模板信息 - settingRepo := repository.NewSettingsRepository() - setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates") - subject := defaultExpireSubject message := defaultExpireMessage + // 查询模板信息 + settingRepo := repository.NewSettingsRepository() + setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates") if err == nil { var templates *domain.NotifyTemplatesSettingsContent - json.Unmarshal([]byte(setting.Content), &templates) if templates != nil && len(templates.NotifyTemplates) > 0 { @@ -81,22 +87,18 @@ func buildMsg(records []domain.Certificate) *domain.NotifyMessage { // 替换变量 count := len(records) domains := make([]string, count) - for i, record := range records { domains[i] = record.SubjectAltNames } - countStr := strconv.Itoa(count) domainStr := strings.Join(domains, ";") - subject = strings.ReplaceAll(subject, "${COUNT}", countStr) subject = strings.ReplaceAll(subject, "${DOMAINS}", domainStr) - message = strings.ReplaceAll(message, "${COUNT}", countStr) message = strings.ReplaceAll(message, "${DOMAINS}", domainStr) // 返回消息 - return &domain.NotifyMessage{ + return &certificateNotification{ Subject: subject, Message: message, } diff --git a/internal/domain/settings.go b/internal/domain/settings.go index 7c8f374c..8d1d983f 100644 --- a/internal/domain/settings.go +++ b/internal/domain/settings.go @@ -22,11 +22,6 @@ type NotifyTemplate struct { type NotifyChannelsSettingsContent map[string]map[string]any -type NotifyMessage struct { - Subject string `json:"subject"` - Message string `json:"message"` -} - func (s *Settings) GetNotifyChannelConfig(channel string) (map[string]any, error) { conf := &NotifyChannelsSettingsContent{} if err := json.Unmarshal([]byte(s.Content), conf); err != nil { diff --git a/internal/pkg/core/deployer/providers/ssh/ssh_test.go b/internal/pkg/core/deployer/providers/ssh/ssh_test.go index f1c25e63..0dd58358 100644 --- a/internal/pkg/core/deployer/providers/ssh/ssh_test.go +++ b/internal/pkg/core/deployer/providers/ssh/ssh_test.go @@ -74,7 +74,6 @@ func TestDeploy(t *testing.T) { }) if err != nil { t.Errorf("err: %+v", err) - panic(err) } fInputCertData, _ := os.ReadFile(fInputCertPath) @@ -82,7 +81,6 @@ func TestDeploy(t *testing.T) { res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData)) if err != nil { t.Errorf("err: %+v", err) - panic(err) } t.Logf("ok: %v", res)