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

✨ feature: add Next to Pprof and Expvar middlewares. #1737

Merged
merged 2 commits into from Jan 29, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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)
}