Use TwinProduction/health for health endpoint

This commit is contained in:
TwinProduction
2021-02-12 23:29:21 -05:00
parent 9cdef02bdc
commit ea9623f695
11 changed files with 147 additions and 8 deletions

View File

@ -0,0 +1 @@
* text=lf

2
vendor/github.com/TwinProduction/health/.gitignore generated vendored Normal file
View File

@ -0,0 +1,2 @@
.idea
*.iml

72
vendor/github.com/TwinProduction/health/README.md generated vendored Normal file
View File

@ -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(<status>)` where `<status>` 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()
}
```

3
vendor/github.com/TwinProduction/health/go.mod generated vendored Normal file
View File

@ -0,0 +1,3 @@
module github.com/TwinProduction/health
go 1.15

51
vendor/github.com/TwinProduction/health/health.go generated vendored Normal file
View File

@ -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
}

8
vendor/github.com/TwinProduction/health/status.go generated vendored Normal file
View File

@ -0,0 +1,8 @@
package health
type Status string
var (
Down Status = "DOWN"
Up Status = "UP"
)

3
vendor/modules.txt vendored
View File

@ -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