mirror of
https://github.com/usual2970/certimate.git
synced 2025-06-19 19:00:00 +00:00
fix: nil pointer
This commit is contained in:
parent
9a937fa072
commit
7e376071f5
@ -33,16 +33,19 @@ func NewCertificateService(repo CertificateRepository) *certificateService {
|
|||||||
|
|
||||||
func (s *certificateService) InitSchedule(ctx context.Context) error {
|
func (s *certificateService) InitSchedule(ctx context.Context) error {
|
||||||
scheduler := app.GetScheduler()
|
scheduler := app.GetScheduler()
|
||||||
|
|
||||||
err := scheduler.Add("certificate", "0 0 * * *", func() {
|
err := scheduler.Add("certificate", "0 0 * * *", func() {
|
||||||
certs, err := s.repo.ListExpireSoon(context.Background())
|
certs, err := s.repo.ListExpireSoon(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
app.GetLogger().Error("failed to get expire soon certificate", "err", err)
|
app.GetLogger().Error("failed to get expire soon certificate", "err", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
msg := buildMsg(certs)
|
|
||||||
// TODO: 空指针 Bug
|
notification := buildExpireSoonNotification(certs)
|
||||||
if err := notify.SendToAllChannels(msg.Subject, msg.Message); err != nil {
|
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)
|
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
|
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 {
|
if len(records) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查询模板信息
|
|
||||||
settingRepo := repository.NewSettingsRepository()
|
|
||||||
setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates")
|
|
||||||
|
|
||||||
subject := defaultExpireSubject
|
subject := defaultExpireSubject
|
||||||
message := defaultExpireMessage
|
message := defaultExpireMessage
|
||||||
|
|
||||||
|
// 查询模板信息
|
||||||
|
settingRepo := repository.NewSettingsRepository()
|
||||||
|
setting, err := settingRepo.GetByName(context.Background(), "notifyTemplates")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
var templates *domain.NotifyTemplatesSettingsContent
|
var templates *domain.NotifyTemplatesSettingsContent
|
||||||
|
|
||||||
json.Unmarshal([]byte(setting.Content), &templates)
|
json.Unmarshal([]byte(setting.Content), &templates)
|
||||||
|
|
||||||
if templates != nil && len(templates.NotifyTemplates) > 0 {
|
if templates != nil && len(templates.NotifyTemplates) > 0 {
|
||||||
@ -81,22 +87,18 @@ func buildMsg(records []domain.Certificate) *domain.NotifyMessage {
|
|||||||
// 替换变量
|
// 替换变量
|
||||||
count := len(records)
|
count := len(records)
|
||||||
domains := make([]string, count)
|
domains := make([]string, count)
|
||||||
|
|
||||||
for i, record := range records {
|
for i, record := range records {
|
||||||
domains[i] = record.SubjectAltNames
|
domains[i] = record.SubjectAltNames
|
||||||
}
|
}
|
||||||
|
|
||||||
countStr := strconv.Itoa(count)
|
countStr := strconv.Itoa(count)
|
||||||
domainStr := strings.Join(domains, ";")
|
domainStr := strings.Join(domains, ";")
|
||||||
|
|
||||||
subject = strings.ReplaceAll(subject, "${COUNT}", countStr)
|
subject = strings.ReplaceAll(subject, "${COUNT}", countStr)
|
||||||
subject = strings.ReplaceAll(subject, "${DOMAINS}", domainStr)
|
subject = strings.ReplaceAll(subject, "${DOMAINS}", domainStr)
|
||||||
|
|
||||||
message = strings.ReplaceAll(message, "${COUNT}", countStr)
|
message = strings.ReplaceAll(message, "${COUNT}", countStr)
|
||||||
message = strings.ReplaceAll(message, "${DOMAINS}", domainStr)
|
message = strings.ReplaceAll(message, "${DOMAINS}", domainStr)
|
||||||
|
|
||||||
// 返回消息
|
// 返回消息
|
||||||
return &domain.NotifyMessage{
|
return &certificateNotification{
|
||||||
Subject: subject,
|
Subject: subject,
|
||||||
Message: message,
|
Message: message,
|
||||||
}
|
}
|
||||||
|
@ -22,11 +22,6 @@ type NotifyTemplate struct {
|
|||||||
|
|
||||||
type NotifyChannelsSettingsContent map[string]map[string]any
|
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) {
|
func (s *Settings) GetNotifyChannelConfig(channel string) (map[string]any, error) {
|
||||||
conf := &NotifyChannelsSettingsContent{}
|
conf := &NotifyChannelsSettingsContent{}
|
||||||
if err := json.Unmarshal([]byte(s.Content), conf); err != nil {
|
if err := json.Unmarshal([]byte(s.Content), conf); err != nil {
|
||||||
|
@ -74,7 +74,6 @@ func TestDeploy(t *testing.T) {
|
|||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fInputCertData, _ := os.ReadFile(fInputCertPath)
|
fInputCertData, _ := os.ReadFile(fInputCertPath)
|
||||||
@ -82,7 +81,6 @@ func TestDeploy(t *testing.T) {
|
|||||||
res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData))
|
res, err := deployer.Deploy(context.Background(), string(fInputCertData), string(fInputKeyData))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("err: %+v", err)
|
t.Errorf("err: %+v", err)
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("ok: %v", res)
|
t.Logf("ok: %v", res)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user