/
healthz_handler.go
46 lines (40 loc) · 1.24 KB
/
healthz_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package healthz
import (
"github.com/elliotxx/healthcheck"
"github.com/elliotxx/healthcheck/checks"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// Register registers the livez and readyz handlers to the specified
// router.
func Register(r *gin.RouterGroup, db *gorm.DB) {
r.GET("/livez", NewLivezHandler())
r.GET("/readyz", NewReadyzHandler(db))
}
// NewLivezHandler creates a new liveness check handler that can be
// used to check if the application is running.
func NewLivezHandler() gin.HandlerFunc {
conf := healthcheck.HandlerConfig{
Verbose: false,
// checkList is a list of healthcheck to run.
Checks: []checks.Check{
checks.NewPingCheck(),
},
FailureNotification: healthcheck.FailureNotification{Threshold: 1},
}
return healthcheck.NewHandler(conf)
}
// NewReadyzHandler creates a new readiness check handler that can be
// used to check if the application is ready to serve traffic.
func NewReadyzHandler(db *gorm.DB) gin.HandlerFunc {
conf := healthcheck.HandlerConfig{
Verbose: true,
// checkList is a list of healthcheck to run.
Checks: []checks.Check{
checks.NewPingCheck(),
NewGormDBCheck(db),
},
FailureNotification: healthcheck.FailureNotification{Threshold: 1},
}
return healthcheck.NewHandler(conf)
}