2025-10-20 08:54:21 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2025-10-31 12:23:48 +00:00
|
|
|
"log"
|
2025-10-20 08:54:21 +01:00
|
|
|
"net/http"
|
|
|
|
|
"time"
|
2025-11-11 13:53:14 +00:00
|
|
|
|
|
|
|
|
"github.com/juanatsap/cv-site/internal/models"
|
2025-10-20 08:54:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// HealthResponse represents the health check response
|
|
|
|
|
type HealthResponse struct {
|
2025-11-11 13:53:14 +00:00
|
|
|
Status string `json:"status"`
|
|
|
|
|
Timestamp time.Time `json:"timestamp"`
|
|
|
|
|
Version string `json:"version"`
|
|
|
|
|
Cache *CacheInfo `json:"cache,omitempty"`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// CacheInfo represents cache statistics
|
|
|
|
|
type CacheInfo struct {
|
|
|
|
|
Hits int64 `json:"hits"`
|
|
|
|
|
Misses int64 `json:"misses"`
|
|
|
|
|
Size int `json:"size"`
|
|
|
|
|
HitRate float64 `json:"hit_rate_percent"`
|
2025-10-20 08:54:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// HealthHandler handles health check requests
|
|
|
|
|
type HealthHandler struct {
|
|
|
|
|
version string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewHealthHandler creates a new health handler
|
|
|
|
|
func NewHealthHandler(version string) *HealthHandler {
|
|
|
|
|
return &HealthHandler{
|
|
|
|
|
version: version,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check performs a health check
|
|
|
|
|
func (h *HealthHandler) Check(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
response := HealthResponse{
|
|
|
|
|
Status: "ok",
|
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
|
Version: h.version,
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-11 13:53:14 +00:00
|
|
|
// Include cache stats if available
|
|
|
|
|
if cache := models.GetCache(); cache != nil {
|
|
|
|
|
stats := cache.GetStats()
|
|
|
|
|
response.Cache = &CacheInfo{
|
|
|
|
|
Hits: stats.Hits,
|
|
|
|
|
Misses: stats.Misses,
|
|
|
|
|
Size: stats.Size,
|
|
|
|
|
HitRate: cache.HitRate(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-10-20 08:54:21 +01:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
2025-10-31 12:23:48 +00:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
|
|
|
// Log error but don't change response status (already written)
|
|
|
|
|
log.Printf("ERROR encoding health check response: %v", err)
|
|
|
|
|
}
|
2025-10-20 08:54:21 +01:00
|
|
|
}
|