Skip to content

Commit

Permalink
included before handler to modify request context
Browse files Browse the repository at this point in the history
  • Loading branch information
mayankagwl committed Sep 24, 2024
1 parent b984460 commit 2a916cd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
7 changes: 7 additions & 0 deletions atreugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@ func newFasthttpServer(cfg Config) *fasthttp.Server {
func (s *Atreugo) handler() fasthttp.RequestHandler {
handler := s.router.Handler

if s.cfg.BeforeHandler != nil {
handler = func(ctx *fasthttp.RequestCtx) {
s.cfg.BeforeHandler(ctx)
s.router.Handler(ctx)
}
}

if len(s.virtualHosts) > 0 {
handler = func(ctx *fasthttp.RequestCtx) {
hostname := strconv.B2S(ctx.URI().Host())
Expand Down
32 changes: 32 additions & 0 deletions patch/atreugo_before_handler_apply.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
diff --git a/atreugo.go b/atreugo.go
index c81bce9..87dc2ce 100644
--- a/atreugo.go
+++ b/atreugo.go
@@ -121,6 +121,13 @@ func newFasthttpServer(cfg Config) *fasthttp.Server {
func (s *Atreugo) handler() fasthttp.RequestHandler {
handler := s.router.Handler

+ if s.cfg.BeforeHandler != nil {
+ handler = func(ctx *fasthttp.RequestCtx) {
+ s.cfg.BeforeHandler(ctx)
+ s.router.Handler(ctx)
+ }
+ }
+
if len(s.virtualHosts) > 0 {
handler = func(ctx *fasthttp.RequestCtx) {
hostname := strconv.B2S(ctx.URI().Host())
diff --git a/types.go b/types.go
index 5265ec7..fce8b7d 100644
--- a/types.go
+++ b/types.go
@@ -369,6 +369,9 @@ type Config struct { // nolint:maligned
//
// NetHttpFormValueFunc gives a FormValueFunc func implementation that is consistent with net/http.
FormValueFunc fasthttp.FormValueFunc
+
+ // BeforeHandler Function to modify ctx before starting path handling.
+ BeforeHandler func(ctx *fasthttp.RequestCtx)
}

// StaticFS represents settings for serving static files
3 changes: 3 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,9 @@ type Config struct { // nolint:maligned
//
// NetHttpFormValueFunc gives a FormValueFunc func implementation that is consistent with net/http.
FormValueFunc fasthttp.FormValueFunc

// BeforeHandler Function to modify ctx before starting path handling.
BeforeHandler func(ctx *fasthttp.RequestCtx)
}

// StaticFS represents settings for serving static files
Expand Down

0 comments on commit 2a916cd

Please sign in to comment.