Compare commits

...

4 Commits

Author SHA1 Message Date
56fa3815cb update: hide Param, add SetParam(can't cover value) 2024-04-10 18:03:38 +08:00
9530fa863f feat: ctx add SendStatus 2024-04-10 11:24:17 +08:00
f3fb259eee feat: ctx add MultipartForm func 2024-03-11 16:28:33 +08:00
67c15513a2 fix: ctx handlers out of length 2024-02-27 16:19:19 +08:00
4 changed files with 78 additions and 8 deletions

3
app.go
View File

@ -165,8 +165,9 @@ func (a *App) handleHTTPRequest(c *Ctx) {
// Find route in tree // Find route in tree
value := root.getValue(rPath, c.params, c.skippedNodes, unescape) value := root.getValue(rPath, c.params, c.skippedNodes, unescape)
if value.params != nil { if value.params != nil {
c.Params = *value.params c.params = value.params
} }
if value.handlers != nil { if value.handlers != nil {
c.handlers = value.handlers c.handlers = value.handlers
c.fullPath = value.fullPath c.fullPath = value.fullPath

33
ctx.go
View File

@ -29,7 +29,7 @@ type Ctx struct {
locals map[string]interface{} locals map[string]interface{}
skippedNodes *[]skippedNode skippedNodes *[]skippedNode
fullPath string fullPath string
Params Params //Params Params
} }
func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx { func newContext(app *App, writer http.ResponseWriter, request *http.Request) *Ctx {
@ -110,12 +110,15 @@ func (c *Ctx) Cookies(key string, defaultValue ...string) string {
func (c *Ctx) Next() error { func (c *Ctx) Next() error {
c.index++ c.index++
if c.index >= len(c.handlers) {
return nil
}
var ( var (
err error err error
handler = c.handlers[c.index] handler = c.handlers[c.index]
) )
//for c.index < len(c.handlers) {
if handler != nil { if handler != nil {
if err = handler(c); err != nil { if err = handler(c); err != nil {
return err return err
@ -123,7 +126,6 @@ func (c *Ctx) Next() error {
} }
c.index++ c.index++
//}
return nil return nil
} }
@ -142,18 +144,36 @@ func (c *Ctx) verify() error {
} }
func (c *Ctx) Param(key string) string { func (c *Ctx) Param(key string) string {
return c.Params.ByName(key) return c.params.ByName(key)
}
func (c *Ctx) SetParam(key, value string) {
params := append(*c.params, Param{Key: key, Value: value})
c.params = &params
} }
func (c *Ctx) Form(key string) string { func (c *Ctx) Form(key string) string {
return c.Request.FormValue(key) return c.Request.FormValue(key)
} }
// FormValue fiber ctx function
func (c *Ctx) FormValue(key string) string {
return c.Request.FormValue(key)
}
func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error) { func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error) {
_, fh, err := c.Request.FormFile(key) _, fh, err := c.Request.FormFile(key)
return fh, err return fh, err
} }
func (c *Ctx) MultipartForm() (*multipart.Form, error) {
if err := c.Request.ParseMultipartForm(c.app.config.BodyLimit); err != nil {
return nil, err
}
return c.Request.MultipartForm, nil
}
func (c *Ctx) Query(key string) string { func (c *Ctx) Query(key string) string {
return c.Request.URL.Query().Get(key) return c.Request.URL.Query().Get(key)
} }
@ -247,6 +267,11 @@ func (c *Ctx) SetHeader(key string, value string) {
c.writermem.Header().Set(key, value) c.writermem.Header().Set(key, value)
} }
func (c *Ctx) SendStatus(code int) error {
c.writermem.WriteHeader(code)
return nil
}
func (c *Ctx) SendString(data string) error { func (c *Ctx) SendString(data string) error {
c.SetHeader("Content-Type", "text/plain") c.SetHeader("Content-Type", "text/plain")
_, err := c.Write([]byte(data)) _, err := c.Write([]byte(data))

View File

@ -6,11 +6,22 @@ import (
) )
func main() { func main() {
app := nf.New(nf.Config{EnableNotImplementHandler: true}) app := nf.New(nf.Config{})
app.Get("/ok", func(c *nf.Ctx) error {
return c.SendStatus(200)
})
api := app.Group("/api") api := app.Group("/api")
api.Get("/1", func(c *nf.Ctx) error { api.Use(func(c *nf.Ctx) error {
return c.SendString("nice") c.SetParam("age", "18")
return c.Next()
})
api.Get("/:name", func(c *nf.Ctx) error {
name := c.Param("name")
age := c.Param("age")
return c.SendString(name + "@" + age)
}) })
log.Fatal(app.Run(":80")) log.Fatal(app.Run(":80"))

View File

@ -0,0 +1,33 @@
package main
import (
"github.com/loveuer/nf"
"github.com/loveuer/nf/nft/resp"
"log"
)
func main() {
app := nf.New(nf.Config{BodyLimit: 10 * 1024 * 1024})
app.Post("/upload", func(c *nf.Ctx) error {
fs, err := c.MultipartForm()
if err != nil {
return resp.Resp400(c, err.Error())
}
fm := make(map[string][]string)
for key := range fs.File {
if _, exist := fm[key]; !exist {
fm[key] = make([]string, 0)
}
for f := range fs.File[key] {
fm[key] = append(fm[key], fs.File[key][f].Filename)
}
}
return resp.Resp200(c, nf.Map{"value": fs.Value, "files": fm})
})
log.Fatal(app.Run(":13322"))
}