I have a basic logging middleware that use the net/http interface:
func logMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
println("logging...")
next.ServeHTTP(w, r)
})
}
With gofiber/adaptor, I can wrap any regular fiber route with the logging middleware:
app.Get("/ping", adaptor.HTTPHandler(logMiddleware(adaptor.FiberHandler(func(c *fiber.Ctx) error {
return c.SendString("pong")
}))))
However, I can't globally wrap all my routes with app.Use:
App.Get("/ping", adaptor.HTTPHandler(logMiddleware))
// cannot use logMiddleware (type func(http.Handler) http.Handler) as type http.Handler in argument to adaptor.HTTPHandler:
// func(http.Handler) http.Handler does not implement http.Handler (missing ServeHTTP method)
Is there anyway to globally wrap all my routes with a net/http middleware?
EDIT: I tried this too:
app.Use(adaptor.HTTPHandler(logMiddleware(adaptor.FiberHandler(func(c *fiber.Ctx) error {
return c.Next()
}))))
// Logging middleware called: logging...
// panic: runtime error: invalid memory address or nil pointer dereference
Thanks for opening your first issue here! 馃帀 Be sure to follow the issue template! If you need help or want to chat with us, join us on Discord https://gofiber.io/discord
Hi @ibraheemdev
Now you can use HTTPMiddleware wrapper to convert http middlewares to Fiber middleware
Most helpful comment
Hi @ibraheemdev
Now you can use HTTPMiddleware wrapper to convert http middlewares to Fiber middleware