mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-21 22:53:09 +00:00
Add disable html support to monitor middleware. (#1620)
* Add disable html support to monitor middleware. * Change field. * Update README.md * Update middleware/monitor/config.go * Fix tests. Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
This commit is contained in:
parent
693f3c5118
commit
b74676704d
@ -36,6 +36,11 @@ func main() {
|
||||
```go
|
||||
// Config defines the config for middleware.
|
||||
type Config struct {
|
||||
// To disable serving HTML, you can make true this option.
|
||||
//
|
||||
// Optional. Default: false
|
||||
APIOnly bool
|
||||
|
||||
// Next defines a function to skip this middleware when returned true.
|
||||
//
|
||||
// Optional. Default: nil
|
||||
@ -47,6 +52,7 @@ type Config struct {
|
||||
|
||||
```go
|
||||
var ConfigDefault = Config{
|
||||
APIOnly: false,
|
||||
Next: nil,
|
||||
}
|
||||
```
|
||||
```
|
||||
|
@ -4,6 +4,11 @@ import "github.com/gofiber/fiber/v2"
|
||||
|
||||
// Config defines the config for middleware.
|
||||
type Config struct {
|
||||
// Whether the service should expose only the monitoring API.
|
||||
//
|
||||
// Optional. Default: false
|
||||
APIOnly bool
|
||||
|
||||
// Next defines a function to skip this middleware when returned true.
|
||||
//
|
||||
// Optional. Default: nil
|
||||
@ -11,7 +16,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
var ConfigDefault = Config{
|
||||
Next: nil,
|
||||
APIOnly: false,
|
||||
Next: nil,
|
||||
}
|
||||
|
||||
func configDefault(config ...Config) Config {
|
||||
@ -28,5 +34,9 @@ func configDefault(config ...Config) Config {
|
||||
cfg.Next = ConfigDefault.Next
|
||||
}
|
||||
|
||||
if !cfg.APIOnly {
|
||||
cfg.APIOnly = ConfigDefault.APIOnly
|
||||
}
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ func New(config ...Config) fiber.Handler {
|
||||
if c.Method() != fiber.MethodGet {
|
||||
return fiber.ErrMethodNotAllowed
|
||||
}
|
||||
if c.Get(fiber.HeaderAccept) == fiber.MIMEApplicationJSON {
|
||||
if c.Get(fiber.HeaderAccept) == fiber.MIMEApplicationJSON || cfg.APIOnly {
|
||||
mutex.Lock()
|
||||
data.PID.CPU = monitPidCpu.Load().(float64)
|
||||
data.PID.RAM = monitPidRam.Load().(uint64)
|
||||
|
@ -105,3 +105,26 @@ func Test_Monitor_Next(t *testing.T) {
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, 404, resp.StatusCode)
|
||||
}
|
||||
|
||||
// go test -run Test_Monitor_APIOnly -race
|
||||
func Test_Monitor_APIOnly(t *testing.T) {
|
||||
//t.Parallel()
|
||||
|
||||
app := fiber.New()
|
||||
|
||||
app.Get("/", New(Config{
|
||||
APIOnly: true,
|
||||
}))
|
||||
|
||||
req := httptest.NewRequest(fiber.MethodGet, "/", nil)
|
||||
req.Header.Set(fiber.HeaderAccept, fiber.MIMEApplicationJSON)
|
||||
resp, err := app.Test(req)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, 200, resp.StatusCode)
|
||||
utils.AssertEqual(t, fiber.MIMEApplicationJSON, resp.Header.Get(fiber.HeaderContentType))
|
||||
|
||||
b, err := ioutil.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, nil, err)
|
||||
utils.AssertEqual(t, true, bytes.Contains(b, []byte("pid")))
|
||||
utils.AssertEqual(t, true, bytes.Contains(b, []byte("os")))
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user