diff --git a/context.go b/context.go index 59439a32..402fd06c 100644 --- a/context.go +++ b/context.go @@ -1,3 +1,10 @@ +// πŸš€ Fiber, Express on Steriods +// πŸ“Œ Don't use in production until version 1.0.0 +// πŸ–₯ https://github.com/fenny/fiber + +// 🦸 Not all heroes wear capes, thank you +1000 +// πŸ’– @valyala, @dgrr, @erikdubbelboer, @savsgio, @julienschmidt + package fiber import ( @@ -18,6 +25,7 @@ import ( // Ctx struct type Ctx struct { noCopy noCopy + route *route next bool params *[]string values []string @@ -53,6 +61,7 @@ func acquireCtx(fctx *fasthttp.RequestCtx) *Ctx { // Return Context to pool func releaseCtx(ctx *Ctx) { + ctx.route = nil ctx.next = false ctx.params = nil ctx.values = nil @@ -192,9 +201,9 @@ func (ctx *Ctx) Cookie(key, value string, options ...interface{}) { if opt.Expire > 0 { cook.SetExpire(time.Unix(int64(opt.Expire), 0)) } - // if opt.MaxAge > 0 { - // cook.SetMaxAge(opt.MaxAge) - // } + if opt.MaxAge > 0 { + cook.SetMaxAge(opt.MaxAge) + } if opt.Domain != "" { cook.SetDomain(opt.Domain) } @@ -481,7 +490,7 @@ func (ctx *Ctx) Render() { } // Route : Only use in debugging -func (ctx *Ctx) Route(r *Fiber) (s struct { +func (ctx *Ctx) Route() (s struct { Method string Path string Wildcard bool @@ -490,36 +499,13 @@ func (ctx *Ctx) Route(r *Fiber) (s struct { Values []string Handler func(*Ctx) }) { - path := ctx.Path() - method := ctx.Method() - for _, route := range r.routes { - if route.method != "*" && route.method != method { - continue - } - if route.any || (route.path == path && route.params == nil) { - s.Method = method - s.Path = path - s.Wildcard = route.any - s.Regex = route.regex - s.Params = route.params - s.Values = ctx.values - s.Handler = route.handler - return - } - if route.regex == nil { - continue - } - if !route.regex.MatchString(path) { - continue - } - s.Method = method - s.Path = path - s.Wildcard = route.any - s.Regex = route.regex - s.Params = route.params - s.Handler = route.handler - return - } + s.Method = ctx.route.method + s.Path = ctx.route.path + s.Wildcard = ctx.route.wildcard + s.Regex = ctx.route.regex + s.Params = ctx.route.params + s.Values = ctx.values + s.Handler = ctx.route.handler return } diff --git a/docs/context.md b/docs/context.md index 2adb7b8d..0c3bddd3 100644 --- a/docs/context.md +++ b/docs/context.md @@ -7,6 +7,7 @@ Checks if the specified content types are acceptable, based on the request’s A // Function signature c.Accepts(ext string) bool + // Example app.Get("/", func(c *fiber.Ctx) { // Accept: text/html @@ -20,8 +21,8 @@ app.Get("/", func(c *fiber.Ctx) { c.Accepts("json") // => // => true - c.Accepts("application/json") - // => // => true + c.Accepts("β˜•") + // => // => false }) ``` @@ -725,31 +726,28 @@ app.Get("/", func(c *fiber.Ctx) { #### Route Contains the currently-matched route struct, **only use this for debugging**. -It returns an anonymous struct containing the following values: -```go -// Route struct -struct { - Method string - Path string - Wildcard bool - Regex *regexp.Regexp - Params []string - Values []string - Handler func(*Ctx) -} -``` +It returns an anonymous struct as shown below. ```go // Function signature -c.Route(app *Fiber) struct +c.Route() struct { + Method string + Path string + Wildcard bool + Regex *regexp.Regexp + Params []string + Values []string + Handler func(*Ctx) +} // Example app.Get("/hello", func(c *fiber.Ctx) { - c.Route(app) - // {GET /hello false [] [] 0x7b4ab0} + c.Route() + // => {GET /hello false [] [] 0x7b4ab0} }) +// http://localhost:8080/hello app.Post("/:api?", func(c *fiber.Ctx) { - c.Route(app) - // {POST / false ^(?:/([^/]+?))?/?$ [lol] [] 0x7b49e0} + c.Route() + // => {POST / false ^(?:/([^/]+?))?/?$ [api] [hello] 0x7b49e0} }) ``` diff --git a/docs/index.html b/docs/index.html index 9677679c..a7a0d352 100644 --- a/docs/index.html +++ b/docs/index.html @@ -18,7 +18,7 @@