Skip to content

elliotxx/healthcheck

Repository files navigation

Low-dependency, High-efficiency, Kubernetes-style healthcheck for Gin Framework


This module will create a kubernetes-style endpoints for Gin framework. Inspired by tavsec/gin-healthcheck.

📜 Language

English | 简体中文

✨ Core Features

  • ⚡ Lightweight
  • 🌲 Low dependency (only gin)
  • 🔥 High efficiency
  • 🔨 Highly customizable
  • ⎈ Kubernetes-style

⚙️ Usage

go get github.com/elliotxx/healthcheck

📖 Examples

Default Check

package main

import (
	"github.com/elliotxx/healthcheck"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	healthcheck.Register(&r.RouterGroup)

	r.Run()
}

This will register the default healthcheck endpoint (/healthz) to the route. The path can be customized using healthcheck.Config structure.

Or use NewHandler() function directly:

package main

import (
	"github.com/elliotxx/healthcheck"
	"github.com/elliotxx/healthcheck/checks"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()

	r.GET("livez", healthcheck.NewHandler(healthcheck.NewDefaultHandlerConfig()))

	readyzChecks := []checks.Check{checks.NewPingCheck(), checks.NewEnvCheck("DB_HOST")}
	r.GET("readyz", healthcheck.NewHandler(healthcheck.NewDefaultHandlerConfigFor(readyzChecks...)))

	r.Run()
}

Output:

$ curl -k http://localhost:8080/readyz
OK

$ curl -k http://localhost:8080/readyz?verbose
[+] Ping ok
[-] Env-DB_HOST ok
health check failed

$ curl -k http://localhost:8080/readyz?verbose&excludes=Env-DB_HOST
[+] Ping ok
health check passed

Enjoy it!

More examples can be seen: ./example_test.go