From 718ebe84d36d4656d8133a633d167bec9d0401b8 Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Fri, 27 Nov 2020 13:31:50 -0800 Subject: [PATCH 1/9] Update routes.go Added /api/services/{id}/responsecode --- handlers/routes.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/handlers/routes.go b/handlers/routes.go index ed233d0e..2975e0db 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -137,7 +137,8 @@ func Router() *mux.Router { api.Handle("/api/services/{id}/failures", authenticated(servicesDeleteFailuresHandler, false)).Methods("DELETE") api.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET") api.Handle("/api/services/{id}/hits", authenticated(apiServiceHitsDeleteHandler, false)).Methods("DELETE") - + api.Handle("/api/services/{id}/responsecode", scoped(apiServiceResponseCodeHandler)).Methods("GET") + // API SERVICE CHART DATA Routes api.Handle("/api/services/{id}/hits_data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET") api.Handle("/api/services/{id}/failure_data", cached("30s", "application/json", apiServiceFailureDataHandler)).Methods("GET") From 1e66d3a470388a44e1c5852d66bd2a48b607c768 Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Fri, 27 Nov 2020 14:59:24 -0800 Subject: [PATCH 2/9] Update handlers.go to add returnResponseCode --- handlers/handlers.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/handlers/handlers.go b/handlers/handlers.go index 2a2f22b0..29a26d7e 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -212,6 +212,27 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(d) } +func returnResponseCode(d interface{}, w http.ResponseWriter, r *http.Request) { + if e, ok := d.(errors.Error); ok { + w.WriteHeader(e.Status()) + json.NewEncoder(w).Encode(e) + return + } + if e, ok := d.(error); ok { + w.WriteHeader(500) + json.NewEncoder(w).Encode(errors.New(e.Error())) + return + } + + // https://golang.org/src/net/http/server.go?s=3003:5866#L150 + // Go does not currently + // support sending user-defined 1xx informational headers, + // with the exception of 100-continue response header that the + // Server sends automatically when the Request.Body is read. + w.WriteHeader(http.StatusOK) + json.NewEncoder(w).Encode(d) +} + // error404Handler is a HTTP handler for 404 error pages func error404Handler(w http.ResponseWriter, r *http.Request) { if usingSSL { From af1d6aa8e0df416c0b4bd464bba8d6a1993b5c25 Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Fri, 27 Nov 2020 15:19:52 -0800 Subject: [PATCH 3/9] Update handlers.go Added teapot as default response --- handlers/handlers.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 29a26d7e..5da8ffec 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -212,7 +212,7 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(d) } -func returnResponseCode(d interface{}, w http.ResponseWriter, r *http.Request) { +func returnResponseCode(s *Service, w http.ResponseWriter, r *http.Request) { if e, ok := d.(errors.Error); ok { w.WriteHeader(e.Status()) json.NewEncoder(w).Encode(e) @@ -224,13 +224,15 @@ func returnResponseCode(d interface{}, w http.ResponseWriter, r *http.Request) { return } - // https://golang.org/src/net/http/server.go?s=3003:5866#L150 // Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the // Server sends automatically when the Request.Body is read. - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(d) + // https://golang.org/src/net/http/server.go?s=3003:5866#L150 + if LastStatusCode >= 100 { + w.WriteHeader(s.LastStatusCode) + } + return w.WriteHeader(http.StatusTeapot) } // error404Handler is a HTTP handler for 404 error pages From 5f86b6e1dd1643babac6c7cf3fef3d8fee38ae60 Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Fri, 27 Nov 2020 15:38:21 -0800 Subject: [PATCH 4/9] Update routes.go apiServiceResponseCodeHandler to use authenticated --- handlers/routes.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/handlers/routes.go b/handlers/routes.go index 2975e0db..296a4391 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -137,7 +137,7 @@ func Router() *mux.Router { api.Handle("/api/services/{id}/failures", authenticated(servicesDeleteFailuresHandler, false)).Methods("DELETE") api.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET") api.Handle("/api/services/{id}/hits", authenticated(apiServiceHitsDeleteHandler, false)).Methods("DELETE") - api.Handle("/api/services/{id}/responsecode", scoped(apiServiceResponseCodeHandler)).Methods("GET") + api.Handle("/api/services/{id}/responsecode", authenticated(apiServiceResponseCodeHandler, false)).Methods("GET") // API SERVICE CHART DATA Routes api.Handle("/api/services/{id}/hits_data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET") From b63130aa30d4403cae78e964d4a5710f0068e412 Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Fri, 27 Nov 2020 16:42:29 -0800 Subject: [PATCH 5/9] Update services.go Added changes to handle response --- handlers/services.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/handlers/services.go b/handlers/services.go index 135ddccf..8e77f5a9 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -316,3 +316,13 @@ func apiServiceHitsHandler(r *http.Request) interface{} { query.Find(&hts) return hts } + +func apiServiceResponseCodeHandler(w http.ResponseWriter, r *http.Request) interface{} { + + service, err := findService(r) + if err != nil { + return err + } + + return returnResponseCode(service, w, r) +} From 087a3339477aa2fb797aa9b87661ebcb348cc65c Mon Sep 17 00:00:00 2001 From: Andrew Gerhold Date: Sun, 29 Nov 2020 19:13:07 -0800 Subject: [PATCH 6/9] Update handlers.go to add DownText --- handlers/handlers.go | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 5da8ffec..b9fd596d 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -213,17 +213,6 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) { } func returnResponseCode(s *Service, w http.ResponseWriter, r *http.Request) { - if e, ok := d.(errors.Error); ok { - w.WriteHeader(e.Status()) - json.NewEncoder(w).Encode(e) - return - } - if e, ok := d.(error); ok { - w.WriteHeader(500) - json.NewEncoder(w).Encode(errors.New(e.Error())) - return - } - // Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the @@ -231,8 +220,12 @@ func returnResponseCode(s *Service, w http.ResponseWriter, r *http.Request) { // https://golang.org/src/net/http/server.go?s=3003:5866#L150 if LastStatusCode >= 100 { w.WriteHeader(s.LastStatusCode) + w.Write([]byte(s.DownText)) + return } - return w.WriteHeader(http.StatusTeapot) + + w.WriteHeader(http.StatusTeapot) + w.Write([]byte("☄ Last Status Code Not Set!")) } // error404Handler is a HTTP handler for 404 error pages From fc89bcdd31de82eecfaf33a5f4c475b36a369974 Mon Sep 17 00:00:00 2001 From: agerho000 Date: Mon, 30 Nov 2020 04:17:55 +0000 Subject: [PATCH 7/9] Fixed type errors --- handlers/handlers.go | 10 ++++++---- handlers/services.go | 12 +++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index b9fd596d..1f0ef42d 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -3,13 +3,14 @@ package handlers import ( "encoding/json" "fmt" - "github.com/statping/statping/types/errors" "html/template" "net/http" "path" "time" "github.com/statping/statping/source" + "github.com/statping/statping/types/errors" + "github.com/statping/statping/types/services" "github.com/statping/statping/utils" ) @@ -212,18 +213,19 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(d) } -func returnResponseCode(s *Service, w http.ResponseWriter, r *http.Request) { +func returnResponseCode(s *services.Service, w http.ResponseWriter, r *http.Request) { // Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the // Server sends automatically when the Request.Body is read. // https://golang.org/src/net/http/server.go?s=3003:5866#L150 - if LastStatusCode >= 100 { + + if s.LastStatusCode >= 100 { w.WriteHeader(s.LastStatusCode) w.Write([]byte(s.DownText)) return } - + w.WriteHeader(http.StatusTeapot) w.Write([]byte("☄ Last Status Code Not Set!")) } diff --git a/handlers/services.go b/handlers/services.go index 8e77f5a9..5fcd2a6a 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -1,6 +1,8 @@ package handlers import ( + "net/http" + "github.com/gorilla/mux" "github.com/statping/statping/database" "github.com/statping/statping/types/errors" @@ -8,7 +10,6 @@ import ( "github.com/statping/statping/types/hits" "github.com/statping/statping/types/services" "github.com/statping/statping/utils" - "net/http" ) type serviceOrder struct { @@ -317,12 +318,13 @@ func apiServiceHitsHandler(r *http.Request) interface{} { return hts } -func apiServiceResponseCodeHandler(w http.ResponseWriter, r *http.Request) interface{} { +func apiServiceResponseCodeHandler(w http.ResponseWriter, r *http.Request) { service, err := findService(r) if err != nil { - return err + sendErrorJson(err, w, r) + return } - - return returnResponseCode(service, w, r) + + returnResponseCode(service, w, r) } From 9452a37b2a917312fd356c574288459c7f0350c0 Mon Sep 17 00:00:00 2001 From: agerho000 Date: Mon, 30 Nov 2020 05:28:26 +0000 Subject: [PATCH 8/9] updated method naming --- handlers/handlers.go | 2 +- handlers/routes.go | 11 ++++++----- handlers/services.go | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/handlers/handlers.go b/handlers/handlers.go index 1f0ef42d..57c8e2d9 100644 --- a/handlers/handlers.go +++ b/handlers/handlers.go @@ -213,7 +213,7 @@ func returnJson(d interface{}, w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(d) } -func returnResponseCode(s *services.Service, w http.ResponseWriter, r *http.Request) { +func returnLastResponse(s *services.Service, w http.ResponseWriter, r *http.Request) { // Go does not currently // support sending user-defined 1xx informational headers, // with the exception of 100-continue response header that the diff --git a/handlers/routes.go b/handlers/routes.go index 296a4391..9dd01c6f 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -1,15 +1,16 @@ package handlers import ( + "net/http" + "net/http/pprof" + "time" + sentryhttp "github.com/getsentry/sentry-go/http" "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/statping/statping/source" "github.com/statping/statping/types/core" "github.com/statping/statping/utils" - "net/http" - "net/http/pprof" - "time" _ "github.com/statping/statping/types/metrics" ) @@ -137,8 +138,8 @@ func Router() *mux.Router { api.Handle("/api/services/{id}/failures", authenticated(servicesDeleteFailuresHandler, false)).Methods("DELETE") api.Handle("/api/services/{id}/hits", scoped(apiServiceHitsHandler)).Methods("GET") api.Handle("/api/services/{id}/hits", authenticated(apiServiceHitsDeleteHandler, false)).Methods("DELETE") - api.Handle("/api/services/{id}/responsecode", authenticated(apiServiceResponseCodeHandler, false)).Methods("GET") - + api.Handle("/api/services/{id}/lastresponse", authenticated(apiServiceLastResponseHandler, false)).Methods("GET") + // API SERVICE CHART DATA Routes api.Handle("/api/services/{id}/hits_data", cached("30s", "application/json", apiServiceDataHandler)).Methods("GET") api.Handle("/api/services/{id}/failure_data", cached("30s", "application/json", apiServiceFailureDataHandler)).Methods("GET") diff --git a/handlers/services.go b/handlers/services.go index 5fcd2a6a..f73f8887 100644 --- a/handlers/services.go +++ b/handlers/services.go @@ -318,7 +318,7 @@ func apiServiceHitsHandler(r *http.Request) interface{} { return hts } -func apiServiceResponseCodeHandler(w http.ResponseWriter, r *http.Request) { +func apiServiceLastResponseHandler(w http.ResponseWriter, r *http.Request) { service, err := findService(r) if err != nil { @@ -326,5 +326,5 @@ func apiServiceResponseCodeHandler(w http.ResponseWriter, r *http.Request) { return } - returnResponseCode(service, w, r) + returnLastResponse(service, w, r) } From d8c12c77e833c1b8d14a9fd1ed72763fa03573f9 Mon Sep 17 00:00:00 2001 From: zeushammer Date: Wed, 20 Jan 2021 19:12:32 -0800 Subject: [PATCH 9/9] added necessary imports --- handlers/routes.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/handlers/routes.go b/handlers/routes.go index 69c40dac..6f708498 100644 --- a/handlers/routes.go +++ b/handlers/routes.go @@ -1,6 +1,8 @@ package handlers import ( + "net/http" + "net/http/pprof" "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp"