Fiber: global net/http middleware

Created on 20 Oct 2020  路  2Comments  路  Source: gofiber/fiber

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
馃 Question

Most helpful comment

Hi @ibraheemdev
Now you can use HTTPMiddleware wrapper to convert http middlewares to Fiber middleware

All 2 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mhf-ir picture mhf-ir  路  3Comments

petersephrin picture petersephrin  路  4Comments

peterbourgon picture peterbourgon  路  4Comments

mewben picture mewben  路  3Comments

MohamedGouaouri picture MohamedGouaouri  路  3Comments