From 658abf1148d3bda59b5dc3e47dbde3db41e1f782 Mon Sep 17 00:00:00 2001 From: Sergio VS Date: Fri, 3 Mar 2023 23:17:52 +0100 Subject: [PATCH] feat: upgrade router - Add group path validations --- go.mod | 2 +- go.sum | 5 ++--- router.go | 40 ++++++++++++++++++++++++++-------------- types.go | 11 ++++++----- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/go.mod b/go.mod index dba501a..2b758bd 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.16 require ( github.com/atreugo/mock v0.0.0-20200601091009-13c275b330b0 - github.com/fasthttp/router v1.4.16 + github.com/fasthttp/router v1.4.17 github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee github.com/valyala/bytebufferpool v1.0.0 github.com/valyala/fasthttp v1.44.0 diff --git a/go.sum b/go.sum index a84f034..173269a 100644 --- a/go.sum +++ b/go.sum @@ -2,12 +2,11 @@ github.com/andybalholm/brotli v1.0.4 h1:V7DdXeJtZscaqfNuAdSRuRFzuiKlHSC/Zh3zl9qY github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/atreugo/mock v0.0.0-20200601091009-13c275b330b0 h1:IVqe9WnancrkICl5HqEfGjrnkQ4+VsU5fodcuFVoG/A= github.com/atreugo/mock v0.0.0-20200601091009-13c275b330b0/go.mod h1:HTHAc8RoZXMVTr6wZQN7Jjm3mYMnbfkqqKdnQgSoe9o= -github.com/fasthttp/router v1.4.16 h1:faWJ9OtaHvAtodreyQLps58M80YFNzphMJtOJzeESXs= -github.com/fasthttp/router v1.4.16/go.mod h1:NFNlTCilbRVkeLc+E5JDkcxUdkpiJGKDL8Zy7Ey2JTI= +github.com/fasthttp/router v1.4.17 h1:Z8fndZotdwcPoYTt8BWwnRBts2UQPnKmOxbb94n0GUc= +github.com/fasthttp/router v1.4.17/go.mod h1:EOMfK/dT1IMzbyPhzw6E2j90owHvY+/BY60bLxOye/8= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/klauspost/compress v1.15.9 h1:wKRjX6JRtDdrE9qwa4b/Cip7ACOshUI4smpCQanqjSY= github.com/klauspost/compress v1.15.9/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= -github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d/go.mod h1:Gy+0tqhJvgGlqnTF8CVGP0AaGRjwBtXs/a5PA0Y3+A4= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee h1:8Iv5m6xEo1NR1AvpV+7XmhI4r39LGNzwUL4YpMuL5vk= github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee/go.mod h1:qwtSXrKuJh/zsFQ12yEE89xfCrGKK63Rr7ctU/uCo4g= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= diff --git a/router.go b/router.go index 14ac3bc..13de855 100644 --- a/router.go +++ b/router.go @@ -49,13 +49,13 @@ func newRouter(cfg Config) *Router { router.HandleOPTIONS = false return &Router{ - router: router, - handleOPTIONS: true, cfg: &routerConfig{ errorView: cfg.ErrorView, debug: cfg.Debug, logger: cfg.Logger, }, + router: router, + handleOPTIONS: true, } } @@ -152,7 +152,7 @@ func (r *Router) handlePath(p *Path) { case p.registered: r.mutable(true) case isOPTIONS: - mutable := !gstrings.Include(r.customOPTIONS, p.url) + mutable := !gstrings.Include(r.customOPTIONS, p.fullURL) r.mutable(mutable) case r.routerMutable: r.mutable(false) @@ -160,8 +160,8 @@ func (r *Router) handlePath(p *Path) { view := p.view if isOPTIONS { - view = buildOptionsView(p.url, view, r.ListPaths()) - r.customOPTIONS = gstrings.UniqueAppend(r.customOPTIONS, p.url) + view = buildOptionsView(p.fullURL, view, r.ListPaths()) + r.customOPTIONS = gstrings.UniqueAppend(r.customOPTIONS, p.fullURL) } handler := r.handler(view, p.middlewares) @@ -169,26 +169,37 @@ func (r *Router) handlePath(p *Path) { handler = fasthttp.TimeoutWithCodeHandler(handler, p.timeout, p.timeoutMsg, p.timeoutCode) } - r.router.Handle(p.method, p.url, handler) + handleFunc := r.router.Handle + if r.group != nil { + handleFunc = r.group.Handle + } + + handleFunc(p.method, p.url, handler) if r.handleOPTIONS && !p.registered && !isOPTIONS { - view = buildOptionsView(p.url, emptyView, r.ListPaths()) + view = buildOptionsView(p.fullURL, emptyView, r.ListPaths()) handler = r.handler(view, p.middlewares) r.mutable(true) - r.router.Handle(fasthttp.MethodOptions, p.url, handler) + handleFunc(fasthttp.MethodOptions, p.url, handler) } } // NewGroupPath returns a new router to group paths. func (r *Router) NewGroupPath(path string) *Router { + groupFunc := r.router.Group + if r.group != nil { + groupFunc = r.group.Group + } + return &Router{ + cfg: r.cfg, parent: r, - prefix: path, router: r.router, routerMutable: r.routerMutable, + prefix: path, + group: groupFunc(path), handleOPTIONS: r.handleOPTIONS, - cfg: r.cfg, } } @@ -394,10 +405,11 @@ func (r *Router) Path(method, url string, viewFn View) *Path { } p := &Path{ - router: r, - method: method, - url: r.getGroupFullPath(url), - view: viewFn, + router: r, + method: method, + url: url, + fullURL: r.getGroupFullPath(url), + view: viewFn, } r.handlePath(p) diff --git a/types.go b/types.go index 8f33f33..eddb1a3 100644 --- a/types.go +++ b/types.go @@ -505,17 +505,17 @@ type routerConfig struct { type Router struct { noCopy nocopy.NoCopy // nolint:structcheck,unused - parent *Router - prefix string - + cfg *routerConfig + parent *Router router *fastrouter.Router routerMutable bool + + prefix string + group *fastrouter.Group handleOPTIONS bool customOPTIONS []string middlewares Middlewares - - cfg *routerConfig } // Path configuration of the registered view @@ -529,6 +529,7 @@ type Path struct { // nolint:maligned method string url string + fullURL string view View middlewares Middlewares