Skip to content

Commit

Permalink
feat: support send alert with specific providers
Browse files Browse the repository at this point in the history
  • Loading branch information
love98ooo committed Sep 27, 2024
1 parent 25d2a77 commit 0170799
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
4 changes: 2 additions & 2 deletions object/site_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func refreshSiteMap() error {
}

newSiteMap[strings.ToLower(site.Domain)] = site
if site.EnableAlert {
newHealthCheckNeededDomains = append(newHealthCheckNeededDomains, site.Domain)
if !shouldStopHealthCheck(site) {
newHealthCheckNeededDomains = append(newHealthCheckNeededDomains, strings.ToLower(site.Domain))
}
for _, domain := range site.OtherDomains {
if domain != "" {
Expand Down
32 changes: 20 additions & 12 deletions object/site_timer_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package object

import (
"fmt"
"strings"
"time"

"github.com/casdoor/casdoor-go-sdk/casdoorsdk"
Expand Down Expand Up @@ -58,17 +59,19 @@ func healthCheck(site *Site, domain string) error {
if user == nil {
return fmt.Errorf("user not found")
}
if user.Email != "" {
err = casdoorsdk.SendEmail("CasWAF HealthCheck Check Alert", pingResponse, "CasWAF", user.Email)
}
if err != nil {
fmt.Println(err)
}
if user.Phone != "" {
err = casdoorsdk.SendSms(pingResponse, user.Phone)
}
if err != nil {
fmt.Println(err)
for _, provider := range site.AlertProviders {
if strings.HasPrefix(provider, "Email/") {
err := casdoorsdk.SendEmailByProvider("CasWAF HealthCheck Check Alert", pingResponse, "CasWAF", provider[6:], user.Email)
if err != nil {
fmt.Println(err)
}
}
if strings.HasPrefix(provider, "SMS/") {
err := casdoorsdk.SendSmsByProvider(pingResponse, provider[4:], user.Phone)
if err != nil {
fmt.Println(err)
}
}
}
return nil
}
Expand All @@ -83,7 +86,8 @@ func startHealthCheckLoop() {
go func() {
for {
site := GetSiteByDomain(domain)
if site == nil || !site.EnableAlert || site.Domain == "" || site.Status == "Inactive" {
if shouldStopHealthCheck(site) {
delete(healthCheckTryTimesMap, domain)
return
}

Expand All @@ -96,3 +100,7 @@ func startHealthCheckLoop() {
}()
}
}

func shouldStopHealthCheck(site *Site) bool {
return site == nil || !site.EnableAlert || site.Domain == "" || site.Status == "Inactive"
}

0 comments on commit 0170799

Please sign in to comment.