Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
9dcf2f8e28 | |||
083b91bfaa | |||
d2d90e6ffd |
33
ctx.go
33
ctx.go
@ -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
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ===============================================================
|
/* ===============================================================
|
||||||
|
@ -2,7 +2,6 @@ package resp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
|
||||||
"github.com/loveuer/nf"
|
"github.com/loveuer/nf"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -15,28 +14,28 @@ type Error struct {
|
|||||||
|
|
||||||
func (e Error) Error() string {
|
func (e Error) Error() string {
|
||||||
if e.msg != "" {
|
if e.msg != "" {
|
||||||
return fmt.Sprintf("%s: %s", e.msg, e.err.Error())
|
return e.msg
|
||||||
}
|
}
|
||||||
|
|
||||||
switch e.status {
|
switch e.status {
|
||||||
case 200:
|
case 200:
|
||||||
return fmt.Sprintf("%s: %s", MSG200, e.err.Error())
|
return MSG200
|
||||||
case 202:
|
case 202:
|
||||||
return fmt.Sprintf("%s: %s", MSG202, e.err.Error())
|
return MSG202
|
||||||
case 400:
|
case 400:
|
||||||
return fmt.Sprintf("%s: %s", MSG400, e.err.Error())
|
return MSG400
|
||||||
case 401:
|
case 401:
|
||||||
return fmt.Sprintf("%s: %s", MSG401, e.err.Error())
|
return MSG401
|
||||||
case 403:
|
case 403:
|
||||||
return fmt.Sprintf("%s: %s", MSG403, e.err.Error())
|
return MSG403
|
||||||
case 404:
|
case 404:
|
||||||
return fmt.Sprintf("%s: %s", MSG404, e.err.Error())
|
return MSG404
|
||||||
case 429:
|
case 429:
|
||||||
return fmt.Sprintf("%s: %s", MSG429, e.err.Error())
|
return MSG429
|
||||||
case 500:
|
case 500:
|
||||||
return fmt.Sprintf("%s: %s", MSG500, e.err.Error())
|
return MSG500
|
||||||
case 501:
|
case 501:
|
||||||
return fmt.Sprintf("%s: %s", MSG501, e.err.Error())
|
return MSG501
|
||||||
}
|
}
|
||||||
|
|
||||||
return e.err.Error()
|
return e.err.Error()
|
||||||
|
27
xtest/multihandler/main.go
Normal file
27
xtest/multihandler/main.go
Normal 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"})
|
||||||
|
}
|
5
xtest/multihandler/req.http
Normal file
5
xtest/multihandler/req.http
Normal 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
|
Reference in New Issue
Block a user