Skip to content

Commit 9c7d0fb

Browse files
committed
Add endpoint for changing health status
1 parent dadcd0a commit 9c7d0fb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

pkg/handler/handler.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ var statusCode = 200
1212

1313
// Health return the dobby health status
1414
func Health(c *gin.Context) {
15-
c.JSON(statusCode, gin.H{"healthy": true})
15+
healthy := true
16+
if statusCode != 200 {
17+
healthy = false
18+
}
19+
c.JSON(statusCode, gin.H{"healthy": healthy})
1620
}
1721

1822
// Version return dobby version
@@ -30,6 +34,18 @@ func Crash(_ *gin.Context) {
3034
log.Fatal("you asked me do so, killing myself :-)")
3135
}
3236

37+
// Sick will make dobby sick
38+
func Sick(c *gin.Context) {
39+
statusCode = 500
40+
c.JSON(200, gin.H{"status": "success"})
41+
}
42+
43+
// Healthy will make dobby healthy again
44+
func Healthy(c *gin.Context) {
45+
statusCode = 200
46+
c.JSON(200, gin.H{"status": "success"})
47+
}
48+
3349
func init() {
3450
healthy, err := strconv.ParseBool(os.Getenv("HEALTH"))
3551

pkg/server.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ func Run(bindAddress, port string) error {
1313
r.GET("/health", handler.Health)
1414
r.GET("/version", handler.Version)
1515
r.PUT("/state/crash", handler.Crash)
16+
r.PUT("/state/sick", handler.Sick)
17+
r.PUT("/state/healthy", handler.Healthy)
1618
return r.Run(fmt.Sprintf("%s:%s", bindAddress, port))
1719
}

0 commit comments

Comments
 (0)