ux: Improve endpoint validation by checking type on start

This commit is contained in:
TwiN 2022-09-01 20:59:09 -04:00
parent 5c6bf84106
commit 52d7cb6f04
2 changed files with 11 additions and 3 deletions

View File

@ -1125,7 +1125,7 @@ endpoints:
conditions:
- "[STATUS] == 200"
`))
if err != core.ErrEndpointWithNoName {
if err == nil {
t.Error("should've returned an error")
}
}

View File

@ -33,13 +33,13 @@ const (
// GatusUserAgent is the default user agent that Gatus uses to send requests.
GatusUserAgent = "Gatus/1.0"
// EndpointType enum for the endpoint type.
EndpointTypeDNS EndpointType = "DNS"
EndpointTypeTCP EndpointType = "TCP"
EndpointTypeICMP EndpointType = "ICMP"
EndpointTypeSTARTTLS EndpointType = "STARTTLS"
EndpointTypeTLS EndpointType = "TLS"
EndpointTypeHTTP EndpointType = "HTTP"
EndpointTypeUNKNOWN EndpointType = "UNKNOWN"
)
var (
@ -54,6 +54,9 @@ var (
// ErrEndpointWithInvalidNameOrGroup is the error with which Gatus will panic if an endpoint has an invalid character where it shouldn't
ErrEndpointWithInvalidNameOrGroup = errors.New("endpoint name and group must not have \" or \\")
// ErrUnknownEndpointType is the error with which Gatus will panic if an endpoint has an unknown type
ErrUnknownEndpointType = errors.New("unknown endpoint type")
)
// Endpoint is the configuration of a monitored
@ -128,8 +131,10 @@ func (endpoint Endpoint) Type() EndpointType {
return EndpointTypeSTARTTLS
case strings.HasPrefix(endpoint.URL, "tls://"):
return EndpointTypeTLS
default:
case strings.HasPrefix(endpoint.URL, "http://") || strings.HasPrefix(endpoint.URL, "https://"):
return EndpointTypeHTTP
default:
return EndpointTypeUNKNOWN
}
}
@ -188,6 +193,9 @@ func (endpoint *Endpoint) ValidateAndSetDefaults() error {
if endpoint.DNS != nil {
return endpoint.DNS.validateAndSetDefault()
}
if endpoint.Type() == EndpointTypeUNKNOWN {
return ErrUnknownEndpointType
}
// Make sure that the request can be created
_, err := http.NewRequest(endpoint.Method, endpoint.URL, bytes.NewBuffer([]byte(endpoint.Body)))
if err != nil {