mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 17:04:26 +00:00
Add next support for Monitor middleware. (#1527)
This commit is contained in:
parent
7a53521da8
commit
76b0d216c1
@ -30,3 +30,23 @@ func main() {
|
|||||||
log.Fatal(app.Listen(":3000"))
|
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
middleware/monitor/config.go
Normal file
32
middleware/monitor/config.go
Normal file
@ -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
|
||||||
|
}
|
@ -48,7 +48,10 @@ var (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// New creates a new middleware handler
|
// 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
|
// Start routine to update statistics
|
||||||
once.Do(func() {
|
once.Do(func() {
|
||||||
p, _ := process.NewProcess(int32(os.Getpid()))
|
p, _ := process.NewProcess(int32(os.Getpid()))
|
||||||
@ -66,6 +69,11 @@ func New() fiber.Handler {
|
|||||||
|
|
||||||
// Return new handler
|
// Return new handler
|
||||||
return func(c *fiber.Ctx) error {
|
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 {
|
if c.Method() != fiber.MethodGet {
|
||||||
return fiber.ErrMethodNotAllowed
|
return fiber.ErrMethodNotAllowed
|
||||||
}
|
}
|
||||||
@ -87,6 +95,7 @@ func New() fiber.Handler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func updateStatistics(p *process.Process) {
|
func updateStatistics(p *process.Process) {
|
||||||
pidCpu, _ := p.CPUPercent()
|
pidCpu, _ := p.CPUPercent()
|
||||||
monitPidCpu.Store(pidCpu / 10)
|
monitPidCpu.Store(pidCpu / 10)
|
||||||
|
@ -88,3 +88,20 @@ func Benchmark_Monitor(b *testing.B) {
|
|||||||
fiber.MIMEApplicationJSON,
|
fiber.MIMEApplicationJSON,
|
||||||
string(fctx.Response.Header.Peek(fiber.HeaderContentType)))
|
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)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user