Skip to content

Commit bd6db8b

Browse files
authored
Merge pull request #1 from PythonGermany/refactor-endpoint-call-method
Refactor endpoint call method
2 parents 48f4339 + c4c8205 commit bd6db8b

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

config/endpoint/endpoint.go

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"maps"
1011
"math/rand"
1112
"net"
1213
"net/http"
@@ -460,22 +461,16 @@ func (e *Endpoint) call(result *Result) {
460461
var err error
461462
var certificate *x509.Certificate
462463
endpointType := e.Type()
463-
switch endpointType {
464-
case TypeHTTP:
465-
request = e.buildHTTPRequest()
466-
case TypeDomain:
467-
// domain expiration checked before call `call`
468-
return
469-
}
470464
startTime := time.Now()
471-
if endpointType == TypeDNS {
465+
switch endpointType {
466+
case TypeDNS:
472467
result.Connected, result.DNSRCode, result.Body, err = client.QueryDNS(e.DNSConfig.QueryType, e.DNSConfig.QueryName, e.URL)
473468
if err != nil {
474469
result.AddError(err.Error())
475470
return
476471
}
477472
result.Duration = time.Since(startTime)
478-
} else if endpointType == TypeSTARTTLS || endpointType == TypeTLS {
473+
case TypeSTARTTLS, TypeTLS:
479474
if endpointType == TypeSTARTTLS {
480475
result.Connected, certificate, err = client.CanPerformStartTLS(strings.TrimPrefix(e.URL, "starttls://"), e.ClientConfig)
481476
} else {
@@ -487,24 +482,20 @@ func (e *Endpoint) call(result *Result) {
487482
}
488483
result.Duration = time.Since(startTime)
489484
result.CertificateExpiration = time.Until(certificate.NotAfter)
490-
} else if endpointType == TypeTCP {
485+
case TypeTCP:
491486
result.Connected, result.Body = client.CanCreateNetworkConnection("tcp", strings.TrimPrefix(e.URL, "tcp://"), e.getParsedBody(), e.ClientConfig)
492487
result.Duration = time.Since(startTime)
493-
} else if endpointType == TypeUDP {
488+
case TypeUDP:
494489
result.Connected, result.Body = client.CanCreateNetworkConnection("udp", strings.TrimPrefix(e.URL, "udp://"), e.getParsedBody(), e.ClientConfig)
495490
result.Duration = time.Since(startTime)
496-
} else if endpointType == TypeSCTP {
491+
case TypeSCTP:
497492
result.Connected = client.CanCreateSCTPConnection(strings.TrimPrefix(e.URL, "sctp://"), e.ClientConfig)
498493
result.Duration = time.Since(startTime)
499-
} else if endpointType == TypeICMP {
494+
case TypeICMP:
500495
result.Connected, result.Duration = client.Ping(strings.TrimPrefix(e.URL, "icmp://"), e.ClientConfig)
501-
} else if endpointType == TypeWS {
496+
case TypeWS:
502497
wsHeaders := map[string]string{}
503-
if e.Headers != nil {
504-
for k, v := range e.Headers {
505-
wsHeaders[k] = v
506-
}
507-
}
498+
maps.Copy(wsHeaders, e.Headers)
508499
if _, exists := wsHeaders["User-Agent"]; !exists {
509500
wsHeaders["User-Agent"] = GatusUserAgent
510501
}
@@ -514,7 +505,7 @@ func (e *Endpoint) call(result *Result) {
514505
return
515506
}
516507
result.Duration = time.Since(startTime)
517-
} else if endpointType == TypeSSH {
508+
case TypeSSH:
518509
// If there's no username, password or private key specified, attempt to validate just the SSH banner
519510
if e.SSHConfig == nil || (len(e.SSHConfig.Username) == 0 && len(e.SSHConfig.Password) == 0 && len(e.SSHConfig.PrivateKey) == 0) {
520511
result.Connected, result.HTTPStatus, err = client.CheckSSHBanner(strings.TrimPrefix(e.URL, "ssh://"), e.ClientConfig)
@@ -543,7 +534,7 @@ func (e *Endpoint) call(result *Result) {
543534
result.Body = output
544535
}
545536
result.Duration = time.Since(startTime)
546-
} else if endpointType == TypeGRPC {
537+
case TypeGRPC:
547538
useTLS := strings.HasPrefix(e.URL, "grpcs://")
548539
address := strings.TrimPrefix(strings.TrimPrefix(e.URL, "grpcs://"), "grpc://")
549540
connected, status, err, duration := client.PerformGRPCHealthCheck(address, useTLS, e.ClientConfig)
@@ -554,9 +545,15 @@ func (e *Endpoint) call(result *Result) {
554545
result.Connected = connected
555546
result.Duration = duration
556547
if e.needsToReadBody() {
557-
result.Body = []byte(fmt.Sprintf("{\"status\":\"%s\"}", status))
548+
result.Body = fmt.Appendf(nil, "{\"status\":\"%s\"}", status)
558549
}
559-
} else {
550+
case TypeDomain:
551+
// domain expiration checked before call `call`
552+
return
553+
case TypeHTTP:
554+
request = e.buildHTTPRequest()
555+
fallthrough
556+
default:
560557
response, err = client.GetHTTPClient(e.ClientConfig).Do(request)
561558
result.Duration = time.Since(startTime)
562559
if err != nil {

0 commit comments

Comments
 (0)