| 1 | # health |
| 2 |  |
| 3 | [](https://goreportcard.com/report/github.com/TwiN/health) |
| 4 | [](https://codecov.io/gh/TwiN/health) |
| 5 | [](https://github.com/TwiN/health) |
| 6 | [](https://pkg.go.dev/github.com/TwiN/health) |
| 7 | |
| 8 | Health is a library used for creating a very simple health endpoint. |
| 9 | |
| 10 | While implementing a health endpoint is very simple, I've grown tired of implementing |
| 11 | it over and over again. |
| 12 | |
| 13 | |
| 14 | ## Installation |
| 15 | ```console |
| 16 | go get -u github.com/TwiN/health |
| 17 | ``` |
| 18 | |
| 19 | |
| 20 | ## Usage |
| 21 | To retrieve the handler, you must use `health.Handler()` and are expected to pass it to the router like so: |
| 22 | ```go |
| 23 | router := http.NewServeMux() |
| 24 | router.Handle("/health", health.Handler()) |
| 25 | server := &http.Server{ |
| 26 | Addr: ":8080", |
| 27 | Handler: router, |
| 28 | } |
| 29 | ``` |
| 30 | |
| 31 | By default, the handler will return `UP` when the status is up, and `DOWN` when the status is down. |
| 32 | If you prefer using JSON, however, you may initialize the health handler like so: |
| 33 | ```go |
| 34 | router.Handle("/health", health.Handler().WithJSON(true)) |
| 35 | ``` |
| 36 | The above will cause the response body to become `{"status":"UP"}` and `{"status":"DOWN"}` for both status respectively, |
| 37 | unless there is a reason, in which case a reason set to `because` would return `{"status":"UP", "reason":"because"}` |
| 38 | and `{"status":"DOWN", "reason":"because"}` respectively. |
| 39 | |
| 40 | To set the health status to `DOWN` with a reason: |
| 41 | ```go |
| 42 | health.SetUnhealthy("<enter reason here>") |
| 43 | ``` |
| 44 | The string passed will be automatically set as the reason. |
| 45 | |
| 46 | In a similar fashion, to set the health status to `UP` and clear the reason: |
| 47 | ```go |
| 48 | health.SetHealthy() |
| 49 | ``` |
| 50 | |
| 51 | |
| 52 | Alternatively, to set the status and the reason individually you can use `health.SetStatus(<status>)` where `<status>` is `health.Up` |
| 53 | or `health.Down`: |
| 54 | ```go |
| 55 | health.SetStatus(health.Up) |
| 56 | health.SetStatus(health.Down) |
| 57 | ``` |
| 58 | As for the reason: |
| 59 | ```go |
| 60 | health.SetReason("database is unreachable") |
| 61 | ``` |
| 62 | |
| 63 | Generally speaking, you'd only want to include a reason if the status is `Down`, but you can do as you desire. |
| 64 | |
| 65 | For the sake of convenience, you can also use `health.SetStatusAndReason(<status>, <reason>)` instead of doing |
| 66 | `health.SetStatus(<status>)` and `health.SetReason(<reason>)` separately. |
| 67 | |
| 68 | |
| 69 | ### Complete example |
| 70 | ```go |
| 71 | package main |
| 72 | |
| 73 | import ( |
| 74 | "net/http" |
| 75 | "time" |
| 76 | |
| 77 | "github.com/TwiN/health" |
| 78 | ) |
| 79 | |
| 80 | func main() { |
| 81 | router := http.NewServeMux() |
| 82 | router.Handle("/health", health.Handler()) |
| 83 | server := &http.Server{ |
| 84 | Addr: "0.0.0.0:8080", |
| 85 | Handler: router, |
| 86 | ReadTimeout: 15 * time.Second, |
| 87 | WriteTimeout: 15 * time.Second, |
| 88 | IdleTimeout: 15 * time.Second, |
| 89 | } |
| 90 | server.ListenAndServe() |
| 91 | } |
| 92 | ``` |