handlers

package
v2.1.4 Latest Latest
Warning

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

Go to latest
Published: Jul 24, 2023 License: MIT Imports: 5 Imported by: 12

README

handlers

The health library comes bundled with some no-thrills HTTP handlers that should fit most use cases.

Usage

After setting up an instance of health, register a healthcheck endpoint and point it at a handler func.

import (
    "github.com/InVisionApp/go-health/v2"
    "github.com/InVisionApp/go-health/v2/checkers"
    "github.com/InVisionApp/go-health/v2/handlers"
)

// create and configure a new health instance
h := health.New()
h.AddChecks(...)

// Register a new endpoint and have it use a pre-built handler
http.HandleFunc("/healthcheck", handlers.NewJSONHandlerFunc(h, nil))
http.ListenAndServe(":8080", nil)

Behavior

If any check fails that is configured as fatal - the handler will return a http.StatusInternalServerError; otherwise, it will return a http.StatusOK.

handlers.NewJSONHandlerFunc output example

{
    "details": {
        "bad-check": {
            "name": "bad-check",
            "status": "failed",
            "error": "Ran into error while performing 'GET' request: Get google.com: unsupported protocol scheme \"\"",
            "check_time": "2017-12-05T19:17:23.691637151-08:00"
        },
        "good-check": {
            "name": "good-check",
            "status": "ok",
            "check_time": "2017-12-05T19:17:23.857481271-08:00"
        }
    },
    "status": "ok"
}

handlers.NewBasicHandlerFunc example output

ok || failed

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewBasicHandlerFunc

func NewBasicHandlerFunc(h health.IHealth) http.HandlerFunc

NewBasicHandlerFunc will return an `http.HandlerFunc` that will write `ok` string + `http.StatusOK` to `rw“ if `h.Failed()` returns `false`; returns `error` + `http.StatusInternalServerError` if `h.Failed()` returns `true`.

func NewJSONHandlerFunc

func NewJSONHandlerFunc(h health.IHealth, custom map[string]interface{}) http.HandlerFunc

NewJSONHandlerFunc will return an `http.HandlerFunc` that will marshal and write the contents of `h.StateMapInterface()` to `rw` and set status code to

`http.StatusOK` if `h.Failed()` is `false` OR set status code to

`http.StatusInternalServerError` if `h.Failed` is `true`. It also accepts a set of optional custom fields to be added to the final JSON body

Types

This section is empty.

Jump to

Keyboard shortcuts

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