Skip to content

Commit fc562dc

Browse files
Add docs from gofiber/fiber@40a8c2c
1 parent 6851ac6 commit fc562dc

File tree

3 files changed

+99
-27
lines changed

3 files changed

+99
-27
lines changed

docs/core/api/app.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ Mounting order is important for `MountPath`. To get mount paths properly, you sh
105105
You can group routes by creating a `*Group` struct.
106106

107107
```go title="Signature"
108-
func (app *App) Group(prefix string, handlers ...Handler) Router
108+
func (app *App) Group(prefix string, handlers ...any) Router
109109
```
110110

111111
```go title="Example"
@@ -153,18 +153,18 @@ func (app *App) RouteChain(path string) Register
153153

154154
```go
155155
type Register interface {
156-
All(handler Handler, handlers ...Handler) Register
157-
Get(handler Handler, handlers ...Handler) Register
158-
Head(handler Handler, handlers ...Handler) Register
159-
Post(handler Handler, handlers ...Handler) Register
160-
Put(handler Handler, handlers ...Handler) Register
161-
Delete(handler Handler, handlers ...Handler) Register
162-
Connect(handler Handler, handlers ...Handler) Register
163-
Options(handler Handler, handlers ...Handler) Register
164-
Trace(handler Handler, handlers ...Handler) Register
165-
Patch(handler Handler, handlers ...Handler) Register
166-
167-
Add(methods []string, handler Handler, handlers ...Handler) Register
156+
All(handler any, handlers ...any) Register
157+
Get(handler any, handlers ...any) Register
158+
Head(handler any, handlers ...any) Register
159+
Post(handler any, handlers ...any) Register
160+
Put(handler any, handlers ...any) Register
161+
Delete(handler any, handlers ...any) Register
162+
Connect(handler any, handlers ...any) Register
163+
Options(handler any, handlers ...any) Register
164+
Trace(handler any, handlers ...any) Register
165+
Patch(handler any, handlers ...any) Register
166+
167+
Add(methods []string, handler any, handlers ...any) Register
168168

169169
RouteChain(path string) Register
170170
}

docs/core/partials/routing/handler.md

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,57 @@ func (app *App) Add(methods []string, path string, handler any, handlers ...any)
2727
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.
30+
Fiber's adapter converts a variety of handler shapes to native
31+
`func(fiber.Ctx) error` callbacks. It currently recognizes thirteen cases (the
32+
numbers below match the comments in `toFiberHandler` inside `adapter.go`). This
33+
lets you mix Fiber-style handlers with Express-style callbacks and even reuse
34+
`net/http` or `fasthttp` functions.
35+
36+
### Fiber-native handlers (cases 1–2)
37+
38+
- **Case 1.** `fiber.Handler` — the canonical `func(fiber.Ctx) error` form.
39+
- **Case 2.** `func(fiber.Ctx)` — Fiber runs the function and treats it as if it
40+
returned `nil`.
41+
42+
### Express-style request handlers (cases 3–8)
43+
44+
- **Case 3.** `func(fiber.Req, fiber.Res) error`
45+
- **Case 4.** `func(fiber.Req, fiber.Res)`
46+
- **Case 5.** `func(fiber.Req, fiber.Res, func() error) error`
47+
- **Case 6.** `func(fiber.Req, fiber.Res, func() error)`
48+
- **Case 7.** `func(fiber.Req, fiber.Res, func()) error`
49+
- **Case 8.** `func(fiber.Req, fiber.Res, func())`
50+
51+
The adapter injects a `next` callback when your signature accepts one. Fiber
52+
propagates downstream errors from `c.Next()` back through the wrapper, so
53+
returning those errors remains optional. If you never call the injected `next`
54+
function, the handler chain stops, matching Express semantics.
55+
56+
### net/http handlers (cases 9–11)
57+
58+
- **Case 9.** `http.HandlerFunc`
59+
- **Case 10.** `http.Handler`
60+
- **Case 11.** `func(http.ResponseWriter, *http.Request)`
3461

3562
:::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.
63+
Fiber adapts these handlers through `fasthttpadaptor`. They do not receive
64+
`fiber.Ctx`, cannot call `c.Next()`, and therefore always terminate the handler
65+
chain. The compatibility layer also adds more overhead than running a native
66+
Fiber handler, so prefer the other forms when possible.
4067
:::
4168

69+
### fasthttp handlers (cases 12–13)
70+
71+
- **Case 12.** `fasthttp.RequestHandler`
72+
- **Case 13.** `func(*fasthttp.RequestCtx) error`
73+
74+
fasthttp handlers run with full access to the underlying `fasthttp.RequestCtx`.
75+
They are expected to manage the response directly. Fiber will propagate any
76+
error returned by the `func(*fasthttp.RequestCtx) error` variant but otherwise
77+
does not inspect the context state.
78+
4279
```go title="Examples"
43-
// Simple GET handler
80+
// Simple GET handler (Fiber accepts both func(fiber.Ctx) and func(fiber.Ctx) error)
4481
app.Get("/api/list", func(c fiber.Ctx) error {
4582
return c.SendString("I'm a GET request!")
4683
})
@@ -52,6 +89,19 @@ httpHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
5289

5390
app.Get("/foo", httpHandler)
5491

92+
// Align with Express-style handlers using fiber.Req and fiber.Res helpers (works
93+
// for middleware and routes alike)
94+
app.Use(func(req fiber.Req, res fiber.Res, next func() error) error {
95+
if req.IP() == "192.168.1.254" {
96+
return res.SendStatus(fiber.StatusForbidden)
97+
}
98+
return next()
99+
})
100+
101+
app.Get("/express", func(req fiber.Req, res fiber.Res) error {
102+
return res.SendString("Hello from Express-style handlers!")
103+
})
104+
55105
// Mount a fasthttp.RequestHandler directly
56106
app.Get("/bar", func(ctx *fasthttp.RequestCtx) {
57107
ctx.SetStatusCode(fiber.StatusAccepted)
@@ -70,13 +120,17 @@ Can be used for middleware packages and prefix catchers. Prefixes now require ei
70120
```go title="Signature"
71121
func (app *App) Use(args ...any) Router
72122

73-
// Different usage variations
74-
func (app *App) Use(handler Handler, handlers ...Handler) Router
75-
func (app *App) Use(path string, handler Handler, handlers ...Handler) Router
76-
func (app *App) Use(paths []string, handler Handler, handlers ...Handler) Router
77-
func (app *App) Use(path string, app *App) Router
123+
// Fiber inspects args to support these common usage patterns:
124+
// - app.Use(handler, handlers ...any)
125+
// - app.Use(path string, handler, handlers ...any)
126+
// - app.Use(paths []string, handler, handlers ...any)
127+
// - app.Use(path string, subApp *App)
78128
```
79129

130+
Each handler argument can independently be a Fiber handler (with or without an
131+
`error` return), an Express-style callback, a `net/http` handler, or any other
132+
supported shape including fasthttp callbacks that return errors.
133+
80134
```go title="Examples"
81135
// Match any request
82136
app.Use(func(c fiber.Ctx) error {

docs/core/whats_new.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,24 @@ We have slightly adapted our router interface
303303

304304
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.
305305

306+
To align even closer with Express, you can also register handlers that accept the new `fiber.Req` and `fiber.Res` helper interfaces. The adapter understands both two-argument (`func(fiber.Req, fiber.Res)`) and three-argument (`func(fiber.Req, fiber.Res, func() error)`) callbacks, regardless of whether they return an `error`. When you include the optional `next` callback, Fiber wires it to `c.Next()` for you so middleware continues to behave as expected. If your handler returns an `error`, the value returned from the injected `next()` bubbles straight back to the caller. When your handler omits an `error` return, Fiber records the result of `next()` and returns it after your function exits so downstream failures still propagate.
307+
308+
| Case | Handler signature | Notes |
309+
| ---- | ----------------- | ----- |
310+
| 1 | `fiber.Handler` | Native Fiber handler. |
311+
| 2 | `func(fiber.Ctx)` | Fiber handler without an error return. |
312+
| 3 | `func(fiber.Req, fiber.Res) error` | Express-style request handler with error return. |
313+
| 4 | `func(fiber.Req, fiber.Res)` | Express-style request handler without error return. |
314+
| 5 | `func(fiber.Req, fiber.Res, func() error) error` | Express-style middleware with an error-returning `next` callback and handler error return. |
315+
| 6 | `func(fiber.Req, fiber.Res, func() error)` | Express-style middleware with an error-returning `next` callback. |
316+
| 7 | `func(fiber.Req, fiber.Res, func()) error` | Express-style middleware with a no-argument `next` callback and handler error return. |
317+
| 8 | `func(fiber.Req, fiber.Res, func())` | Express-style middleware with a no-argument `next` callback. |
318+
| 9 | `http.HandlerFunc` | Standard-library handler function adapted through `fasthttpadaptor`. |
319+
| 10 | `http.Handler` | Standard-library handler implementation; pointer receivers must be non-nil. |
320+
| 11 | `func(http.ResponseWriter, *http.Request)` | Standard-library function handlers via `fasthttpadaptor`. |
321+
| 12 | `fasthttp.RequestHandler` | Direct fasthttp handler without error return. |
322+
| 13 | `func(*fasthttp.RequestCtx) error` | fasthttp handler that returns an error to Fiber. |
323+
306324
### Route chaining
307325

308326
`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.

0 commit comments

Comments
 (0)