1
0
mirror of https://github.com/gofiber/fiber.git synced 2025-02-21 05:12:40 +00:00

Improve routing calculation

This commit is contained in:
Fenny 2020-06-30 00:27:28 +02:00
parent bc7696ee0d
commit 909c683e12
2 changed files with 7 additions and 5 deletions

6
ctx.go
View File

@ -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
@ -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
@ -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
}

View File

@ -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