From 52d7cb6f0489baad64e3c1b73df9a8b258bd717e Mon Sep 17 00:00:00 2001 From: TwiN Date: Thu, 1 Sep 2022 20:59:09 -0400 Subject: [PATCH] ux: Improve endpoint validation by checking type on start --- config/config_test.go | 2 +- core/endpoint.go | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/config/config_test.go b/config/config_test.go index 1a013692..1273eb54 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1125,7 +1125,7 @@ endpoints: conditions: - "[STATUS] == 200" `)) - if err != core.ErrEndpointWithNoName { + if err == nil { t.Error("should've returned an error") } } diff --git a/core/endpoint.go b/core/endpoint.go index b53e3659..edfdf436 100644 --- a/core/endpoint.go +++ b/core/endpoint.go @@ -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 {