Skip to content

Commit c907412

Browse files
Add docs from gofiber/fiber@2c43a0f
1 parent 17be9e9 commit c907412

File tree

4 files changed

+105
-45
lines changed

4 files changed

+105
-45
lines changed

docs/core/extra/faq.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,19 @@ For more information, see issue [#750](https://github.com/gofiber/fiber/issues/7
177177

178178
## How can I handle conversions between Fiber and net/http?
179179

180-
The `adaptor` middleware provides utilities for converting between Fiber and `net/http`. It allows seamless integration of `net/http` handlers, middleware, and requests into Fiber applications, and vice versa.
180+
Fiber can register common `net/http` handlers directly—just pass an
181+
`http.Handler`, `http.HandlerFunc`, compatible function, or even a native
182+
`fasthttp.RequestHandler` to your routing method. For other interoperability scenarios, the `adaptor` middleware provides
183+
utilities for converting between Fiber and `net/http`. It allows seamless
184+
integration of `net/http` handlers, middleware, and requests into Fiber
185+
applications, and vice versa.
186+
187+
:::caution Performance trade-offs
188+
Converted `net/http` handlers run through a compatibility layer. They won't expose
189+
`fiber.Ctx` or Fiber-specific helpers, and the extra adaptation work makes them slower
190+
than native Fiber handlers. Use them when interoperability matters, but prefer Fiber
191+
handlers for maximum performance.
192+
:::
181193

182194
For details on how to:
183195

docs/core/middleware/adaptor.md

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ id: adaptor
66

77
The `adaptor` package converts between Fiber and `net/http`, letting you reuse handlers, middleware, and requests across both frameworks.
88

9+
:::tip
10+
Fiber can register plain `net/http` handlers directly—just pass an `http.Handler`,
11+
`http.HandlerFunc`, or `func(http.ResponseWriter, *http.Request)` to any router
12+
method and it will be adapted automatically. The adaptor helpers remain valuable
13+
when you need to convert middleware, swap handler directions, or transform
14+
requests explicitly.
15+
:::
16+
17+
:::caution Fiber features are unavailable
18+
Even when you register them directly, adapted `net/http` handlers still run with standard
19+
library semantics. They don't have access to `fiber.Ctx`, and the compatibility layer comes
20+
with additional overhead compared to native Fiber handlers. Use them for interop and legacy
21+
scenarios, but prefer Fiber handlers when performance or Fiber-specific APIs matter.
22+
:::
23+
924
## Features
1025

1126
- Convert `net/http` handlers and middleware to Fiber handlers
@@ -31,7 +46,8 @@ The `adaptor` package converts between Fiber and `net/http`, letting you reuse h
3146

3247
### 1. Using `net/http` handlers in Fiber
3348

34-
This example shows how to run a standard `net/http` handler within a Fiber app:
49+
This example shows how to run a standard `net/http` handler within a Fiber app
50+
without calling the adaptor explicitly:
3551

3652
```go
3753
package main
@@ -40,14 +56,13 @@ import (
4056
"fmt"
4157
"net/http"
4258
"github.com/gofiber/fiber/v3"
43-
"github.com/gofiber/fiber/v3/middleware/adaptor"
4459
)
4560

4661
func main() {
4762
app := fiber.New()
4863

49-
// Convert an http.Handler to a Fiber handler
50-
app.Get("/", adaptor.HTTPHandler(http.HandlerFunc(helloHandler)))
64+
// Fiber adapts net/http handlers for you during registration
65+
app.Get("/", http.HandlerFunc(helloHandler))
5166

5267
app.Listen(":3000")
5368
}
@@ -57,6 +72,14 @@ func helloHandler(w http.ResponseWriter, r *http.Request) {
5772
}
5873
```
5974

75+
If you prefer to reuse the converted handler in multiple places, you can still
76+
obtain it manually via `github.com/gofiber/fiber/v3/middleware/adaptor`:
77+
78+
```go
79+
converted := adaptor.HTTPHandler(http.HandlerFunc(helloHandler))
80+
app.Get("/cached", converted)
81+
```
82+
6083
### 2. Using `net/http` middleware with Fiber
6184

6285
Middleware written for `net/http` can run inside Fiber:

docs/core/partials/routing/handler.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,30 +9,54 @@ Registers a route bound to a specific [HTTP method](https://developer.mozilla.or
99

1010
```go title="Signatures"
1111
// HTTP methods
12-
func (app *App) Get(path string, handler Handler, handlers ...Handler) Router
13-
func (app *App) Head(path string, handler Handler, handlers ...Handler) Router
14-
func (app *App) Post(path string, handler Handler, handlers ...Handler) Router
15-
func (app *App) Put(path string, handler Handler, handlers ...Handler) Router
16-
func (app *App) Delete(path string, handler Handler, handlers ...Handler) Router
17-
func (app *App) Connect(path string, handler Handler, handlers ...Handler) Router
18-
func (app *App) Options(path string, handler Handler, handlers ...Handler) Router
19-
func (app *App) Trace(path string, handler Handler, handlers ...Handler) Router
20-
func (app *App) Patch(path string, handler Handler, handlers ...Handler) Router
21-
22-
// Add allows you to specify a method as value
23-
func (app *App) Add(method, path string, handler Handler, handlers ...Handler) Router
12+
func (app *App) Get(path string, handler any, handlers ...any) Router
13+
func (app *App) Head(path string, handler any, handlers ...any) Router
14+
func (app *App) Post(path string, handler any, handlers ...any) Router
15+
func (app *App) Put(path string, handler any, handlers ...any) Router
16+
func (app *App) Delete(path string, handler any, handlers ...any) Router
17+
func (app *App) Connect(path string, handler any, handlers ...any) Router
18+
func (app *App) Options(path string, handler any, handlers ...any) Router
19+
func (app *App) Trace(path string, handler any, handlers ...any) Router
20+
func (app *App) Patch(path string, handler any, handlers ...any) Router
21+
22+
// Add allows you to specify multiple methods at once
23+
func (app *App) Add(methods []string, path string, handler any, handlers ...any) Router
2424

2525
// All will register the route on all HTTP methods
2626
// Almost the same as app.Use but not bound to prefixes
27-
func (app *App) All(path string, handler Handler, handlers ...Handler) Router
27+
func (app *App) All(path string, handler any, handlers ...any) Router
2828
```
2929

30+
Handlers can be native Fiber handlers (`func(fiber.Ctx) error`), familiar `net/http`
31+
shapes such as `http.Handler`, `http.HandlerFunc`, or
32+
`func(http.ResponseWriter, *http.Request)`, and even plain `fasthttp.RequestHandler`
33+
functions. Fiber automatically adapts supported handlers for you during registration.
34+
35+
:::caution Compatibility overhead
36+
Adapted `net/http` handlers execute through a compatibility layer. They don't receive
37+
`fiber.Ctx` or gain access to Fiber-specific APIs, and the conversion adds more
38+
overhead than running a native `fiber.Handler`. Because they cannot call `c.Next()`, they will also terminate the handler chain. Prefer Fiber handlers when you need the
39+
lowest latency or Fiber features.
40+
:::
41+
3042
```go title="Examples"
3143
// Simple GET handler
3244
app.Get("/api/list", func(c fiber.Ctx) error {
3345
return c.SendString("I'm a GET request!")
3446
})
3547

48+
// Reuse an existing net/http handler without manual adaptation
49+
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
50+
w.WriteHeader(http.StatusNoContent)
51+
})
52+
53+
app.Get("/foo", httpHandler)
54+
55+
// Mount a fasthttp.RequestHandler directly
56+
app.Get("/bar", func(ctx *fasthttp.RequestCtx) {
57+
ctx.SetStatusCode(fiber.StatusAccepted)
58+
})
59+
3660
// Simple POST handler
3761
app.Post("/api/register", func(c fiber.Ctx) error {
3862
return c.SendString("I'm a POST request!")

docs/core/whats_new.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -299,36 +299,13 @@ app.Listen("app.sock", fiber.ListenerConfig{
299299

300300
We have slightly adapted our router interface
301301

302-
### HTTP method registration
302+
### Handler compatibility
303303

304-
In `v2` one handler was already mandatory when the route has been registered, but this was checked at runtime and was not correctly reflected in the signature, this has now been changed in `v3` to make it more explicit.
305-
306-
```diff
307-
- Get(path string, handlers ...Handler) Router
308-
+ Get(path string, handler Handler, middleware ...Handler) Router
309-
- Head(path string, handlers ...Handler) Router
310-
+ Head(path string, handler Handler, middleware ...Handler) Router
311-
- Post(path string, handlers ...Handler) Router
312-
+ Post(path string, handler Handler, middleware ...Handler) Router
313-
- Put(path string, handlers ...Handler) Router
314-
+ Put(path string, handler Handler, middleware ...Handler) Router
315-
- Delete(path string, handlers ...Handler) Router
316-
+ Delete(path string, handler Handler, middleware ...Handler) Router
317-
- Connect(path string, handlers ...Handler) Router
318-
+ Connect(path string, handler Handler, middleware ...Handler) Router
319-
- Options(path string, handlers ...Handler) Router
320-
+ Options(path string, handler Handler, middleware ...Handler) Router
321-
- Trace(path string, handlers ...Handler) Router
322-
+ Trace(path string, handler Handler, middleware ...Handler) Router
323-
- Patch(path string, handlers ...Handler) Router
324-
+ Patch(path string, handler Handler, middleware ...Handler) Router
325-
- All(path string, handlers ...Handler) Router
326-
+ All(path string, handler Handler, middleware ...Handler) Router
327-
```
304+
Fiber now ships with a routing adapter (see `adapter.go`) that understands native Fiber handlers alongside `net/http` and `fasthttp` handlers. Route registration helpers accept a required `handler` argument plus optional additional `handlers`, all typed as `any`, and the adapter transparently converts supported handler styles so you can keep using the ecosystem functions you're familiar with.
328305

329306
### Route chaining
330307

331-
This release introduces a dedicated `RouteChain` helper, inspired by [`Express`](https://expressjs.com/en/api.html#app.route), for declaring a stack of handlers on the same path. The original `Route` helper for prefix encapsulation also remains available.
308+
`RouteChain` is a new helper inspired by [`Express`](https://expressjs.com/en/api.html#app.route) that makes it easy to declare a stack of handlers on the same path, while the existing `Route` helper stays available for prefix encapsulation.
332309

333310
```go
334311
RouteChain(path string) Register
@@ -394,7 +371,7 @@ To enable the routing changes above we had to slightly adjust the signature of t
394371

395372
```diff
396373
- Add(method, path string, handlers ...Handler) Router
397-
+ Add(methods []string, path string, handler Handler, middleware ...Handler) Router
374+
+ Add(methods []string, path string, handler any, handlers ...any) Router
398375
```
399376

400377
### Test Config
@@ -1652,6 +1629,30 @@ app.Listen(":3000", fiber.ListenConfig{
16521629

16531630
### 🗺 Router
16541631

1632+
#### Direct `net/http` handlers
1633+
1634+
Route registration helpers now accept native `net/http` handlers. Pass an
1635+
`http.Handler`, `http.HandlerFunc`, or compatible function directly to methods
1636+
such as `app.Get`, `Group`, or `RouteChain` and Fiber will adapt it at
1637+
registration time. Manual wrapping through the adaptor middleware is no longer
1638+
required for these common cases.
1639+
1640+
:::note Compatibility considerations
1641+
Adapted handlers stick to `net/http` semantics. They do not interact with `fiber.Ctx`
1642+
and are slower than native Fiber handlers because of the extra conversion layer. Use
1643+
them to ease migrations, but prefer Fiber handlers in performance-critical paths.
1644+
:::
1645+
1646+
```go
1647+
httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
1648+
if _, err := w.Write([]byte("served by net/http")); err != nil {
1649+
panic(err)
1650+
}
1651+
})
1652+
1653+
app.Get("/", httpHandler)
1654+
```
1655+
16551656
#### Middleware Registration
16561657

16571658
The signatures for [`Add`](#middleware-registration) and [`Route`](#route-chaining) have been changed.

0 commit comments

Comments
 (0)