2021-02-13 05:29:21 +01:00
|
|
|
# health
|
2022-04-11 07:39:47 +02:00
|
|
|
![test](https://github.com/TwiN/health/workflows/test/badge.svg?branch=master)
|
2021-10-08 03:28:04 +02:00
|
|
|
[![Go Report Card](https://goreportcard.com/badge/github.com/TwiN/health)](https://goreportcard.com/report/github.com/TwiN/health)
|
|
|
|
[![codecov](https://codecov.io/gh/TwiN/health/branch/master/graph/badge.svg)](https://codecov.io/gh/TwiN/health)
|
|
|
|
[![Go version](https://img.shields.io/github/go-mod/go-version/TwiN/health.svg)](https://github.com/TwiN/health)
|
|
|
|
[![Go Reference](https://pkg.go.dev/badge/github.com/TwiN/health.svg)](https://pkg.go.dev/github.com/TwiN/health)
|
2021-02-13 05:29:21 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
## Installation
|
2022-04-11 07:39:47 +02:00
|
|
|
```console
|
2021-10-08 03:28:04 +02:00
|
|
|
go get -u github.com/TwiN/health
|
2021-02-13 05:29:21 +01:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## 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{
|
2021-10-08 03:28:04 +02:00
|
|
|
Addr: ":8080",
|
|
|
|
Handler: router,
|
2021-02-13 05:29:21 +01:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
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))
|
|
|
|
```
|
2021-11-20 05:43:24 +01:00
|
|
|
The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively,
|
|
|
|
unless there is a reason, in which case a reason set to `because` would return `{"status":"UP", "reason":"because"}`
|
|
|
|
and `{"status":"DOWN", "reason":"because"}` respectively.
|
2021-02-13 05:29:21 +01:00
|
|
|
|
2022-04-11 07:39:47 +02:00
|
|
|
To set the health status to `DOWN`, you may use `health.SetUnhealthy("<enter reason here>`)` -- the
|
|
|
|
string passed will be automatically set as the reason. In a similar fashion, to set the health status to `UP`,
|
|
|
|
you may use `health.SetHealthy()`.
|
|
|
|
|
|
|
|
Alternatively, to change the health of the application, you can use `health.SetStatus(<status>)` where `<status>` is `health.Up`
|
2021-02-13 05:29:21 +01:00
|
|
|
or `health.Down`:
|
|
|
|
```go
|
|
|
|
health.SetStatus(health.Up)
|
|
|
|
health.SetStatus(health.Down)
|
|
|
|
```
|
|
|
|
|
2021-11-20 05:43:24 +01:00
|
|
|
As for the reason:
|
|
|
|
```go
|
|
|
|
health.SetReason("database is unreachable")
|
|
|
|
```
|
|
|
|
|
|
|
|
Generally speaking, you'd only want to include a reason if the status is `Down`, but you can do as you desire.
|
|
|
|
|
2022-04-11 07:39:47 +02:00
|
|
|
For the sake of convenience, you can also use `health.SetStatusAndReason(<status>, <reason>)` instead of doing
|
|
|
|
`health.SetStatus(<status>)` and `health.SetReason(<reason>)` separately.
|
|
|
|
|
2021-02-13 05:29:21 +01:00
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
### Complete example
|
2021-02-13 05:29:21 +01:00
|
|
|
```go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-10-08 03:28:04 +02:00
|
|
|
"net/http"
|
|
|
|
"time"
|
2021-02-13 05:29:21 +01:00
|
|
|
|
2021-10-08 03:28:04 +02:00
|
|
|
"github.com/TwiN/health"
|
2021-02-13 05:29:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-10-08 03:28:04 +02:00
|
|
|
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()
|
2021-02-13 05:29:21 +01:00
|
|
|
}
|
|
|
|
```
|