Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disable html support to monitor middleware. #1620

Merged
merged 5 commits into from Dec 5, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions 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
DisableHTML bool
efectn marked this conversation as resolved.
Show resolved Hide resolved

// 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{
DisableHTML: 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 {
// To disable serving HTML, you can make true this option.
hi019 marked this conversation as resolved.
Show resolved Hide resolved
//
// 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 @@ -61,6 +61,29 @@ func Test_Monitor_JSON(t *testing.T) {
utils.AssertEqual(t, true, bytes.Contains(b, []byte("os")))
}

// 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")))
}

// go test -v -run=^$ -bench=Benchmark_Monitor -benchmem -count=4
func Benchmark_Monitor(b *testing.B) {
app := fiber.New()
Expand Down