Skip to content

Commit dc04b9d

Browse files
Add docs from gofiber/fiber@0e2821f
1 parent d78169f commit dc04b9d

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

docs/core/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:

0 commit comments

Comments
 (0)