Files
fiber/middleware/healthcheck/healthcheck.go
zeekayandHanzo Dev f60e254acb module: rename to github.com/zap-proto/fiber/v3
Fork of gofiber/fiber v3 based on the v3.2.0 tag. Rewrites all self-imports
github.com/gofiber/fiber/v3 -> github.com/zap-proto/fiber/v3. Upstream
gofiber/schema and gofiber/utils/v2 dependencies are left untouched.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-10 12:49:35 -07:00

54 lines
1.3 KiB
Go

package healthcheck
import (
"github.com/zap-proto/fiber/v3"
)
// healthResponse represents the JSON/XML/MsgPack/CBOR response structure.
type healthResponse struct {
Status string `json:"status" xml:"status" msgpack:"status" cbor:"status"`
}
// New returns a health-check handler that responds based on the provided
// configuration.
func New(config ...Config) fiber.Handler {
cfg := configDefault(config...)
return func(c fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}
if c.Method() != fiber.MethodGet {
return c.Next()
}
healthy := cfg.Probe(c)
statusCode := fiber.StatusOK
statusMessage := "OK"
if !healthy {
statusCode = fiber.StatusServiceUnavailable
statusMessage = "Service Unavailable"
}
// Set the status code
c.Status(statusCode)
// Return response based on configured format
switch cfg.ResponseFormat {
case FormatJSON:
return c.JSON(healthResponse{Status: statusMessage})
case FormatXML:
return c.XML(healthResponse{Status: statusMessage})
case FormatMsgPack:
return c.MsgPack(healthResponse{Status: statusMessage})
case FormatCBOR:
return c.CBOR(healthResponse{Status: statusMessage})
default: // FormatText
return c.SendString(statusMessage)
}
}
}