Adaptor
The adaptor
package converts between Fiber and net/http
, letting you reuse handlers, middleware, and requests across both frameworks.
Fiber can register plain net/http
handlers directlyβjust pass an http.Handler
,
http.HandlerFunc
, or func(http.ResponseWriter, *http.Request)
to any router
method and it will be adapted automatically. The adaptor helpers remain valuable
when you need to convert middleware, swap handler directions, or transform
requests explicitly.
Even when you register them directly, adapted net/http
handlers still run with standard
library semantics. They don't have access to fiber.Ctx
, and the compatibility layer comes
with additional overhead compared to native Fiber handlers. Use them for interop and legacy
scenarios, but prefer Fiber handlers when performance or Fiber-specific APIs matter.
Featuresβ
- Convert
net/http
handlers and middleware to Fiber handlers - Convert Fiber handlers to
net/http
handlers - Convert a Fiber context (
fiber.Ctx
) into anhttp.Request
API Referenceβ
Name | Signature | Description |
---|---|---|
HTTPHandler | HTTPHandler(h http.Handler) fiber.Handler | Converts http.Handler to fiber.Handler |
HTTPHandlerFunc | HTTPHandlerFunc(h http.HandlerFunc) fiber.Handler | Converts http.HandlerFunc to fiber.Handler |
HTTPMiddleware | HTTPMiddleware(mw func(http.Handler) http.Handler) fiber.Handler | Converts http.Handler middleware to fiber.Handler middleware |
FiberHandler | FiberHandler(h fiber.Handler) http.Handler | Converts fiber.Handler to http.Handler |
FiberHandlerFunc | FiberHandlerFunc(h fiber.Handler) http.HandlerFunc | Converts fiber.Handler to http.HandlerFunc |
FiberApp | FiberApp(app *fiber.App) http.HandlerFunc | Converts an entire Fiber app to a http.HandlerFunc |
ConvertRequest | ConvertRequest(c fiber.Ctx, forServer bool) (*http.Request, error) | Converts fiber.Ctx into a http.Request |
CopyContextToFiberContext | CopyContextToFiberContext(context any, requestContext *fasthttp.RequestCtx) | Copies context.Context to fasthttp.RequestCtx |
Usage Examplesβ
1. Using net/http
handlers in Fiberβ
This example shows how to run a standard net/http
handler within a Fiber app
without calling the adaptor explicitly:
package main
import (
"fmt"
"net/http"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
// Fiber adapts net/http handlers for you during registration
app.Get("/", http.HandlerFunc(helloHandler))
app.Listen(":3000")
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello from net/http!")
}
If you prefer to reuse the converted handler in multiple places, you can still
obtain it manually via github.com/gofiber/fiber/v3/middleware/adaptor
:
converted := adaptor.HTTPHandler(http.HandlerFunc(helloHandler))
app.Get("/cached", converted)
2. Using net/http
middleware with Fiberβ
Middleware written for net/http
can run inside Fiber:
package main
import (
"log"
"net/http"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
)
func main() {
app := fiber.New()
// Apply an http middleware in Fiber
app.Use(adaptor.HTTPMiddleware(loggingMiddleware))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello Fiber!")
})
app.Listen(":3000")
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println("Request received")
next.ServeHTTP(w, r)
})
}
3. Using Fiber handlers in net/http
β
You can use Fiber handlers from net/http
:
package main
import (
"net/http"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
)
func main() {
// Convert a Fiber handler to an http.Handler
http.Handle("/", adaptor.FiberHandler(helloFiber))
// Convert a Fiber handler to an http.HandlerFunc
http.HandleFunc("/func", adaptor.FiberHandlerFunc(helloFiber))
http.ListenAndServe(":3000", nil)
}
func helloFiber(c fiber.Ctx) error {
return c.SendString("Hello from Fiber!")
}
4. Running a Fiber app in net/http
β
You can wrap a full Fiber app inside net/http
:
package main
import (
"net/http"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
)
func main() {
app := fiber.New()
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello from Fiber!")
})
// Run Fiber inside an http server
http.ListenAndServe(":3000", adaptor.FiberApp(app))
}
5. Converting a Fiber context (fiber.Ctx
) to http.Request
β
To access an http.Request
within a Fiber handler:
package main
import (
"net/http"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
)
func main() {
app := fiber.New()
app.Get("/request", handleRequest)
app.Listen(":3000")
}
func handleRequest(c fiber.Ctx) error {
httpReq, err := adaptor.ConvertRequest(c, false)
if err != nil {
return err
}
return c.SendString("Converted Request URL: " + httpReq.URL.String())
}
Summaryβ
The adaptor
package lets Fiber and net/http
interoperate so you can:
- Convert handlers and middleware in both directions
- Run Fiber apps inside
net/http
- Convert
fiber.Ctx
tohttp.Request
This makes it straightforward to integrate Fiber with existing Go projects or migrate between frameworks.