From f0fc5fa44f18d98a326773e6658e2fd0773d4b7d Mon Sep 17 00:00:00 2001 From: loveuer Date: Fri, 12 Jan 2024 21:42:29 +0800 Subject: [PATCH] alpha: v0.0.2 --- ctx.go | 72 +++++++++++++++++++++++++++++++++++++++ resp.go | 48 -------------------------- xtest/queryParser/main.go | 11 ++++-- 3 files changed, 80 insertions(+), 51 deletions(-) diff --git a/ctx.go b/ctx.go index d4284ec..9a2dbed 100644 --- a/ctx.go +++ b/ctx.go @@ -3,8 +3,11 @@ package nf import ( "bytes" "encoding/json" + "fmt" "io" "log" + "mime/multipart" + "net" "net/http" "strings" ) @@ -91,6 +94,11 @@ func (c *Ctx) Form(key string) string { return c.Request.FormValue(key) } +func (c *Ctx) FormFile(key string) (*multipart.FileHeader, error) { + _, fh, err := c.Request.FormFile(key) + return fh, err +} + func (c *Ctx) Query(key string) string { return c.Request.URL.Query().Get(key) } @@ -104,6 +112,14 @@ func (c *Ctx) Get(key string, defaultValue ...string) string { return value } +func (c *Ctx) IP() string { + ip, _, err := net.SplitHostPort(strings.TrimSpace(c.Request.RemoteAddr)) + if err != nil { + return "" + } + return ip +} + func (c *Ctx) BodyParser(out interface{}) error { var ( err error @@ -152,5 +168,61 @@ func (c *Ctx) BodyParser(out interface{}) error { } func (c *Ctx) QueryParser(out interface{}) error { + //v := reflect.ValueOf(out) + // + //if v.Kind() == reflect.Ptr && v.Elem().Kind() != reflect.Map { + //} + return parseToStruct("query", out, c.Request.URL.Query()) } + +/* =============================================================== +|| Handle Ctx Response Part +=============================================================== */ + +func (c *Ctx) Status(code int) *Ctx { + c.StatusCode = code + c.Writer.WriteHeader(code) + return c +} + +func (c *Ctx) Set(key string, value string) { + c.Writer.Header().Set(key, value) +} + +func (c *Ctx) SetHeader(key string, value string) { + c.Writer.Header().Set(key, value) +} + +func (c *Ctx) SendString(data string) error { + c.SetHeader("Content-Type", "text/plain") + _, err := c.Write([]byte(data)) + return err +} + +func (c *Ctx) Writef(format string, values ...interface{}) (int, error) { + c.SetHeader("Content-Type", "text/plain") + return c.Writer.Write([]byte(fmt.Sprintf(format, values...))) +} + +func (c *Ctx) JSON(data interface{}) error { + c.SetHeader("Content-Type", "application/json") + + encoder := json.NewEncoder(c.Writer) + + if err := encoder.Encode(data); err != nil { + return err + } + + return nil +} + +func (c *Ctx) Write(data []byte) (int, error) { + return c.Writer.Write(data) +} + +func (c *Ctx) HTML(html string) error { + c.SetHeader("Content-Type", "text/html") + _, err := c.Writer.Write([]byte(html)) + return err +} diff --git a/resp.go b/resp.go index 7c9a03a..bb6d6b6 100644 --- a/resp.go +++ b/resp.go @@ -1,49 +1 @@ package nf - -import ( - "encoding/json" - "fmt" -) - -func (c *Ctx) Status(code int) *Ctx { - c.StatusCode = code - c.Writer.WriteHeader(code) - return c -} - -func (c *Ctx) SetHeader(key string, value string) { - c.Writer.Header().Set(key, value) -} - -func (c *Ctx) SendString(data string) error { - c.SetHeader("Content-Type", "text/plain") - _, err := c.Write([]byte(data)) - return err -} - -func (c *Ctx) Writef(format string, values ...interface{}) (int, error) { - c.SetHeader("Content-Type", "text/plain") - return c.Writer.Write([]byte(fmt.Sprintf(format, values...))) -} - -func (c *Ctx) JSON(data interface{}) error { - c.SetHeader("Content-Type", "application/json") - - encoder := json.NewEncoder(c.Writer) - - if err := encoder.Encode(data); err != nil { - return err - } - - return nil -} - -func (c *Ctx) Write(data []byte) (int, error) { - return c.Writer.Write(data) -} - -func (c *Ctx) HTML(html string) error { - c.SetHeader("Content-Type", "text/html") - _, err := c.Writer.Write([]byte(html)) - return err -} diff --git a/xtest/queryParser/main.go b/xtest/queryParser/main.go index bc9d9b4..20769fa 100644 --- a/xtest/queryParser/main.go +++ b/xtest/queryParser/main.go @@ -18,13 +18,18 @@ func main() { var ( err error req = new(Req) + rm = make(map[string]any) ) - if err = c.QueryParser(req); err != nil { - return nf.NewNFError(400, err.Error()) + //if err = c.QueryParser(req); err != nil { + // return nf.NewNFError(400, "1:"+err.Error()) + //} + + if err = c.QueryParser(&rm); err != nil { + return nf.NewNFError(400, "2:"+err.Error()) } - return c.JSON(nf.Map{"status": 200, "data": req}) + return c.JSON(nf.Map{"status": 200, "data": req, "map": rm}) }) log.Fatal(app.Run("0.0.0.0:80"))