Skip to content

Commit

Permalink
Add next support for Monitor middleware. (#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
efectn committed Sep 13, 2021
1 parent 7a53521 commit 76b0d21
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 1 deletion.
20 changes: 20 additions & 0 deletions middleware/monitor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,23 @@ func main() {
log.Fatal(app.Listen(":3000"))
}
```

## Config

```go
// Config defines the config for middleware.
type Config struct {
// Next defines a function to skip this middleware when returned true.
//
// Optional. Default: nil
Next func(c *fiber.Ctx) bool
}
```

## Default Config

```go
var ConfigDefault = Config{
Next: nil,
}
```
32 changes: 32 additions & 0 deletions middleware/monitor/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package monitor

import "github.com/gofiber/fiber/v2"

// Config defines the config for middleware.
type Config struct {
// 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,
}

func configDefault(config ...Config) Config {
// Return default config if nothing provided
if len(config) < 1 {
return ConfigDefault
}

// Override default config
cfg := config[0]

// Set default values
if cfg.Next == nil {
cfg.Next = ConfigDefault.Next
}

return cfg
}
11 changes: 10 additions & 1 deletion middleware/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,10 @@ var (
)

// New creates a new middleware handler
func New() fiber.Handler {
func New(config ...Config) fiber.Handler {
// Set default config
cfg := configDefault(config...)

// Start routine to update statistics
once.Do(func() {
p, _ := process.NewProcess(int32(os.Getpid()))
Expand All @@ -66,6 +69,11 @@ func New() fiber.Handler {

// Return new handler
return func(c *fiber.Ctx) error {
// Don't execute middleware if Next returns true
if cfg.Next != nil && cfg.Next(c) {
return c.Next()
}

if c.Method() != fiber.MethodGet {
return fiber.ErrMethodNotAllowed
}
Expand All @@ -87,6 +95,7 @@ func New() fiber.Handler {
}
}


func updateStatistics(p *process.Process) {
pidCpu, _ := p.CPUPercent()
monitPidCpu.Store(pidCpu / 10)
Expand Down
17 changes: 17 additions & 0 deletions middleware/monitor/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,20 @@ func Benchmark_Monitor(b *testing.B) {
fiber.MIMEApplicationJSON,
string(fctx.Response.Header.Peek(fiber.HeaderContentType)))
}

// go test -run Test_Monitor_Next
func Test_Monitor_Next(t *testing.T) {
t.Parallel()

app := fiber.New()

app.Use("/", New(Config{
Next: func(_ *fiber.Ctx) bool {
return true
},
}))

resp, err := app.Test(httptest.NewRequest(fiber.MethodPost, "/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}

0 comments on commit 76b0d21

Please sign in to comment.