📝 docs: make Hooks public (#2015)

pull/2017/head
M. Efe Çetin 2022-08-16 09:13:38 +03:00 committed by GitHub
parent 2517944c80
commit aa4d2c3f65
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 19 deletions

4
app.go
View File

@ -110,7 +110,7 @@ type App struct {
// Mounted and main apps
appList map[string]*App
// Hooks
hooks *hooks
hooks *Hooks
// Latest route & group
latestRoute *Route
latestGroup *Group
@ -819,7 +819,7 @@ func (app *App) Server() *fasthttp.Server {
}
// Hooks returns the hook struct to register hooks.
func (app *App) Hooks() *hooks {
func (app *App) Hooks() *Hooks {
return app.hooks
}

View File

@ -9,7 +9,8 @@ type OnListenHandler = func() error
type OnShutdownHandler = OnListenHandler
type OnForkHandler = func(int) error
type hooks struct {
// Hooks is a struct to use it with App.
type Hooks struct {
// Embed app
app *App
@ -23,8 +24,8 @@ type hooks struct {
onFork []OnForkHandler
}
func newHooks(app *App) *hooks {
return &hooks{
func newHooks(app *App) *Hooks {
return &Hooks{
app: app,
onRoute: make([]OnRouteHandler, 0),
onGroup: make([]OnGroupHandler, 0),
@ -38,7 +39,7 @@ func newHooks(app *App) *hooks {
// OnRoute is a hook to execute user functions on each route registeration.
// Also you can get route properties by route parameter.
func (h *hooks) OnRoute(handler ...OnRouteHandler) {
func (h *Hooks) OnRoute(handler ...OnRouteHandler) {
h.app.mutex.Lock()
h.onRoute = append(h.onRoute, handler...)
h.app.mutex.Unlock()
@ -48,7 +49,7 @@ func (h *hooks) OnRoute(handler ...OnRouteHandler) {
// Also you can get route properties by route parameter.
//
// WARN: OnName only works with naming routes, not groups.
func (h *hooks) OnName(handler ...OnNameHandler) {
func (h *Hooks) OnName(handler ...OnNameHandler) {
h.app.mutex.Lock()
h.onName = append(h.onName, handler...)
h.app.mutex.Unlock()
@ -56,7 +57,7 @@ func (h *hooks) OnName(handler ...OnNameHandler) {
// OnGroup is a hook to execute user functions on each group registeration.
// Also you can get group properties by group parameter.
func (h *hooks) OnGroup(handler ...OnGroupHandler) {
func (h *Hooks) OnGroup(handler ...OnGroupHandler) {
h.app.mutex.Lock()
h.onGroup = append(h.onGroup, handler...)
h.app.mutex.Unlock()
@ -66,34 +67,34 @@ func (h *hooks) OnGroup(handler ...OnGroupHandler) {
// Also you can get group properties by group parameter.
//
// WARN: OnGroupName only works with naming groups, not routes.
func (h *hooks) OnGroupName(handler ...OnGroupNameHandler) {
func (h *Hooks) OnGroupName(handler ...OnGroupNameHandler) {
h.app.mutex.Lock()
h.onGroupName = append(h.onGroupName, handler...)
h.app.mutex.Unlock()
}
// OnListen is a hook to execute user functions on Listen, ListenTLS, Listener.
func (h *hooks) OnListen(handler ...OnListenHandler) {
func (h *Hooks) OnListen(handler ...OnListenHandler) {
h.app.mutex.Lock()
h.onListen = append(h.onListen, handler...)
h.app.mutex.Unlock()
}
// OnShutdown is a hook to execute user functions after Shutdown.
func (h *hooks) OnShutdown(handler ...OnShutdownHandler) {
func (h *Hooks) OnShutdown(handler ...OnShutdownHandler) {
h.app.mutex.Lock()
h.onShutdown = append(h.onShutdown, handler...)
h.app.mutex.Unlock()
}
// OnFork is a hook to execute user function after fork process.
func (h *hooks) OnFork(handler ...OnForkHandler) {
func (h *Hooks) OnFork(handler ...OnForkHandler) {
h.app.mutex.Lock()
h.onFork = append(h.onFork, handler...)
h.app.mutex.Unlock()
}
func (h *hooks) executeOnRouteHooks(route Route) error {
func (h *Hooks) executeOnRouteHooks(route Route) error {
for _, v := range h.onRoute {
if err := v(route); err != nil {
return err
@ -103,7 +104,7 @@ func (h *hooks) executeOnRouteHooks(route Route) error {
return nil
}
func (h *hooks) executeOnNameHooks(route Route) error {
func (h *Hooks) executeOnNameHooks(route Route) error {
for _, v := range h.onName {
if err := v(route); err != nil {
return err
@ -113,7 +114,7 @@ func (h *hooks) executeOnNameHooks(route Route) error {
return nil
}
func (h *hooks) executeOnGroupHooks(group Group) error {
func (h *Hooks) executeOnGroupHooks(group Group) error {
for _, v := range h.onGroup {
if err := v(group); err != nil {
return err
@ -123,7 +124,7 @@ func (h *hooks) executeOnGroupHooks(group Group) error {
return nil
}
func (h *hooks) executeOnGroupNameHooks(group Group) error {
func (h *Hooks) executeOnGroupNameHooks(group Group) error {
for _, v := range h.onGroupName {
if err := v(group); err != nil {
return err
@ -133,7 +134,7 @@ func (h *hooks) executeOnGroupNameHooks(group Group) error {
return nil
}
func (h *hooks) executeOnListenHooks() error {
func (h *Hooks) executeOnListenHooks() error {
for _, v := range h.onListen {
if err := v(); err != nil {
return err
@ -143,13 +144,13 @@ func (h *hooks) executeOnListenHooks() error {
return nil
}
func (h *hooks) executeOnShutdownHooks() {
func (h *Hooks) executeOnShutdownHooks() {
for _, v := range h.onShutdown {
_ = v()
}
}
func (h *hooks) executeOnForkHooks(pid int) {
func (h *Hooks) executeOnForkHooks(pid int) {
for _, v := range h.onFork {
_ = v(pid)
}