github.com/gofiber/fiber/v2@v2.47.0/docs/guide/hooks.md (about)

     1  ---
     2  id: hooks
     3  title: 🪝 Hooks
     4  sidebar_position: 6
     5  ---
     6  
     7  import Tabs from '@theme/Tabs';
     8  import TabItem from '@theme/TabItem';
     9  
    10  With Fiber v2.30.0, you can execute custom user functions when to run some methods. Here is a list of this hooks:
    11  - [OnRoute](#onroute)
    12  - [OnName](#onname)
    13  - [OnGroup](#ongroup)
    14  - [OnGroupName](#ongroupname)
    15  - [OnListen](#onlisten)
    16  - [OnFork](#onfork)
    17  - [OnShutdown](#onshutdown)
    18  - [OnMount](#onmount)
    19  
    20  ## Constants
    21  ```go
    22  // Handlers define a function to create hooks for Fiber.
    23  type OnRouteHandler = func(Route) error
    24  type OnNameHandler = OnRouteHandler
    25  type OnGroupHandler = func(Group) error
    26  type OnGroupNameHandler = OnGroupHandler
    27  type OnListenHandler = func() error
    28  type OnForkHandler = func(int) error
    29  type OnShutdownHandler = OnListenHandler
    30  type OnMountHandler = func(*App) error
    31  ```
    32  
    33  ## OnRoute
    34  
    35  OnRoute is a hook to execute user functions on each route registeration. Also you can get route properties by **route** parameter.
    36  
    37  ```go title="Signature"
    38  func (app *App) OnRoute(handler ...OnRouteHandler)
    39  ```
    40  
    41  ## OnName
    42  
    43  OnName is a hook to execute user functions on each route naming. Also you can get route properties by **route** parameter.
    44  
    45  :::caution
    46  OnName only works with naming routes, not groups.
    47  :::
    48  
    49  ```go title="Signature"
    50  func (app *App) OnName(handler ...OnNameHandler)
    51  ```
    52  
    53  <Tabs>
    54  <TabItem value="onname-example" label="OnName Example">
    55  
    56  ```go
    57  package main
    58  
    59  import (
    60  	"fmt"
    61  
    62  	"github.com/gofiber/fiber/v2"
    63  )
    64  
    65  func main() {
    66  	app := fiber.New()
    67  
    68  	app.Get("/", func(c *fiber.Ctx) error {
    69  		return c.SendString(c.Route().Name)
    70  	}).Name("index")
    71  
    72  	app.Hooks().OnName(func(r fiber.Route) error {
    73  		fmt.Print("Name: " + r.Name + ", ")
    74  
    75  		return nil
    76  	})
    77  
    78  	app.Hooks().OnName(func(r fiber.Route) error {
    79  		fmt.Print("Method: " + r.Method + "\n")
    80  
    81  		return nil
    82  	})
    83  
    84  	app.Get("/add/user", func(c *fiber.Ctx) error {
    85  		return c.SendString(c.Route().Name)
    86  	}).Name("addUser")
    87  
    88  	app.Delete("/destroy/user", func(c *fiber.Ctx) error {
    89  		return c.SendString(c.Route().Name)
    90  	}).Name("destroyUser")
    91  
    92  	app.Listen(":5000")
    93  }
    94  
    95  // Results:
    96  // Name: addUser, Method: GET
    97  // Name: destroyUser, Method: DELETE
    98  ```
    99  </TabItem>
   100  </Tabs>
   101  
   102  ## OnGroup
   103  
   104  OnGroup is a hook to execute user functions on each group registeration. Also you can get group properties by **group** parameter.
   105  
   106  ```go title="Signature"
   107  func (app *App) OnGroup(handler ...OnGroupHandler)
   108  ```
   109  
   110  ## OnGroupName
   111  
   112  OnGroupName is a hook to execute user functions on each group naming. Also you can get group properties by **group** parameter.
   113  
   114  :::caution
   115  OnGroupName only works with naming groups, not routes.
   116  :::
   117  
   118  ```go title="Signature"
   119  func (app *App) OnGroupName(handler ...OnGroupNameHandler)
   120  ```
   121  
   122  ## OnListen
   123  
   124  OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.
   125  
   126  ```go title="Signature"
   127  func (app *App) OnListen(handler ...OnListenHandler)
   128  ```
   129  
   130  ## OnFork
   131  
   132  OnFork is a hook to execute user functions on Fork.
   133  
   134  ```go title="Signature"
   135  func (app *App) OnFork(handler ...OnForkHandler)
   136  ```
   137  
   138  ## OnShutdown
   139  
   140  OnShutdown is a hook to execute user functions after Shutdown.
   141  
   142  ```go title="Signature"
   143  func (app *App) OnShutdown(handler ...OnShutdownHandler)
   144  ```
   145  
   146  ## OnMount
   147  
   148  OnMount is a hook to execute user function after mounting process. The mount event is fired when sub-app is mounted on a parent app. The parent app is passed as a parameter. It works for app and group mounting.
   149  
   150  ```go title="Signature"
   151  func (h *Hooks) OnMount(handler ...OnMountHandler) 
   152  ```
   153  
   154  <Tabs>
   155  <TabItem value="onmount-example" label="OnMount Example">
   156  
   157  ```go
   158  package main
   159  
   160  import (
   161  	"fmt"
   162  
   163  	"github.com/gofiber/fiber/v2"
   164  )
   165  
   166  func main() {
   167  	app := New()
   168  	app.Get("/", testSimpleHandler).Name("x")
   169  
   170  	subApp := New()
   171  	subApp.Get("/test", testSimpleHandler)
   172  
   173  	subApp.Hooks().OnMount(func(parent *fiber.App) error {
   174  		fmt.Print("Mount path of parent app: "+parent.MountPath())
   175  		// ...
   176  
   177  		return nil
   178  	})
   179  
   180  	app.Mount("/sub", subApp)
   181  }
   182  
   183  // Result:
   184  // Mount path of parent app: 
   185  ```
   186  
   187  </TabItem>
   188  </Tabs>
   189  
   190  
   191  :::caution
   192  OnName/OnRoute/OnGroup/OnGroupName hooks are mount-sensitive. If you use one of these routes on sub app and you mount it; paths of routes and groups will start with mount prefix.