Skip to content

Commit

Permalink
Add disable html support to monitor middleware. (#1620)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
efectn and hi019 committed Dec 5, 2021
1 parent 693f3c5 commit b746767
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
8 changes: 7 additions & 1 deletion middleware/monitor/README.md
Expand Up @@ -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
Expand All @@ -47,6 +52,7 @@ type Config struct {

```go
var ConfigDefault = Config{
APIOnly: false,
Next: nil,
}
```
```
12 changes: 11 additions & 1 deletion middleware/monitor/config.go
Expand Up @@ -4,14 +4,20 @@ 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
Next func(c *fiber.Ctx) bool
}

var ConfigDefault = Config{
Next: nil,
APIOnly: false,
Next: nil,
}

func configDefault(config ...Config) Config {
Expand All @@ -28,5 +34,9 @@ func configDefault(config ...Config) Config {
cfg.Next = ConfigDefault.Next
}

if !cfg.APIOnly {
cfg.APIOnly = ConfigDefault.APIOnly
}

return cfg
}
2 changes: 1 addition & 1 deletion middleware/monitor/monitor.go
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions middleware/monitor/monitor_test.go
Expand Up @@ -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")))
}

0 comments on commit b746767

Please sign in to comment.