38 lines
609 B
Go
38 lines
609 B
Go
package health
|
|
|
|
import (
|
|
"database/sql"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
func HealthHandler(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "OK",
|
|
})
|
|
}
|
|
|
|
type Handler struct {
|
|
db *sql.DB
|
|
}
|
|
|
|
func NewHandler(db *sql.DB) *Handler {
|
|
return &Handler{db: db}
|
|
}
|
|
|
|
func (h *Handler) Check(c *gin.Context) {
|
|
// Kiểm tra kết nối database
|
|
err := h.db.PingContext(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusServiceUnavailable, gin.H{
|
|
"status": "Database connection failed",
|
|
"error": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "OK",
|
|
})
|
|
}
|