From 9530fa863f5936a6491486d4ccb3382ccfd168b9 Mon Sep 17 00:00:00 2001 From: loveuer Date: Wed, 10 Apr 2024 11:24:17 +0800 Subject: [PATCH] feat: ctx add SendStatus --- ctx.go | 5 +++++ xtest/basic/main.go | 6 +++++- xtest/multipart_form/main.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 xtest/multipart_form/main.go diff --git a/ctx.go b/ctx.go index 3b9e2d6..48b35f3 100644 --- a/ctx.go +++ b/ctx.go @@ -262,6 +262,11 @@ func (c *Ctx) SetHeader(key string, value string) { 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 { c.SetHeader("Content-Type", "text/plain") _, err := c.Write([]byte(data)) diff --git a/xtest/basic/main.go b/xtest/basic/main.go index ad2a066..db513a5 100644 --- a/xtest/basic/main.go +++ b/xtest/basic/main.go @@ -6,7 +6,11 @@ import ( ) 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.Get("/1", func(c *nf.Ctx) error { diff --git a/xtest/multipart_form/main.go b/xtest/multipart_form/main.go new file mode 100644 index 0000000..50f2865 --- /dev/null +++ b/xtest/multipart_form/main.go @@ -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")) +}