Skip to content

Commit

Permalink
feat: upgrade router
Browse files Browse the repository at this point in the history
- Add group path validations
  • Loading branch information
savsgio committed Mar 3, 2023
1 parent a379d98 commit 658abf1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 2 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
40 changes: 26 additions & 14 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down Expand Up @@ -152,43 +152,54 @@ 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)
}

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)
if p.withTimeout {
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,
}
}

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -529,6 +529,7 @@ type Path struct { // nolint:maligned

method string
url string
fullURL string
view View
middlewares Middlewares

Expand Down

0 comments on commit 658abf1

Please sign in to comment.