Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
137d4ee5c8 | |||
de3ce47671 | |||
1c9c21e294 | |||
9dcf2f8e28 | |||
083b91bfaa |
38
ctx.go
38
ctx.go
@ -5,7 +5,6 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -62,16 +61,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
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ===============================================================
|
/* ===============================================================
|
||||||
@ -127,8 +143,6 @@ func (c *Ctx) BodyParser(out interface{}) error {
|
|||||||
ctype = strings.ToLower(c.Request.Header.Get("Content-Type"))
|
ctype = strings.ToLower(c.Request.Header.Get("Content-Type"))
|
||||||
)
|
)
|
||||||
|
|
||||||
log.Printf("BodyParser: Content-Type=%s", ctype)
|
|
||||||
|
|
||||||
ctype = parseVendorSpecificContentType(ctype)
|
ctype = parseVendorSpecificContentType(ctype)
|
||||||
|
|
||||||
ctypeEnd := strings.IndexByte(ctype, ';')
|
ctypeEnd := strings.IndexByte(ctype, ';')
|
||||||
@ -139,9 +153,9 @@ func (c *Ctx) BodyParser(out interface{}) error {
|
|||||||
if strings.HasSuffix(ctype, "json") {
|
if strings.HasSuffix(ctype, "json") {
|
||||||
bs, err := io.ReadAll(c.Request.Body)
|
bs, err := io.ReadAll(c.Request.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("BodyParser: read all err=%v", err)
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
_ = c.Request.Body.Close()
|
||||||
|
|
||||||
c.Request.Body = io.NopCloser(bytes.NewReader(bs))
|
c.Request.Body = io.NopCloser(bytes.NewReader(bs))
|
||||||
|
|
||||||
|
2
nf.go
2
nf.go
@ -53,7 +53,7 @@ func New(config ...Config) *App {
|
|||||||
app.config = defaultConfig
|
app.config = defaultConfig
|
||||||
}
|
}
|
||||||
|
|
||||||
app.RouterGroup = &RouterGroup{app: app}
|
app.RouterGroup = &RouterGroup{app: app, prefix: ""}
|
||||||
app.groups = []*RouterGroup{app.RouterGroup}
|
app.groups = []*RouterGroup{app.RouterGroup}
|
||||||
|
|
||||||
if !app.config.DisableLogger {
|
if !app.config.DisableLogger {
|
||||||
|
@ -48,7 +48,7 @@ func (r *router) getRoute(method string, path string) (*_node, map[string]string
|
|||||||
root, ok := r.roots[method]
|
root, ok := r.roots[method]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil
|
return &_node{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
n := root.search(searchParts, 0)
|
n := root.search(searchParts, 0)
|
||||||
@ -64,10 +64,11 @@ func (r *router) getRoute(method string, path string) (*_node, map[string]string
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n, params
|
return n, params
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, nil
|
return root, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *router) getRoutes(method string) []*_node {
|
func (r *router) getRoutes(method string) []*_node {
|
||||||
@ -90,6 +91,7 @@ func (r *router) handle(c *Ctx) error {
|
|||||||
c.params = params
|
c.params = params
|
||||||
key := c.Method + "-" + node.pattern
|
key := c.Method + "-" + node.pattern
|
||||||
c.handlers = append(c.handlers, r.handlers[key]...)
|
c.handlers = append(c.handlers, r.handlers[key]...)
|
||||||
|
//c.handlers = append(r.handlers[key], c.handlers...)
|
||||||
} else {
|
} else {
|
||||||
return c.app.config.NotFoundHandler(c)
|
return c.app.config.NotFoundHandler(c)
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,11 @@ GET http://127.0.0.1/hello/nf
|
|||||||
|
|
||||||
### test resp error
|
### test resp error
|
||||||
GET http://127.0.0.1/error
|
GET http://127.0.0.1/error
|
||||||
|
|
||||||
|
### test basic post
|
||||||
|
POST http://127.0.0.1/data
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{
|
||||||
|
"name": "nice"
|
||||||
|
}
|
@ -5,7 +5,6 @@ import (
|
|||||||
"github.com/loveuer/nf"
|
"github.com/loveuer/nf"
|
||||||
"github.com/loveuer/nf/nft/resp"
|
"github.com/loveuer/nf/nft/resp"
|
||||||
"log"
|
"log"
|
||||||
"net"
|
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -25,7 +24,27 @@ func main() {
|
|||||||
app.Get("/error", func(c *nf.Ctx) error {
|
app.Get("/error", func(c *nf.Ctx) error {
|
||||||
return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil))
|
return resp.RespError(c, resp.NewError(404, "not found", errors.New("NNNot Found"), nil))
|
||||||
})
|
})
|
||||||
|
app.Post("/data", func(c *nf.Ctx) error {
|
||||||
|
type Req struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
ln, _ := net.Listen("tcp", ":80")
|
var (
|
||||||
log.Fatal(app.RunListener(ln))
|
err error
|
||||||
|
req = new(Req)
|
||||||
|
rm = make(map[string]any)
|
||||||
|
)
|
||||||
|
|
||||||
|
if err = c.BodyParser(req); err != nil {
|
||||||
|
return c.JSON(nf.Map{"status": 400, "msg": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = c.BodyParser(&rm); err != nil {
|
||||||
|
return c.JSON(nf.Map{"status": 400, "msg": err.Error()})
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(nf.Map{"status": 200, "data": req, "map": rm})
|
||||||
|
})
|
||||||
|
|
||||||
|
log.Fatal(app.Run(":80"))
|
||||||
}
|
}
|
||||||
|
30
xtest/midd/main.go
Normal file
30
xtest/midd/main.go
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/loveuer/nf"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
app := nf.New(nf.Config{DisableLogger: false})
|
||||||
|
|
||||||
|
app.Get("/hello", func(c *nf.Ctx) error {
|
||||||
|
return c.SendString("world")
|
||||||
|
})
|
||||||
|
app.Get("/panic", func(c *nf.Ctx) error {
|
||||||
|
panic("panic")
|
||||||
|
})
|
||||||
|
app.Use(ml())
|
||||||
|
|
||||||
|
log.Fatal(app.Run(":7777"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ml() nf.HandlerFunc {
|
||||||
|
return func(c *nf.Ctx) error {
|
||||||
|
log.Printf("[ML] [%s] - [%s]", c.Method, c.Path())
|
||||||
|
index := []byte(`<h1>my not found</h1>`)
|
||||||
|
c.Set("Content-Type", "text/html")
|
||||||
|
_, err := c.Write(index)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
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