Skip to content

Commit

Permalink
Improve routing calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
Fenny committed Jun 29, 2020
1 parent bc7696e commit 909c683
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Ctx struct {
indexRoute int // Index of the current route
indexHandler int // Index of the current handler
method string // HTTP method
methodINT int // HTTP method INT equivalent
path string // Prettified HTTP path
pathOriginal string // Original HTTP path
values []string // Route parameter values
Expand Down Expand Up @@ -90,6 +91,7 @@ func (app *App) AcquireCtx(fctx *fasthttp.RequestCtx) *Ctx {
ctx.pathOriginal = ctx.path
// Set method
ctx.method = getString(fctx.Request.Header.Method())
ctx.methodINT = methodInt(ctx.method)
// Attach *fasthttp.RequestCtx to ctx
ctx.Fasthttp = fctx
return ctx
Expand Down Expand Up @@ -580,10 +582,12 @@ func (ctx *Ctx) Location(path string) {
func (ctx *Ctx) Method(override ...string) string {
if len(override) > 0 {
method := utils.ToUpper(override[0])
if methodInt(method) == 0 && method != MethodGet {
mINT := methodInt(method)
if mINT == 0 && method != MethodGet {
return ctx.method
}
ctx.method = method
ctx.methodINT = mINT
}
return ctx.method
}
Expand Down
6 changes: 2 additions & 4 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@ func (r *Route) match(path, original string) (match bool, values []string) {
}

func (app *App) next(ctx *Ctx) bool {
// TODO set unique INT within handler(), not here over and over again
method := methodInt(ctx.method)
// Get stack length
lenr := len(app.stack[method]) - 1
lenr := len(app.stack[ctx.methodINT]) - 1
// Loop over the route stack starting from previous index
for ctx.indexRoute < lenr {
// Increment route index
ctx.indexRoute++
// Get *Route
route := app.stack[method][ctx.indexRoute]
route := app.stack[ctx.methodINT][ctx.indexRoute]
// Check if it matches the request path
match, values := route.match(ctx.path, ctx.pathOriginal)
// No match, next route
Expand Down

0 comments on commit 909c683

Please sign in to comment.