github.com/gofiber/fiber/v2@v2.47.0/middleware/expvar/expvar.go (about)

     1  package expvar
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/gofiber/fiber/v2"
     7  
     8  	"github.com/valyala/fasthttp/expvarhandler"
     9  )
    10  
    11  // New creates a new middleware handler
    12  func New(config ...Config) fiber.Handler {
    13  	// Set default config
    14  	cfg := configDefault(config...)
    15  
    16  	// Return new handler
    17  	return func(c *fiber.Ctx) error {
    18  		// Don't execute middleware if Next returns true
    19  		if cfg.Next != nil && cfg.Next(c) {
    20  			return c.Next()
    21  		}
    22  
    23  		path := c.Path()
    24  		// We are only interested in /debug/vars routes
    25  		if len(path) < 11 || !strings.HasPrefix(path, "/debug/vars") {
    26  			return c.Next()
    27  		}
    28  		if path == "/debug/vars" {
    29  			expvarhandler.ExpvarHandler(c.Context())
    30  			return nil
    31  		}
    32  
    33  		return c.Redirect("/debug/vars", fiber.StatusFound)
    34  	}
    35  }