mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-16 02:52:45 +00:00
✨ feature: add Next to Pprof and Expvar middlewares. (#1737)
* Add Next to Pprof and Expvar middlewares. * Update READMEs.
This commit is contained in:
parent
d3c2122086
commit
5766feef29
@ -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
middleware/expvar/config.go
Normal file
32
middleware/expvar/config.go
Normal file
@ -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
|
||||
}
|
@ -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") {
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
middleware/pprof/config.go
Normal file
32
middleware/pprof/config.go
Normal file
@ -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
|
||||
}
|
@ -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") {
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user