Code Monkey home page Code Monkey logo

go-health's Introduction

Go health server

This repo contains a liveness and readiness server for Go.

Installation

go get github.com/moveaxlab/go-health

Usage

Health checks are defined with functions that take in input a context.Context and return a CheckStatus struct. The struct contains a Healthy boolean to indicate wether the monitored feature is healthy, and can provide additional info in the Info and Details fields. The Error field can contain additional info when the feature is unhealthy.

Health checks can be registered on the health check server, and will be called every time the /ready endpoint is called.

The /ready endpoint will return a 200 HTTP status if all health checks report a healthy status, or a 503 HTTP status otherwise. The response body will contain details on the health status of the service.

The /live endpoint always returns a 200 HTTP status.

Your services can implement the HealthCheck and HealthChecks methods that return respectively a single checker function or multiple checker functions. Services that implement this interface can be registered with the health check server using the Check and CheckAll methods.

package main

import (
	"os"
	"sync"
	"syscall"

	"github.com/moveaxlab/go-health"
)

type MyController interface {
	HealthCheck() health.Checker
}
	
type MyDatabase interface {
	HealthChecks() []health.Checker
}

func main() {
	server := health.NewHealthCheckServer(8080)
	wg := &sync.WaitGroup{}
	channel := make(chan os.Signal, 1)

	// register a custom health checker
	server.AddHealthChecker(func (_ context.Context) health.CheckStatus {
		return {
			Name: "custom",
			Healthy: true,
		}
	})

	var controller MyController
	var database MyDatabase

	// or register your services
	server.Check(controller)
	server.CheckAll(database)

	// start the server
	server.Start()

	// stop the server gracefully
	signal.Notify(channel, syscall.SIGINT)
	signal.Notify(channel, syscall.SIGTERM)

	wg.Add(1)
	go func() {
		defer wg.Done()
		select {
		case <-channel:
			err := server.Stop()
			if err != nil {
				fmt.Printf("server stop returned an error: %v", err)
			}
		}

	}()
	wg.Wait()
}

go-health's People

Contributors

asmeikal avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.