health

package
v0.0.0-...-095633c Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: Apache-2.0, Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package health contains simple utilities for implementing a /health endpoint that adheres to the requirements defined in the draft IETF network working group standard, Health Check Response Format for HTTP APIs.

https://tools.ietf.org/id/draft-inadarei-api-health-check-01.html

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PassHandler

type PassHandler struct{}

PassHandler implements a simple handler that returns the most basic health response with a status of 'pass'.

func (PassHandler) ServeHTTP

func (h PassHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Response

type Response struct {
	Status Status `json:"status"`
}

Response implements the most basic required fields for the health response based on the format defined in the draft IETF network working group standard, Health Check Response Format for HTTP APIs.

https://tools.ietf.org/id/draft-inadarei-api-health-check-01.html

Example
package main

import (
	"fmt"
	"io/ioutil"
	"net/http"
	"net/http/httptest"

	supporthttp "github.com/shantanu-hashcash/go/support/http"
	"github.com/shantanu-hashcash/go/support/log"
	"github.com/shantanu-hashcash/go/support/render/health"
	"github.com/shantanu-hashcash/go/support/render/httpjson"
)

func main() {
	mux := supporthttp.NewAPIMux(log.DefaultLogger)

	mux.Get("/health", func(w http.ResponseWriter, r *http.Request) {
		healthCheckResult := false
		response := health.Response{}
		if healthCheckResult {
			response.Status = health.StatusPass
		} else {
			response.Status = health.StatusFail
		}
		httpjson.Render(w, response, httpjson.HEALTHJSON)
	})

	r := httptest.NewRequest("GET", "/health", nil)
	w := httptest.NewRecorder()
	mux.ServeHTTP(w, r)
	resp := w.Result()

	fmt.Println("Content Type:", resp.Header.Get("Content-Type"))
	fmt.Println("Status Code:", resp.StatusCode)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println("Body:", string(body))

}
Output:

Content Type: application/health+json; charset=utf-8
Status Code: 200
Body: {
  "status": "fail"
}

type Status

type Status string

Status indicates whether the service is health or not.

const (
	// StatusPass indicates that the service is healthy.
	StatusPass Status = "pass"
	// StatusFail indicates that the service is unhealthy.
	StatusFail Status = "fail"
)

Jump to

Keyboard shortcuts

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