Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,15 @@
return app
}

// NewWithCustomCtx creates a new Fiber instance and applies the
// provided function to generate a custom context type. It mirrors the behaviour

Check failure on line 627 in app.go

View workflow job for this annotation

GitHub Actions / lint

`behaviour` is a misspelling of `behavior` (misspell)
// of calling `New()` followed by `app.setCtxFunc(fn)`.
func NewWithCustomCtx(newCtxFunc func(app *App) CustomCtx, config ...Config) *App {
app := New(config...)
app.setCtxFunc(newCtxFunc)
return app
}

// Adds an ip address to TrustProxyConfig.ranges or TrustProxyConfig.ips based on whether it is an IP range or not
func (app *App) handleTrustedProxy(ipAddress string) {
if strings.Contains(ipAddress, "/") {
Expand All @@ -642,13 +651,14 @@
}
}

// NewCtxFunc allows to customize ctx methods as we want.
// Note: It doesn't allow adding new methods, only customizing exist methods.
func (app *App) NewCtxFunc(function func(app *App) CustomCtx) {
// setCtxFunc applies the given context factory to the app.
// It is used internally by NewWithCustomCtx. It doesn't allow adding new methods,
// only customizing existing ones.
func (app *App) setCtxFunc(function func(app *App) CustomCtx) {
app.newCtxFunc = function

if app.server != nil {
app.server.Handler = app.customRequestHandler
app.server.Handler = app.requestHandler
}
}

Expand Down Expand Up @@ -902,11 +912,7 @@
func (app *App) Handler() fasthttp.RequestHandler { //revive:disable-line:confusing-naming // Having both a Handler() (uppercase) and a handler() (lowercase) is fine. TODO: Use nolint:revive directive instead. See https://github.com/golangci/golangci-lint/issues/3476
// prepare the server for the start
app.startupProcess()

if app.newCtxFunc != nil {
return app.customRequestHandler
}
return app.defaultRequestHandler
return app.requestHandler
}

// Stack returns the raw router stack.
Expand Down Expand Up @@ -1117,11 +1123,7 @@
}

// fasthttp server settings
if app.newCtxFunc != nil {
app.server.Handler = app.customRequestHandler
} else {
app.server.Handler = app.defaultRequestHandler
}
app.server.Handler = app.requestHandler
app.server.Name = app.config.ServerHeader
app.server.Concurrency = app.config.Concurrency
app.server.NoDefaultDate = app.config.DisableDefaultDate
Expand Down
11 changes: 1 addition & 10 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,11 +1045,6 @@ func (c *DefaultCtx) Next() error {
}

// Continue handler stack
if c.app.newCtxFunc != nil {
_, err := c.app.nextCustom(c)
return err
}

_, err := c.app.next(c)
return err
}
Expand All @@ -1060,11 +1055,7 @@ func (c *DefaultCtx) RestartRouting() error {
var err error

c.indexRoute = -1
if c.app.newCtxFunc != nil {
_, err = c.app.nextCustom(c)
} else {
_, err = c.app.next(c)
}
_, err = c.app.next(c)
return err
}

Expand Down
13 changes: 4 additions & 9 deletions ctx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ func (c *customCtx) Params(key string, defaultValue ...string) string { //revive
func Test_Ctx_CustomCtx(t *testing.T) {
t.Parallel()

app := New()

app.NewCtxFunc(func(app *App) CustomCtx {
app := NewWithCustomCtx(func(app *App) CustomCtx {
return &customCtx{
DefaultCtx: *NewDefaultCtx(app),
}
Expand All @@ -133,15 +131,12 @@ func Test_Ctx_CustomCtx_and_Method(t *testing.T) {

// Create app with custom request methods
methods := append(DefaultMethods, "JOHN") //nolint:gocritic // We want a new slice here
app := New(Config{
RequestMethods: methods,
})

// Create custom context
app.NewCtxFunc(func(app *App) CustomCtx {
app := NewWithCustomCtx(func(app *App) CustomCtx {
return &customCtx{
DefaultCtx: *NewDefaultCtx(app),
}
}, Config{
RequestMethods: methods,
})

// Add route with custom method
Expand Down
14 changes: 6 additions & 8 deletions docs/api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,14 @@
func (app *App) ErrorHandler(ctx Ctx, err error) error
```

## NewCtxFunc

Check failure on line 515 in docs/api/app.md

View workflow job for this annotation

GitHub Actions / markdownlint

Multiple consecutive blank lines

docs/api/app.md:515 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.38.0/doc/md012.md
`NewCtxFunc` allows you to customize the `ctx` struct as needed.
## NewWithCustomCtx

`NewWithCustomCtx` creates a new `*App` and sets the custom context factory
function at construction time.

```go title="Signature"
func (app *App) NewCtxFunc(function func(app *App) CustomCtx)
func NewWithCustomCtx(fn func(app *App) CustomCtx, config ...Config) *App
```

```go title="Example"
Expand All @@ -533,22 +535,18 @@
fiber.DefaultCtx
}

// Custom method
func (c *CustomCtx) Params(key string, defaultValue ...string) string {
return "prefix_" + c.DefaultCtx.Params(key)
}

func main() {
app := fiber.New()

app.NewCtxFunc(func(app *fiber.App) fiber.CustomCtx {
app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.CustomCtx {
return &CustomCtx{
DefaultCtx: *fiber.NewDefaultCtx(app),
}
})
Comment on lines 541 to 546
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Fix incorrect return type in factory signature
The example’s factory function is declared as func(app *fiber.App) fiber.CustomCtx, but fiber.CustomCtx isn’t a defined type. It should match the local CustomCtx struct or the interface, e.g.:

app := fiber.NewWithCustomCtx(func(app *fiber.App) CustomCtx {
    return &CustomCtx{ DefaultCtx: *fiber.NewDefaultCtx(app) }
})

Or explicitly return the interface type:

func(app *fiber.App) fiber.Ctx { … }
🤖 Prompt for AI Agents
In docs/api/app.md around lines 541 to 546, the factory function passed to
fiber.NewWithCustomCtx has an incorrect return type fiber.CustomCtx, which is
not defined. Change the return type to either the local CustomCtx struct or the
fiber.Ctx interface to match the expected signature. For example, update the
function signature to func(app *fiber.App) *CustomCtx or func(app *fiber.App)
fiber.Ctx and ensure the returned value matches this type.


app.Get("/:id", func(c fiber.Ctx) error {
// Use custom method - output: prefix_123
return c.SendString(c.Params("id"))
})

Expand Down
17 changes: 6 additions & 11 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ We have made several changes to the Fiber app, including:

- **RegisterCustomBinder**: Allows for the registration of custom binders.
- **RegisterCustomConstraint**: Allows for the registration of custom constraints.
- **NewCtxFunc**: Introduces a new context function.
- **NewWithCustomCtx**: Initialize an app with a custom context in one step.
- **State**: Provides a global state for the application, which can be used to store and retrieve data across the application. Check out the [State](./api/state) method for further details.

### Removed Methods
Expand All @@ -90,19 +90,17 @@ Fiber v3 introduces a customizable `Ctx` interface, allowing developers to exten

The idea behind custom `Ctx` classes is to give developers the ability to extend the default context with additional methods and properties tailored to the specific requirements of their application. This allows for better request handling and easier implementation of specific logic.

#### NewCtxFunc
#### NewWithCustomCtx

The `NewCtxFunc` method allows you to customize the `Ctx` struct as needed.
`NewWithCustomCtx` creates the application and sets the custom context factory at initialization time.

```go title="Signature"
func (app *App) NewCtxFunc(function func(app *App) CustomCtx)
func NewWithCustomCtx(fn func(app *App) CustomCtx, config ...Config) *App
```

<details>
<summary>Example</summary>

Here’s an example of how to customize the `Ctx` interface:

```go
package main

Expand All @@ -115,15 +113,12 @@ type CustomCtx struct {
fiber.Ctx
}

// Custom method
func (c *CustomCtx) CustomMethod() string {
return "custom value"
}

func main() {
app := fiber.New()

app.NewCtxFunc(func(app *fiber.App) fiber.Ctx {
app := fiber.NewWithCustomCtx(func(app *fiber.App) fiber.Ctx {
return &CustomCtx{
Ctx: *fiber.NewCtx(app),
}
Expand All @@ -138,7 +133,7 @@ func main() {
}
```

In this example, a custom context `CustomCtx` is created with an additional method `CustomMethod`. The `NewCtxFunc` method is used to replace the default context with the custom one.
This example creates a `CustomCtx` with an extra `CustomMethod` and initializes the app with `NewWithCustomCtx`.

</details>

Expand Down
46 changes: 1 addition & 45 deletions helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,52 +102,8 @@ func (app *App) quoteString(raw string) string {
}

// Scan stack if other methods match the request
func (app *App) methodExist(c *DefaultCtx) bool {
var exists bool

methods := app.config.RequestMethods
for i := 0; i < len(methods); i++ {
// Skip original method
if c.getMethodInt() == i {
continue
}
// Reset stack index
c.setIndexRoute(-1)

tree, ok := c.App().treeStack[i][c.treePathHash]
if !ok {
tree = c.App().treeStack[i][0]
}
// Get stack length
lenr := len(tree) - 1
// Loop over the route stack starting from previous index
for c.getIndexRoute() < lenr {
// Increment route index
c.setIndexRoute(c.getIndexRoute() + 1)
// Get *Route
route := tree[c.getIndexRoute()]
// Skip use routes
if route.use {
continue
}
// Check if it matches the request path
match := route.match(c.getDetectionPath(), c.Path(), c.getValues())
// No match, next route
if match {
// We matched
exists = true
// Add method to Allow header
c.Append(HeaderAllow, methods[i])
// Break stack loop
break
}
}
}
return exists
}

// Scan stack if other methods match the request
func (app *App) methodExistCustom(c CustomCtx) bool {
func (app *App) methodExist(c CustomCtx) bool {
var exists bool
methods := app.config.RequestMethods
for i := 0; i < len(methods); i++ {
Expand Down
Loading
Loading