Skip to content

Commit

Permalink
feat: added more logs for the status parsing function
Browse files Browse the repository at this point in the history
  • Loading branch information
KostLinux committed Aug 23, 2024
1 parent fde027d commit 1205d00
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"net/http"
"sync"

json "github.com/lookinlabs/status-page-middleware/pkg/json"

"github.com/gin-gonic/gin"
"github.com/lookinlabs/status-page-middleware/pkg/checks"
"github.com/lookinlabs/status-page-middleware/pkg/config"
json "github.com/lookinlabs/status-page-middleware/pkg/json"
"github.com/lookinlabs/status-page-middleware/pkg/logger"
"github.com/lookinlabs/status-page-middleware/pkg/model"
)
Expand All @@ -20,21 +19,23 @@ func Services(cfg *config.Environments, ctx *gin.Context) {
ctx.JSON(http.StatusInternalServerError, gin.H{
"message": "Error loading config",
})

return
}

var wg sync.WaitGroup
for i := range services {
for each := range services {
wg.Add(1)
go func(service *model.Service) {
defer wg.Done()

switch service.Type {
case "http":
method := "GET"
headers := map[string]string{}
body := ""
var basicAuth *model.BasicAuth

var basicAuth *model.BasicAuth
if service.Request != nil {
method = service.Request.Method
headers = service.Request.Headers
Expand All @@ -43,6 +44,7 @@ func Services(cfg *config.Environments, ctx *gin.Context) {
logger.Errorf("StatusMiddleware: Error encoding JSON request body: %v", err)
service.Status = "error"
service.Error = err.Error()

return
}
body = string(bodyBytes)
Expand All @@ -56,25 +58,31 @@ func Services(cfg *config.Environments, ctx *gin.Context) {
status, err := checks.HTTP(service.URL, method, headers, body, basicAuth)
service.Status = status
if err != nil {
logger.Errorf("StatusMiddleware: Error checking HTTP status: %v", err)
service.Error = err.Error()
}

case "dns":
status, err := checks.DNS(service.URL)
service.Status = status
if err != nil {
logger.Errorf("StatusMiddleware: Error checking DNS status: %v", err)
service.Error = err.Error()
}

case "tcp":
status, err := checks.TCP(service.URL)
service.Status = status
if err != nil {
logger.Errorf("StatusMiddleware: Error checking TCP status: %v", err)
service.Error = err.Error()
}

default:
service.Status = "unknown"
service.Error = "unknown service type"
}
}(&services[i])
}(&services[each])
}

wg.Wait()
Expand Down

0 comments on commit 1205d00

Please sign in to comment.