Skip to content

Commit 0e2821f

Browse files
authored
Merge pull request #4178 from gofiber/claude/update-healthcheck-endpoints
2 parents bf952a1 + 0d4ec53 commit 0e2821f

4 files changed

Lines changed: 329 additions & 3 deletions

File tree

docs/middleware/healthcheck.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ app.Get(healthcheck.StartupEndpoint, healthcheck.New())
1818

1919
By default the probe returns `true`, so each endpoint responds with `200 OK`; returning `false` yields `503 Service Unavailable`.
2020

21+
The default response format is plain text, but you can configure the middleware to return responses in JSON, XML, MessagePack, or CBOR formats.
22+
2123
- **Liveness**: Checks if the server is running.
2224
- **Readiness**: Checks if the application is ready to handle requests.
2325
- **Startup**: Checks if the application has completed its startup sequence.
@@ -61,6 +63,49 @@ The middleware responds only to GET. Use `app.All` to expose a probe on every me
6163
app.All("/healthz", healthcheck.New())
6264
```
6365

66+
### Response Formats
67+
68+
You can configure the response format using the `ResponseFormat` field in the config:
69+
70+
```go
71+
import (
72+
"github.com/gofiber/fiber/v3"
73+
"github.com/gofiber/fiber/v3/middleware/healthcheck"
74+
)
75+
76+
// JSON format
77+
app.Get(healthcheck.LivenessEndpoint, healthcheck.New(healthcheck.Config{
78+
ResponseFormat: healthcheck.FormatJSON,
79+
}))
80+
// Response: {"status":"OK"}
81+
82+
// XML format
83+
app.Get(healthcheck.ReadinessEndpoint, healthcheck.New(healthcheck.Config{
84+
ResponseFormat: healthcheck.FormatXML,
85+
}))
86+
// Response: <healthResponse><status>OK</status></healthResponse>
87+
```
88+
89+
**Note:** MessagePack and CBOR formats require configuring the appropriate encoders in your Fiber app:
90+
91+
```go
92+
import (
93+
"github.com/fxamacker/cbor/v2"
94+
"github.com/gofiber/fiber/v3"
95+
"github.com/gofiber/fiber/v3/middleware/healthcheck"
96+
"github.com/shamaton/msgpack/v3"
97+
)
98+
99+
app := fiber.New(fiber.Config{
100+
MsgPackEncoder: msgpack.Marshal,
101+
CBOREncoder: cbor.Marshal,
102+
})
103+
104+
app.Get(healthcheck.LivenessEndpoint, healthcheck.New(healthcheck.Config{
105+
ResponseFormat: healthcheck.FormatMsgPack,
106+
}))
107+
```
108+
64109
## Config
65110

66111
```go
@@ -78,9 +123,29 @@ type Config struct {
78123
//
79124
// Optional. Default: func(c fiber.Ctx) bool { return true }
80125
Probe func(fiber.Ctx) bool
126+
127+
// ResponseFormat specifies the format of the healthcheck response.
128+
// Supported formats: Text (default), JSON, XML, MsgPack, CBOR.
129+
//
130+
// Optional. Default: FormatText
131+
ResponseFormat ResponseFormat
81132
}
82133
```
83134

135+
### Response Format Constants
136+
137+
```go
138+
type ResponseFormat int
139+
140+
const (
141+
FormatText ResponseFormat = iota // Plain text response (default)
142+
FormatJSON // JSON response
143+
FormatXML // XML response
144+
FormatMsgPack // MessagePack response
145+
FormatCBOR // CBOR response
146+
)
147+
```
148+
84149
## Default Config
85150

86151
The default configuration used by this middleware is defined as follows:

middleware/healthcheck/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ import (
44
"github.com/gofiber/fiber/v3"
55
)
66

7+
// ResponseFormat defines the format of the healthcheck response.
8+
type ResponseFormat int
9+
10+
const (
11+
// FormatText returns a plain text response (default behavior).
12+
FormatText ResponseFormat = iota
13+
// FormatJSON returns a JSON response.
14+
FormatJSON
15+
// FormatXML returns an XML response.
16+
FormatXML
17+
// FormatMsgPack returns a MessagePack response.
18+
FormatMsgPack
19+
// FormatCBOR returns a CBOR response.
20+
FormatCBOR
21+
)
22+
723
// Config defines the configuration options for the healthcheck middleware.
824
type Config struct {
925
// Next defines a function to skip this middleware when returned true. If this function returns true
@@ -18,6 +34,12 @@ type Config struct {
1834
//
1935
// Optional. Default: func(c fiber.Ctx) bool { return true }
2036
Probe func(fiber.Ctx) bool
37+
38+
// ResponseFormat specifies the format of the healthcheck response.
39+
// Supported formats: Text (default), JSON, XML, MsgPack, CBOR.
40+
//
41+
// Optional. Default: FormatText
42+
ResponseFormat ResponseFormat
2143
}
2244

2345
const (

middleware/healthcheck/healthcheck.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import (
44
"github.com/gofiber/fiber/v3"
55
)
66

7+
// healthResponse represents the JSON/XML/MsgPack/CBOR response structure.
8+
type healthResponse struct {
9+
Status string `json:"status" xml:"status" msgpack:"status" cbor:"status"`
10+
}
11+
712
// New returns a health-check handler that responds based on the provided
813
// configuration.
914
func New(config ...Config) fiber.Handler {
@@ -19,10 +24,30 @@ func New(config ...Config) fiber.Handler {
1924
return c.Next()
2025
}
2126

22-
if cfg.Probe(c) {
23-
return c.SendStatus(fiber.StatusOK)
27+
healthy := cfg.Probe(c)
28+
statusCode := fiber.StatusOK
29+
statusMessage := "OK"
30+
31+
if !healthy {
32+
statusCode = fiber.StatusServiceUnavailable
33+
statusMessage = "Service Unavailable"
2434
}
2535

26-
return c.SendStatus(fiber.StatusServiceUnavailable)
36+
// Set the status code
37+
c.Status(statusCode)
38+
39+
// Return response based on configured format
40+
switch cfg.ResponseFormat {
41+
case FormatJSON:
42+
return c.JSON(healthResponse{Status: statusMessage})
43+
case FormatXML:
44+
return c.XML(healthResponse{Status: statusMessage})
45+
case FormatMsgPack:
46+
return c.MsgPack(healthResponse{Status: statusMessage})
47+
case FormatCBOR:
48+
return c.CBOR(healthResponse{Status: statusMessage})
49+
default: // FormatText
50+
return c.SendString(statusMessage)
51+
}
2752
}
2853
}

0 commit comments

Comments
 (0)