Skip to content

Commit

Permalink
Merge pull request #243 from efectn/docs-for-hooks
Browse files Browse the repository at this point in the history
add docs for hooks.
  • Loading branch information
ReneWerner87 authored Mar 21, 2022
2 parents 3cdbb78 + c54606d commit 0bfa4c4
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
* [📝 Templates](guide/templates.md)
* [🐛 Error Handling](guide/error-handling.md)
* [🔎 Validation](guide/validation.md)
* [🪝 Hooks](guide/hooks.md)


## Extra

Expand Down
9 changes: 9 additions & 0 deletions api/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,12 @@ if resp.StatusCode == fiber.StatusOK {
```
{% endcode %}

## Hooks

Hooks is a method to return [hooks](../guide/hooks.md) property.

{% code title="Signature" %}
```go
func (app *App) Hooks() *Hooks
```
{% endcode %}
136 changes: 136 additions & 0 deletions guide/hooks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
# 🪝 Hooks

With Fiber v2.30.0, you can execute custom user functions when to run some methods. Here is a list of this hooks:
- [OnRoute](#onroute)
- [OnName](#onname)
- [OnGroup](#ongroup)
- [OnGroupName](#ongroupname)
- [OnListen](#onlisten)
- [OnShutdown](#onshutdown)

## Constants
```go
// Handlers define a function to create hooks for Fiber.
type OnRouteHandler = func(Route) error
type OnNameHandler = OnRouteHandler
type OnGroupHandler = func(Group) error
type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = func() error
type OnShutdownHandler = OnListenHandler
```

## OnRoute

OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by **route** parameter.

{% code title="Signature" %}
```go
func (app *App) OnRoute(handler ...OnRouteHandler)
```
{% endcode %}

## OnName

OnName is a hook to execute user functions on each route naming. Also you can get route properties by **route** parameter.

**WARN:** OnName only works with naming routes, not groups.

{% code title="Signature" %}
```go
func (app *App) OnName(handler ...OnNameHandler)
```
{% endcode %}

## OnGroup

OnGroup is a hook to execute user functions on each group registeration. Also you can get group properties by **group** parameter.

{% code title="Signature" %}
```go
func (app *App) OnGroup(handler ...OnGroupHandler)
```
{% endcode %}

## OnGroupName

OnGroupName is a hook to execute user functions on each group naming. Also you can get group properties by **group** parameter.

**WARN:** OnGroupName only works with naming groups, not routes.

{% code title="Signature" %}
```go
func (app *App) OnGroupName(handler ...OnGroupNameHandler)
```
{% endcode %}

## OnListen

OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.

{% code title="Signature" %}
```go
func (app *App) OnListen(handler ...OnListenHandler)
```
{% endcode %}

## OnShutdown

OnShutdown is a hook to execute user functions after Shutdown.

{% code title="Signature" %}
```go
func (app *App) OnShutdown(handler ...OnShutdownHandler)
```
{% endcode %}


{% tabs %}
{% tab title="OnName Example" %}
```go
package main
import (
"fmt"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("index")
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Name: " + r.Name + ", ")
return nil
})
app.Hooks().OnName(func(r fiber.Route) error {
fmt.Print("Method: " + r.Method + "\n")
return nil
})
app.Get("/add/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("addUser")
app.Delete("/destroy/user", func(c *fiber.Ctx) error {
return c.SendString(c.Route().Name)
}).Name("destroyUser")
app.Listen(":5000")
}
// Results:
// Name: addUser, Method: GET
// Name: destroyUser, Method: DELETE
```
{% endtab %}

{% endtabs %}


0 comments on commit 0bfa4c4

Please sign in to comment.