alpha: v0.0.2
This commit is contained in:
parent
d1e1a32eed
commit
f0fc5fa44f
72
ctx.go
72
ctx.go
@ -3,8 +3,11 @@ package nf
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"mime/multipart"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
@ -91,6 +94,11 @@ func (c *Ctx) Form(key string) string {
|
|||||||
return c.Request.FormValue(key)
|
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 {
|
func (c *Ctx) Query(key string) string {
|
||||||
return c.Request.URL.Query().Get(key)
|
return c.Request.URL.Query().Get(key)
|
||||||
}
|
}
|
||||||
@ -104,6 +112,14 @@ func (c *Ctx) Get(key string, defaultValue ...string) string {
|
|||||||
return value
|
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 {
|
func (c *Ctx) BodyParser(out interface{}) error {
|
||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
@ -152,5 +168,61 @@ func (c *Ctx) BodyParser(out interface{}) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Ctx) QueryParser(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())
|
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
|
||||||
|
}
|
||||||
|
48
resp.go
48
resp.go
@ -1,49 +1 @@
|
|||||||
package nf
|
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
|
|
||||||
}
|
|
||||||
|
@ -18,13 +18,18 @@ func main() {
|
|||||||
var (
|
var (
|
||||||
err error
|
err error
|
||||||
req = new(Req)
|
req = new(Req)
|
||||||
|
rm = make(map[string]any)
|
||||||
)
|
)
|
||||||
|
|
||||||
if err = c.QueryParser(req); err != nil {
|
//if err = c.QueryParser(req); err != nil {
|
||||||
return nf.NewNFError(400, err.Error())
|
// 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"))
|
log.Fatal(app.Run("0.0.0.0:80"))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user