fix: nil pointer

This commit is contained in:
Fu Diwei 2025-01-07 01:10:26 +08:00
parent 9a937fa072
commit 7e376071f5
3 changed files with 17 additions and 22 deletions

View File

@ -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,
}

View File

@ -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 {

View File

@ -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)