Skip to content

Commit

Permalink
✨ feature: add Next to Pprof and Expvar middlewares. (#1737)
Browse files Browse the repository at this point in the history
* Add Next to Pprof and Expvar middlewares.

* Update READMEs.
  • Loading branch information
efectn committed Jan 29, 2022
1 parent d3c2122 commit 5766fee
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 2 deletions.
20 changes: 20 additions & 0 deletions middleware/expvar/README.md
Expand Up @@ -63,3 +63,23 @@ curl 127.0.0.1:3000/debug/vars?r=c
"count": 1
}
```

## 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/expvar/config.go
@@ -0,0 +1,32 @@
package expvar

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
}
10 changes: 9 additions & 1 deletion middleware/expvar/expvar.go
Expand Up @@ -8,9 +8,17 @@ import (
)

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

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

path := c.Path()
// We are only interested in /debug/vars routes
if len(path) < 11 || !strings.HasPrefix(path, "/debug/vars") {
Expand Down
17 changes: 17 additions & 0 deletions middleware/expvar/expvar_test.go
Expand Up @@ -81,3 +81,20 @@ func Test_Expvar_Other_Path(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 302, resp.StatusCode)
}

// go test -run Test_Expvar_Next
func Test_Expvar_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.MethodGet, "/debug/vars", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}
20 changes: 20 additions & 0 deletions middleware/pprof/README.md
Expand Up @@ -23,3 +23,23 @@ After you initiate your Fiber app, you can use the following possibilities:
// Default middleware
app.Use(pprof.New())
```

## 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/pprof/config.go
@@ -0,0 +1,32 @@
package pprof

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
}
10 changes: 9 additions & 1 deletion middleware/pprof/pprof.go
Expand Up @@ -24,9 +24,17 @@ var (
)

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

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

path := c.Path()
// We are only interested in /debug/pprof routes
if len(path) < 12 || !strings.HasPrefix(path, "/debug/pprof") {
Expand Down
17 changes: 17 additions & 0 deletions middleware/pprof/pprof_test.go
Expand Up @@ -87,3 +87,20 @@ func Test_Pprof_Other(t *testing.T) {
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 302, resp.StatusCode)
}

// go test -run Test_Pprof_Next
func Test_Pprof_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.MethodGet, "/debug/pprof/", nil))
utils.AssertEqual(t, nil, err)
utils.AssertEqual(t, 404, resp.StatusCode)
}

0 comments on commit 5766fee

Please sign in to comment.