diff --git a/.gitignore b/.gitignore index 8ab7f9b5..b2618a1a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .vscode -gatus \ No newline at end of file +gatus +db.db \ No newline at end of file diff --git a/controller/controller.go b/controller/controller.go index 6a1f05b5..039e5645 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -16,6 +16,7 @@ import ( "github.com/TwinProduction/gatus/security" "github.com/TwinProduction/gatus/watchdog" "github.com/TwinProduction/gocache" + "github.com/TwinProduction/health" "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus/promhttp" ) @@ -77,8 +78,8 @@ func CreateRouter(cfg *config.Config) *mux.Router { if cfg.Metrics { router.Handle("/metrics", promhttp.Handler()).Methods("GET") } + router.Handle("/health", health.Handler().WithJSON(true)).Methods("GET") router.HandleFunc("/favicon.ico", favIconHandler).Methods("GET") - router.HandleFunc("/health", healthHandler).Methods("GET") router.HandleFunc("/api/v1/statuses", secureIfNecessary(cfg, serviceStatusesHandler)).Methods("GET") // No GzipHandler for this one, because we cache the content router.HandleFunc("/api/v1/statuses/{key}", secureIfNecessary(cfg, GzipHandlerFunc(serviceStatusHandler))).Methods("GET") router.HandleFunc("/api/v1/badges/uptime/{duration}/{identifier}", badgeHandler).Methods("GET") @@ -167,12 +168,6 @@ func serviceStatusHandler(writer http.ResponseWriter, r *http.Request) { _, _ = writer.Write(output) } -func healthHandler(writer http.ResponseWriter, _ *http.Request) { - writer.Header().Add("Content-Type", "application/json") - writer.WriteHeader(http.StatusOK) - _, _ = writer.Write([]byte("{\"status\":\"UP\"}")) -} - // favIconHandler handles requests for /favicon.ico func favIconHandler(writer http.ResponseWriter, request *http.Request) { http.ServeFile(writer, request, staticFolder+"/favicon.ico") diff --git a/go.mod b/go.mod index 3650b7a3..83b954ee 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.15 require ( cloud.google.com/go v0.74.0 // indirect github.com/TwinProduction/gocache v1.2.1 + github.com/TwinProduction/health v1.0.0 github.com/go-ping/ping v0.0.0-20201115131931-3300c582a663 github.com/google/gofuzz v1.2.0 // indirect github.com/gorilla/mux v1.8.0 diff --git a/go.sum b/go.sum index afb90f86..b7f0c0f8 100644 --- a/go.sum +++ b/go.sum @@ -52,6 +52,8 @@ github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWX github.com/Shopify/toxiproxy v2.1.4+incompatible/go.mod h1:OXgGpZ6Cli1/URJOF1DMxUHB2q5Ap20/P/eIdh4G0pI= github.com/TwinProduction/gocache v1.2.1 h1:NAdMwO9SQEZFmX69YWx6fzhwb6fHakkLri0451c+V1w= github.com/TwinProduction/gocache v1.2.1/go.mod h1:6zkBoLjrFLkIISwkZTgLy67qliCGSon1xpORM4Ri5HM= +github.com/TwinProduction/health v1.0.0 h1:TVyYTAORQQZ8LaptX8jCHZRCGCAO6e+oJx19BUIzQYY= +github.com/TwinProduction/health v1.0.0/go.mod h1:ys4mYKUeEfYrWmkm60xLtPjTuLIEDQNBZaTZvenLG1c= github.com/VividCortex/gohistogram v1.0.0/go.mod h1:Pf5mBqqDxYaXu3hDrrU+w6nw50o/4+TcAqDqk/vUH7g= github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5/go.mod h1:SkGFH1ia65gfNATL8TAiHDNxPzPdmEL5uirI2Uyuz6c= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= diff --git a/vendor/github.com/TwinProduction/health/.gitattributes b/vendor/github.com/TwinProduction/health/.gitattributes new file mode 100644 index 00000000..fbd75d33 --- /dev/null +++ b/vendor/github.com/TwinProduction/health/.gitattributes @@ -0,0 +1 @@ +* text=lf \ No newline at end of file diff --git a/vendor/github.com/TwinProduction/health/.gitignore b/vendor/github.com/TwinProduction/health/.gitignore new file mode 100644 index 00000000..c38fa4e0 --- /dev/null +++ b/vendor/github.com/TwinProduction/health/.gitignore @@ -0,0 +1,2 @@ +.idea +*.iml diff --git a/vendor/github.com/TwinProduction/health/README.md b/vendor/github.com/TwinProduction/health/README.md new file mode 100644 index 00000000..aac0aadd --- /dev/null +++ b/vendor/github.com/TwinProduction/health/README.md @@ -0,0 +1,72 @@ +# health + +![build](https://github.com/TwinProduction/health/workflows/build/badge.svg?branch=master) +[![Go Report Card](https://goreportcard.com/badge/github.com/TwinProduction/health)](https://goreportcard.com/report/github.com/TwinProduction/health) +[![codecov](https://codecov.io/gh/TwinProduction/health/branch/master/graph/badge.svg)](https://codecov.io/gh/TwinProduction/health) +[![Go version](https://img.shields.io/github/go-mod/go-version/TwinProduction/health.svg)](https://github.com/TwinProduction/health) +[![Go Reference](https://pkg.go.dev/badge/github.com/TwinProduction/health.svg)](https://pkg.go.dev/github.com/TwinProduction/health) + +Health is a library used for creating a very simple health endpoint. + +While implementing a health endpoint is very simple, I've grown tired of implementing +it over and over again. + +## Installation + +``` +go get -u github.com/TwinProduction/health +``` + + +## Usage + +To retrieve the handler, you must use `health.Handler()` and are expected to pass it to the router like so: +```go +router := http.NewServeMux() +router.Handle("/health", health.Handler()) +server := &http.Server{ + Addr: ":8080", + Handler: router, +} +``` + +By default, the handler will return `UP` when the status is down, and `DOWN` when the status is down. +If you prefer using JSON, however, you may initialize the health handler like so: +```go +router.Handle("/health", health.Handler().WithJSON(true)) +``` +The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively. + +To change the health of the application, you can use `health.SetStatus()` where `` is one `health.Up` +or `health.Down`: + +```go +health.SetStatus(health.Up) +health.SetStatus(health.Down) +``` + +### Complete example + +```go +package main + +import ( + "net/http" + "time" + + "github.com/TwinProduction/health" +) + +func main() { + router := http.NewServeMux() + router.Handle("/health", health.Handler()) + server := &http.Server{ + Addr: "0.0.0.0:8080", + Handler: router, + ReadTimeout: 15 * time.Second, + WriteTimeout: 15 * time.Second, + IdleTimeout: 15 * time.Second, + } + server.ListenAndServe() +} +``` diff --git a/vendor/github.com/TwinProduction/health/go.mod b/vendor/github.com/TwinProduction/health/go.mod new file mode 100644 index 00000000..501a57d3 --- /dev/null +++ b/vendor/github.com/TwinProduction/health/go.mod @@ -0,0 +1,3 @@ +module github.com/TwinProduction/health + +go 1.15 diff --git a/vendor/github.com/TwinProduction/health/health.go b/vendor/github.com/TwinProduction/health/health.go new file mode 100644 index 00000000..fdd88d29 --- /dev/null +++ b/vendor/github.com/TwinProduction/health/health.go @@ -0,0 +1,51 @@ +package health + +import "net/http" + +var ( + handler = &healthHandler{ + useJSON: false, + status: Up, + } +) + +type healthHandler struct { + useJSON bool + status Status +} + +// WithJSON configures whether the handler should output a response in JSON or in raw text +// +// Defaults to false +func (h *healthHandler) WithJSON(v bool) *healthHandler { + h.useJSON = v + return h +} + +func (h healthHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) { + var status int + var body []byte + if h.status == Up { + status = http.StatusOK + } else { + status = http.StatusInternalServerError + } + if h.useJSON { + writer.Header().Set("Content-Type", "application/json") + body = []byte(`{"status":"` + h.status + `"}`) + } else { + body = []byte(h.status) + } + writer.WriteHeader(status) + _, _ = writer.Write(body) +} + +// Handler retrieves the health handler +func Handler() *healthHandler { + return handler +} + +// SetStatus sets the status to be reflected by the health handler +func SetStatus(status Status) { + handler.status = status +} diff --git a/vendor/github.com/TwinProduction/health/status.go b/vendor/github.com/TwinProduction/health/status.go new file mode 100644 index 00000000..3e2cbd9f --- /dev/null +++ b/vendor/github.com/TwinProduction/health/status.go @@ -0,0 +1,8 @@ +package health + +type Status string + +var ( + Down Status = "DOWN" + Up Status = "UP" +) diff --git a/vendor/modules.txt b/vendor/modules.txt index 983fd230..1aa60371 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -4,6 +4,9 @@ cloud.google.com/go/compute/metadata # github.com/TwinProduction/gocache v1.2.1 ## explicit github.com/TwinProduction/gocache +# github.com/TwinProduction/health v1.0.0 +## explicit +github.com/TwinProduction/health # github.com/beorn7/perks v1.0.1 github.com/beorn7/perks/quantile # github.com/cespare/xxhash/v2 v2.1.1