health

package module
v1.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 3 Imported by: 4

README

health

test Go Report Card codecov Go version Go Reference

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/TwiN/health

Usage

To retrieve the handler, you must use health.Handler() and are expected to pass it to the router like so:

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 up, and DOWN when the status is down. If you prefer using JSON, however, you may initialize the health handler like so:

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, 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.

To set the health status to DOWN with a reason:

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 and clear the reason:

health.SetHealthy()

Alternatively, to set the status and the reason individually you can use health.SetStatus(<status>) where <status> is health.Up or health.Down:

health.SetStatus(health.Up)
health.SetStatus(health.Down)

As for the reason:

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.

For the sake of convenience, you can also use health.SetStatusAndReason(<status>, <reason>) instead of doing health.SetStatus(<status>) and health.SetReason(<reason>) separately.

Complete example
package main

import (
    "net/http"
    "time"

    "github.com/TwiN/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()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetReason added in v1.2.0

func GetReason() string

GetReason retrieves the current status returned by the health handler

func Handler

func Handler() *healthHandler

Handler retrieves the health handler

func SetHealthy added in v1.4.0

func SetHealthy()

SetHealthy sets the status to Up and the reason to a blank string

func SetReason added in v1.2.0

func SetReason(reason string)

SetReason sets a reason for the current status to be returned by the health handler

func SetStatus

func SetStatus(status Status)

SetStatus sets the status to be returned by the health handler

func SetStatusAndReason added in v1.3.0

func SetStatusAndReason(status Status, reason string)

SetStatusAndReason sets the status and reason to be returned by the health handler

func SetStatusAndResetReason added in v1.5.0

func SetStatusAndResetReason(status Status)

SetStatusAndResetReason sets the status and resets the reason to a blank string

func SetUnhealthy added in v1.4.0

func SetUnhealthy(reason string)

SetUnhealthy sets the status to Down and the reason to the string passed as parameter

Unlike SetHealthy, this function enforces setting a reason, because it's good practice to give at least a bit of information as to why an application is unhealthy, and this library attempts to promote good practices.

Types

type Status

type Status string
var (
	Down Status = "DOWN" // For when the application is unhealthy
	Up   Status = "UP"   // For when the application is healthy
)

func GetStatus added in v1.1.0

func GetStatus() Status

GetStatus retrieves the current status returned by the health handler

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL