hooks.go 5.1 KB

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