github.com/gofiber/fiber/v2@v2.47.0/hooks.go (about)

     1  package fiber
     2  
     3  import (
     4  	"log"
     5  )
     6  
     7  // OnRouteHandler Handlers define a function to create hooks for Fiber.
     8  type (
     9  	OnRouteHandler     = func(Route) error
    10  	OnNameHandler      = OnRouteHandler
    11  	OnGroupHandler     = func(Group) error
    12  	OnGroupNameHandler = OnGroupHandler
    13  	OnListenHandler    = func() error
    14  	OnShutdownHandler  = OnListenHandler
    15  	OnForkHandler      = func(int) error
    16  	OnMountHandler     = func(*App) error
    17  )
    18  
    19  // Hooks is a struct to use it with App.
    20  type Hooks struct {
    21  	// Embed app
    22  	app *App
    23  
    24  	// Hooks
    25  	onRoute     []OnRouteHandler
    26  	onName      []OnNameHandler
    27  	onGroup     []OnGroupHandler
    28  	onGroupName []OnGroupNameHandler
    29  	onListen    []OnListenHandler
    30  	onShutdown  []OnShutdownHandler
    31  	onFork      []OnForkHandler
    32  	onMount     []OnMountHandler
    33  }
    34  
    35  func newHooks(app *App) *Hooks {
    36  	return &Hooks{
    37  		app:         app,
    38  		onRoute:     make([]OnRouteHandler, 0),
    39  		onGroup:     make([]OnGroupHandler, 0),
    40  		onGroupName: make([]OnGroupNameHandler, 0),
    41  		onName:      make([]OnNameHandler, 0),
    42  		onListen:    make([]OnListenHandler, 0),
    43  		onShutdown:  make([]OnShutdownHandler, 0),
    44  		onFork:      make([]OnForkHandler, 0),
    45  		onMount:     make([]OnMountHandler, 0),
    46  	}
    47  }
    48  
    49  // OnRoute is a hook to execute user functions on each route registeration.
    50  // Also you can get route properties by route parameter.
    51  func (h *Hooks) OnRoute(handler ...OnRouteHandler) {
    52  	h.app.mutex.Lock()
    53  	h.onRoute = append(h.onRoute, handler...)
    54  	h.app.mutex.Unlock()
    55  }
    56  
    57  // OnName is a hook to execute user functions on each route naming.
    58  // Also you can get route properties by route parameter.
    59  //
    60  // WARN: OnName only works with naming routes, not groups.
    61  func (h *Hooks) OnName(handler ...OnNameHandler) {
    62  	h.app.mutex.Lock()
    63  	h.onName = append(h.onName, handler...)
    64  	h.app.mutex.Unlock()
    65  }
    66  
    67  // OnGroup is a hook to execute user functions on each group registeration.
    68  // Also you can get group properties by group parameter.
    69  func (h *Hooks) OnGroup(handler ...OnGroupHandler) {
    70  	h.app.mutex.Lock()
    71  	h.onGroup = append(h.onGroup, handler...)
    72  	h.app.mutex.Unlock()
    73  }
    74  
    75  // OnGroupName is a hook to execute user functions on each group naming.
    76  // Also you can get group properties by group parameter.
    77  //
    78  // WARN: OnGroupName only works with naming groups, not routes.
    79  func (h *Hooks) OnGroupName(handler ...OnGroupNameHandler) {
    80  	h.app.mutex.Lock()
    81  	h.onGroupName = append(h.onGroupName, handler...)
    82  	h.app.mutex.Unlock()
    83  }
    84  
    85  // OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.
    86  func (h *Hooks) OnListen(handler ...OnListenHandler) {
    87  	h.app.mutex.Lock()
    88  	h.onListen = append(h.onListen, handler...)
    89  	h.app.mutex.Unlock()
    90  }
    91  
    92  // OnShutdown is a hook to execute user functions after Shutdown.
    93  func (h *Hooks) OnShutdown(handler ...OnShutdownHandler) {
    94  	h.app.mutex.Lock()
    95  	h.onShutdown = append(h.onShutdown, handler...)
    96  	h.app.mutex.Unlock()
    97  }
    98  
    99  // OnFork is a hook to execute user function after fork process.
   100  func (h *Hooks) OnFork(handler ...OnForkHandler) {
   101  	h.app.mutex.Lock()
   102  	h.onFork = append(h.onFork, handler...)
   103  	h.app.mutex.Unlock()
   104  }
   105  
   106  // OnMount is a hook to execute user function after mounting process.
   107  // The mount event is fired when sub-app is mounted on a parent app. The parent app is passed as a parameter.
   108  // It works for app and group mounting.
   109  func (h *Hooks) OnMount(handler ...OnMountHandler) {
   110  	h.app.mutex.Lock()
   111  	h.onMount = append(h.onMount, handler...)
   112  	h.app.mutex.Unlock()
   113  }
   114  
   115  func (h *Hooks) executeOnRouteHooks(route Route) error {
   116  	// Check mounting
   117  	if h.app.mountFields.mountPath != "" {
   118  		route.path = h.app.mountFields.mountPath + route.path
   119  		route.Path = route.path
   120  	}
   121  
   122  	for _, v := range h.onRoute {
   123  		if err := v(route); err != nil {
   124  			return err
   125  		}
   126  	}
   127  
   128  	return nil
   129  }
   130  
   131  func (h *Hooks) executeOnNameHooks(route Route) error {
   132  	// Check mounting
   133  	if h.app.mountFields.mountPath != "" {
   134  		route.path = h.app.mountFields.mountPath + route.path
   135  		route.Path = route.path
   136  	}
   137  
   138  	for _, v := range h.onName {
   139  		if err := v(route); err != nil {
   140  			return err
   141  		}
   142  	}
   143  
   144  	return nil
   145  }
   146  
   147  func (h *Hooks) executeOnGroupHooks(group Group) error {
   148  	// Check mounting
   149  	if h.app.mountFields.mountPath != "" {
   150  		group.Prefix = h.app.mountFields.mountPath + group.Prefix
   151  	}
   152  
   153  	for _, v := range h.onGroup {
   154  		if err := v(group); err != nil {
   155  			return err
   156  		}
   157  	}
   158  
   159  	return nil
   160  }
   161  
   162  func (h *Hooks) executeOnGroupNameHooks(group Group) error {
   163  	// Check mounting
   164  	if h.app.mountFields.mountPath != "" {
   165  		group.Prefix = h.app.mountFields.mountPath + group.Prefix
   166  	}
   167  
   168  	for _, v := range h.onGroupName {
   169  		if err := v(group); err != nil {
   170  			return err
   171  		}
   172  	}
   173  
   174  	return nil
   175  }
   176  
   177  func (h *Hooks) executeOnListenHooks() error {
   178  	for _, v := range h.onListen {
   179  		if err := v(); err != nil {
   180  			return err
   181  		}
   182  	}
   183  
   184  	return nil
   185  }
   186  
   187  func (h *Hooks) executeOnShutdownHooks() {
   188  	for _, v := range h.onShutdown {
   189  		if err := v(); err != nil {
   190  			log.Printf("failed to call shutdown hook: %v\n", err)
   191  		}
   192  	}
   193  }
   194  
   195  func (h *Hooks) executeOnForkHooks(pid int) {
   196  	for _, v := range h.onFork {
   197  		if err := v(pid); err != nil {
   198  			log.Printf("failed to call fork hook: %v\n", err)
   199  		}
   200  	}
   201  }
   202  
   203  func (h *Hooks) executeOnMountHooks(app *App) error {
   204  	for _, v := range h.onMount {
   205  		if err := v(app); err != nil {
   206  			return err
   207  		}
   208  	}
   209  
   210  	return nil
   211  }