53 lines
No EOL
1 KiB
Go
53 lines
No EOL
1 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type HealthHandler struct {
|
|
startTime time.Time
|
|
configDir string
|
|
}
|
|
|
|
func NewHealthHandler() *HealthHandler {
|
|
return &HealthHandler{startTime: time.Now()}
|
|
}
|
|
|
|
func (h *HealthHandler) SetConfigDir(dir string) {
|
|
h.configDir = dir
|
|
}
|
|
|
|
func (h *HealthHandler) Health(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ok",
|
|
"uptime": time.Since(h.startTime).String(),
|
|
"timestamp": time.Now().Format(time.RFC3339),
|
|
"version": "dev",
|
|
})
|
|
}
|
|
|
|
func (h *HealthHandler) Ready(c *gin.Context) {
|
|
configStatus := "ok"
|
|
if h.configDir != "" {
|
|
if _, err := os.Stat(h.configDir); err != nil {
|
|
configStatus = "error"
|
|
} else if f, err := os.CreateTemp(h.configDir, ".writetest"); err != nil {
|
|
configStatus = "not writable"
|
|
} else {
|
|
f.Close()
|
|
os.Remove(f.Name())
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"status": "ready",
|
|
"checks": gin.H{
|
|
"database": "ok",
|
|
"traefik": "ok",
|
|
"configDir": configStatus,
|
|
},
|
|
})
|
|
} |