diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..62c8935 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ \ No newline at end of file diff --git a/README.md b/README.md index 1582e5e..e889315 100644 --- a/README.md +++ b/README.md @@ -15,58 +15,75 @@ * [ ] text/plain ## Usage -### Start using it -Download and install it: +All: -```sh -$ go get github.com/tpkeeper/gin-dump +```go +func main() { + router := gin.Default() + + //use Dump() default will print on stdout + router.Use(gindump.Dump()) + + //or use DumpWithOptions() with more options + router.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) { + fmt.Println(dumpStr) + })) + + router.Post("/",myHandler) + + ... + + router.Run() +} ``` -Import it in your code: +Group: ```go -import "github.com/tpkeeper/gin-dump" +func main() { + router := gin.Default() + + dumpGroup := router.Group("/group") + + //use Dump() default will print on stdout + dumpGroup.Use(gindump.Dump()) + + //or use DumpWithOptions() with more options + dumpGroup.Use(gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) { + fmt.Println(dumpStr) + })) + + dumpGroup.Post("/",myHandler) + + ... + + router.Run() +} + ``` -### Canonical example: +EndPoint: ```go -package main - -import ( - "fmt" - "time" - "github.com/gin-gonic/gin" - "github.com/tpkeeper/gin-dump" -) - func main() { router := gin.Default() - showReq := true - showResp := true - showBody := true - showHeaders := false - showCookies := false + //use Dump() default will print on stdout + router.Post("/",gindump.Dump(),myHandler) - router.Use(gindump.Dump(nil)) // prints on stdout - // or - router.Use(gindump.Dump(func(dumpStr string) { - fmt.Println(dumpStr) - })) - // or - router.Use(gindump.DumpWithOptions(showReq, showResp, showBody, showHeaders, showCookies, nil) // prints on stdout - // or - router.Use(gindump.DumpWithOptions(showReq, showResp, showBody, showHeaders, showCookies, func(dumpStr string) { + //or use DumpWithOptions() with more options + router.Post("/",gindump.DumpWithOptions(true, true, false, true, false, func(dumpStr string) { fmt.Println(dumpStr) - })) - - //... - router.Run() + }),myHandler) + + ... + + router.Run() } ``` + ### Output is as follows ```sh diff --git a/gindump.go b/gindump.go index 9fc793b..961440d 100644 --- a/gindump.go +++ b/gindump.go @@ -12,13 +12,13 @@ import ( "github.com/gin-gonic/gin" ) -func Dump(cb func(dumpStr string)) gin.HandlerFunc { - return DumpWithOptions(true, true, true, true, true, cb) +func Dump() gin.HandlerFunc { + return DumpWithOptions(true, true, true, true, true, nil) } func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders bool, showCookies bool, cb func(dumpStr string)) gin.HandlerFunc { - headerHiddenFields := make([]string,0) - bodyHiddenFields := make([]string,0) + headerHiddenFields := make([]string, 0) + bodyHiddenFields := make([]string, 0) if !showCookies { headerHiddenFields = append(headerHiddenFields, "cookie") @@ -29,7 +29,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo if showReq && showHeaders { //dump req header - s, err := FormatToJson(ctx.Request.Header, headerHiddenFields) + s, err := FormatToBeautifulJson(ctx.Request.Header, headerHiddenFields) if err != nil { strB.WriteString(fmt.Sprintf("\nparse req header err \n" + err.Error())) @@ -80,7 +80,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo } val, err := url.ParseQuery(string(bts)) - s, err := FormatToJson(val, bodyHiddenFields) + s, err := FormatToBeautifulJson(val, bodyHiddenFields) if err != nil { strB.WriteString(fmt.Sprintf("\nparse req body err \n" + err.Error())) goto DumpRes @@ -100,7 +100,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo if showResp && showHeaders { //dump res header - sHeader, err := FormatToJson(ctx.Writer.Header(), headerHiddenFields) + sHeader, err := FormatToBeautifulJson(ctx.Writer.Header(), headerHiddenFields) if err != nil { strB.WriteString(fmt.Sprintf("\nparse res header err \n" + err.Error())) } else { @@ -145,7 +145,7 @@ func DumpWithOptions(showReq bool, showResp bool, showBody bool, showHeaders boo if cb != nil { cb(strB.String()) } else { - fmt.Print(strB.String()) + fmt.Println(strB.String()) } } } diff --git a/gindump_test.go b/gindump_test.go index f98f5a8..eaf229a 100644 --- a/gindump_test.go +++ b/gindump_test.go @@ -7,7 +7,6 @@ import ( "io" "net/http" "net/http/httptest" - "net/http/httputil" "net/url" "strings" "testing" @@ -29,9 +28,7 @@ func performRequest(r http.Handler, method, contentType string, path string, bod func TestMIMEJSON(t *testing.T) { router := gin.New() - router.Use(Dump(func(dumpStr string) { - fmt.Println(dumpStr) - })) + router.Use(Dump()) router.POST("/dump", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ @@ -56,12 +53,13 @@ func TestMIMEJSON(t *testing.T) { } body := bytes.NewBuffer(b) + performRequest(router, "POST", gin.MIMEJSON, "/dump", body) } func TestMIMEJSONWithOption(t *testing.T) { router := gin.New() - router.Use(DumpWithOptions(true,false,true,true,false,func(dumpStr string) { + router.Use(DumpWithOptions(true, false, true, true, false, func(dumpStr string) { fmt.Println(dumpStr) })) @@ -92,18 +90,11 @@ func TestMIMEJSONWithOption(t *testing.T) { } - - func TestMIMEPOSTFORM(t *testing.T) { router := gin.New() - router.Use(Dump(func(dumpStr string) { - fmt.Println(dumpStr) - })) + router.Use(Dump()) router.POST("/dump", func(c *gin.Context) { - bts, err := httputil.DumpRequest(c.Request, true) - fmt.Println(string(bts), err) - c.JSON(http.StatusOK, gin.H{ "ok": true, "data": map[string]interface{}{ diff --git a/go.mod b/go.mod index 94fc707..f2f5a53 100644 --- a/go.mod +++ b/go.mod @@ -15,3 +15,5 @@ require ( gopkg.in/go-playground/validator.v8 v8.18.2 // indirect gopkg.in/yaml.v2 v2.2.2 // indirect ) + +go 1.13 diff --git a/parse.go b/parse.go index ce67bbc..8cdce2c 100644 --- a/parse.go +++ b/parse.go @@ -13,8 +13,6 @@ var StringMaxLength = 0 var Newline = "\n" var Indent = 4 - - func BeautifyJsonBytes(data []byte, hiddenFields []string) ([]byte, error) { var v interface{} if err := json.Unmarshal(data, &v); err != nil { @@ -26,8 +24,9 @@ func BeautifyJsonBytes(data []byte, hiddenFields []string) ([]byte, error) { return []byte(format(v, 1)), nil } -//transfer v to json bytes -func FormatToJson(v interface{}, hiddenFields []string) ([]byte, error) { +//transfer v to beautified json bytes +func FormatToBeautifulJson(v interface{}, hiddenFields []string) ([]byte, error) { + data, err := json.Marshal(v) if err != nil { return nil, err @@ -132,7 +131,7 @@ func removeHiddenFields(v interface{}, hiddenFields []string) interface{} { // case insensitive key deletion for _, hiddenField := range hiddenFields { - for k, _ := range m { + for k := range m { if strings.ToLower(k) == strings.ToLower(hiddenField) { delete(m, k) }