package handlers import ( "fmt" "runtime" "time" "github.com/gofiber/fiber/v3" ) func ErrorHandler(c fiber.Ctx, err error) error { code := fiber.StatusInternalServerError message := "Internal Server Error" if e, ok := err.(*fiber.Error); ok { code = e.Code message = e.Message } return c.Status(code).JSON(fiber.Map{ "error": message, "timestamp": time.Now().UTC(), "path": c.Path(), "method": c.Method(), "request_id": c.Get("X-Request-ID", ""), }) } func HealthCheck(c fiber.Ctx) error { uptime := time.Since(startTime) return c.JSON(fiber.Map{ "status": "ok", "timestamp": time.Now().UTC(), "uptime": uptime.String(), "service": "gateway", "version": "1.0.0", "checks": fiber.Map{ "database": "ok", "cache": "ok", }, }) } func Metrics(c fiber.Ctx) error { var m runtime.MemStats runtime.ReadMemStats(&m) return c.JSON(fiber.Map{ "system": fiber.Map{ "memory": fiber.Map{ "allocated": formatBytes(m.Alloc), "total_allocated": formatBytes(m.TotalAlloc), "system": formatBytes(m.Sys), "gc_runs": m.NumGC, }, "goroutines": runtime.NumGoroutine(), "cpu_count": runtime.NumCPU(), }, "service": fiber.Map{ "uptime": time.Since(startTime).String(), "version": "1.0.0", "timestamp": time.Now().UTC(), }, }) } var startTime = time.Now() func formatBytes(b uint64) string { const unit = 1024 if b < unit { return fmt.Sprintf("%d B", b) } div, exp := uint64(unit), 0 for n := b / unit; n >= unit; n /= unit { div *= unit exp++ } return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "KMGTPE"[exp]) }