mirror of
https://github.com/vcscsvcscs/GenerationsHeritage.git
synced 2025-08-14 23:09:07 +02:00
Add healthcheck endpoint
This commit is contained in:
54
backend/liveness/liveness.go
Normal file
54
backend/liveness/liveness.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package liveness
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"sync"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type HealthCheck interface {
|
||||
SetStatus(status string)
|
||||
GetStatus() string
|
||||
HealthCheckHandler() gin.HandlerFunc
|
||||
}
|
||||
|
||||
type healthCheck struct {
|
||||
status string
|
||||
sync sync.Mutex
|
||||
}
|
||||
|
||||
func New() HealthCheck {
|
||||
return &healthCheck{
|
||||
status: "ok",
|
||||
}
|
||||
}
|
||||
|
||||
func (hc *healthCheck) SetStatus(status string) {
|
||||
hc.sync.Lock()
|
||||
defer hc.sync.Unlock()
|
||||
|
||||
hc.status = status
|
||||
}
|
||||
|
||||
func (hc *healthCheck) GetStatus() string {
|
||||
hc.sync.Lock()
|
||||
defer hc.sync.Unlock()
|
||||
|
||||
return hc.status
|
||||
}
|
||||
|
||||
func (hc *healthCheck) HealthCheckHandler() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
switch hc.GetStatus() {
|
||||
case "nok":
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"status": hc.GetStatus(),
|
||||
})
|
||||
default:
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"status": hc.GetStatus(),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user