status

package module
v0.0.0-...-49e379a Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 8 Imported by: 0

README

status

Run Tests codecov Go Report Card GoDoc

Example

package main

import (
	"context"
	"errors"
	"log"
	"math/rand"
	"net/http"

	"github.com/denchenko/status"
)

func main() {
	healthChecker := status.NewHealthChecker().
		WithTarget("db", status.TargetImportanceHigh, func(ctx context.Context) error {
			return generateRandomError()
		}).
		WithTarget("network", status.TargetImportanceLow, func(ctx context.Context) error {
			return generateRandomError()
		})

	statusPage, err := status.NewPage()
	if err != nil {
		log.Printf("constructing page: %v", err)
		return
	}
	statusPage.WithHealthChecker(healthChecker)
	statusPage.WithURL("Swagger", "/swagger")

	http.HandleFunc("/health", healthChecker.Handler())
	http.HandleFunc("/status", statusPage.Handler())

	http.ListenAndServe(":8080", nil)
}

func generateRandomError() error {
	if rand.Intn(2) == 0 {
		return nil
	}

	return errors.New("dependency is not healthy")
}

Documentation

Index

Constants

View Source
const (
	TargetImportanceLow  = TargetImportance("low")
	TargetImportanceHigh = TargetImportance("high")
)
View Source
const (
	HealthTargetStatusOk   = HealthTargetStatus("ok")
	HealthTargetStatusFail = HealthTargetStatus("fail")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type HealthCheckFunc

type HealthCheckFunc func(ctx context.Context) error

type HealthCheckResult

type HealthCheckResult struct {
	Target       HealthTarget       `json:"target"`
	Status       HealthTargetStatus `json:"status"`
	ErrorMessage string             `json:"error,omitempty"`
	// contains filtered or unexported fields
}

type HealthChecker

type HealthChecker struct {
	// contains filtered or unexported fields
}

func NewHealthChecker

func NewHealthChecker() *HealthChecker

func (*HealthChecker) Check

func (*HealthChecker) Handler

func (c *HealthChecker) Handler() http.HandlerFunc

func (*HealthChecker) WithTarget

func (c *HealthChecker) WithTarget(name string, importance TargetImportance, check HealthCheckFunc) *HealthChecker

type HealthTarget

type HealthTarget struct {
	Name       string           `json:"name"`
	Importance TargetImportance `json:"importance"`
	// contains filtered or unexported fields
}

type HealthTargetStatus

type HealthTargetStatus string

type Page

type Page struct {
	// contains filtered or unexported fields
}

func NewPage

func NewPage() (*Page, error)

func (*Page) Handler

func (p *Page) Handler() http.HandlerFunc

func (*Page) WithHealthChecker

func (p *Page) WithHealthChecker(hc *HealthChecker)

func (*Page) WithURL

func (p *Page) WithURL(name, url string)

type TargetImportance

type TargetImportance string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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