Compare commits

...

2 Commits

Author SHA1 Message Date
9dcf2f8e28 update: route handlers execute rule(execute all -> manual next) 2024-01-30 11:01:58 +08:00
083b91bfaa feat: add ctx.cookies func 2024-01-29 19:16:36 +08:00
4 changed files with 57 additions and 8 deletions

33
ctx.go
View File

@ -62,16 +62,33 @@ func (c *Ctx) Path(overWrite ...string) string {
return path return path
} }
func (c *Ctx) Next() error { func (c *Ctx) Cookies(key string, defaultValue ...string) string {
c.index++ var (
s := len(c.handlers) dv = ""
for ; c.index < s; c.index++ { )
if err := c.handlers[c.index](c); err != nil {
return err if len(defaultValue) > 0 {
} dv = defaultValue[0]
} }
return nil cookie, err := c.Request.Cookie(key)
if err != nil || cookie.Value == "" {
return dv
}
return cookie.Value
}
func (c *Ctx) Next() error {
c.index++
var err error
if c.index < len(c.handlers) {
err = c.handlers[c.index](c)
}
return err
} }
/* =============================================================== /* ===============================================================

0
go.sum Normal file
View File

View File

@ -0,0 +1,27 @@
package main
import (
"github.com/loveuer/nf"
"log"
)
func main() {
app := nf.New()
app.Get("/nice", h1, h2)
log.Fatal(app.Run(":3333"))
}
func h1(c *nf.Ctx) error {
you := c.Query("to")
if you == "you" {
return c.JSON(nf.Map{"status": 201, "msg": "nice to meet you"})
}
return c.Next()
}
func h2(c *nf.Ctx) error {
return c.JSON(nf.Map{"status": 200, "msg": "hello world"})
}

View File

@ -0,0 +1,5 @@
### test multi handlers no next
GET http://127.0.0.1:3333/nice?to=you
### test multi handlers do next
GET http://127.0.0.1:3333/nice?to=nf