Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ feature: add initial support for hooks #1777

Merged
merged 30 commits into from Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b0933d1
Add initial support for hooks.
efectn Feb 14, 2022
b43788d
release ctx, mutex.
efectn Feb 17, 2022
76a2e45
Add unit tests.
efectn Feb 18, 2022
c61eac9
add comment lines.
efectn Feb 18, 2022
124671b
update
efectn Feb 18, 2022
7292511
update
efectn Feb 18, 2022
649bb6c
Merge branch 'gofiber:master' into add-hooks-support
efectn Feb 19, 2022
2faecaa
remove unnecessary code.
efectn Feb 19, 2022
2e18ea0
Merge branch 'add-hooks-support' of https://github.com/efectn/fiber i…
efectn Feb 19, 2022
429ee79
fix race condition.
efectn Feb 19, 2022
8a04452
fix gosec.
efectn Feb 20, 2022
936a260
skip error handling for onshutdown and onresponse.
efectn Feb 20, 2022
495e722
update
efectn Feb 20, 2022
2d8dce2
separate hooks from app.go
efectn Feb 21, 2022
ca72c2c
make hooks field private, hook struct public and Hooks() func.
efectn Feb 23, 2022
7dd783b
remove onreq and onres because of they can be done by middlewares.
efectn Feb 25, 2022
5b6073a
OnGroupName method.
efectn Feb 25, 2022
a9605c3
Update hooks.go
efectn Feb 26, 2022
57de317
handle errors for name and groupname
efectn Feb 28, 2022
401e4c6
Merge branch 'add-hooks-support' of https://github.com/efectn/fiber i…
efectn Feb 28, 2022
bf99b34
fix tests.
efectn Feb 28, 2022
02fa494
Update app.go
hi019 Mar 2, 2022
1e4138e
use struct fields instead of map
efectn Mar 2, 2022
8efce6f
Merge branch 'add-hooks-support' of https://github.com/efectn/fiber i…
efectn Mar 2, 2022
4bb277e
add multi-handler.
efectn Mar 2, 2022
fecaa73
add onGroup, make prefix field public on Group struct.
efectn Mar 2, 2022
3dbf49b
Update hooks.go
efectn Mar 5, 2022
0223ed1
add newhooks method.
efectn Mar 7, 2022
8d9f0dc
✨ feature: add initial support for hooks
ReneWerner87 Mar 8, 2022
34592bc
remove ctx from hooks.
efectn Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 3 additions & 11 deletions app.go
Expand Up @@ -114,7 +114,7 @@ type App struct {
// Mounted and main apps
appList map[string]*App
// Hooks
hooks Hooks
hooks hooks
// Latest route & group
latestRoute *Route
latestGroup *Group
Expand Down Expand Up @@ -466,15 +466,7 @@ func New(config ...Config) *App {
}

// Define hooks
app.hooks = Hooks{
app: app,
onRoute: make([]OnRouteHandler, 0),
onGroup: make([]OnGroupHandler, 0),
onGroupName: make([]OnGroupNameHandler, 0),
onName: make([]OnNameHandler, 0),
onListen: make([]OnListenHandler, 0),
onShutdown: make([]OnShutdownHandler, 0),
}
app.hooks = newHooks(app)

// Override config if provided
if len(config) > 0 {
Expand Down Expand Up @@ -895,7 +887,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
}

Expand Down
38 changes: 25 additions & 13 deletions hooks.go
Expand Up @@ -12,7 +12,7 @@ type OnGroupNameHandler = OnGroupHandler
type OnListenHandler = Handler
type OnShutdownHandler = Handler

type Hooks struct {
type hooks struct {
// Embed app
app *App

Expand All @@ -25,9 +25,21 @@ type Hooks struct {
onShutdown []OnShutdownHandler
}

func newHooks(app *App) hooks {
efectn marked this conversation as resolved.
Show resolved Hide resolved
return hooks{
app: app,
onRoute: make([]OnRouteHandler, 0),
onGroup: make([]OnGroupHandler, 0),
onGroupName: make([]OnGroupNameHandler, 0),
onName: make([]OnNameHandler, 0),
onListen: make([]OnListenHandler, 0),
onShutdown: make([]OnShutdownHandler, 0),
}
}

// 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()
Expand All @@ -37,15 +49,15 @@ 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()
}

// 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()
Expand All @@ -55,27 +67,27 @@ 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()
}

func (h *Hooks) executeOnRouteHooks(route Route) error {
func (h *hooks) executeOnRouteHooks(route Route) error {
for _, v := range h.onRoute {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand All @@ -88,7 +100,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 {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand All @@ -101,7 +113,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 {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand All @@ -114,7 +126,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 {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand All @@ -127,7 +139,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 {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand All @@ -140,7 +152,7 @@ func (h *Hooks) executeOnListenHooks() error {
return nil
}

func (h *Hooks) executeOnShutdownHooks() {
func (h *hooks) executeOnShutdownHooks() {
for _, v := range h.onShutdown {
ctx := h.app.AcquireCtx(&fasthttp.RequestCtx{})
defer h.app.ReleaseCtx(ctx)
Expand Down