34 lines
718 B
Go
34 lines
718 B
Go
|
|
package handler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"net/http"
|
||
|
|
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
type HealthHandler struct {
|
||
|
|
db *gorm.DB
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewHealthHandler(db *gorm.DB) *HealthHandler {
|
||
|
|
return &HealthHandler{db: db}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *HealthHandler) Healthz(c *gin.Context) {
|
||
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||
|
|
}
|
||
|
|
|
||
|
|
func (h *HealthHandler) Readyz(c *gin.Context) {
|
||
|
|
sqlDB, err := h.db.DB()
|
||
|
|
if err != nil {
|
||
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "error", "error": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if err := sqlDB.PingContext(c.Request.Context()); err != nil {
|
||
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "error", "error": err.Error()})
|
||
|
|
return
|
||
|
|
}
|
||
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
||
|
|
}
|