Skip to content

Commit

Permalink
feat(ginhelper): 支持Protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
peakxie committed Jan 7, 2021
1 parent ec06d3e commit 1d7e09c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 11 deletions.
19 changes: 16 additions & 3 deletions ginhelper/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strconv"

"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/golang/protobuf/proto"
"github.com/peakxie/utils/loghelper"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -59,17 +61,28 @@ func WrapperJ(fun interface{}, reqfun interface{}, rspfun interface{}) gin.Handl
}

Log(ctx).Infof("[RSP] URI:(%s) BODY:(%s)", ctx.Request.URL.Path, loghelper.ToPrintString(rsp))
ctx.Header("Content-Type", "application/json")
ctx.JSON(http.StatusOK, rsp)

if _, ok := rsp.(proto.Message); ok {
ctx.ProtoBuf(http.StatusOK, rsp)
} else {
//ctx.Header("Content-Type", "application/json")
ctx.JSON(http.StatusOK, rsp)
}
}()
reqV := reflect.ValueOf(reqfun).Call([]reflect.Value{})
if len(reqV) != 1 && reqV[0].IsNil() {
Log(ctx).Errorf("get req error.")
panic(errors.New("req struct err"))
}

if err := ctx.ShouldBindJSON(reqV[0].Interface()); err != nil {
req := reqV[0].Interface()
var err error
if _, ok := req.(proto.Message); ok {
err = ctx.ShouldBindWith(reqV[0].Interface(), binding.ProtoBuf)
} else {
err = ctx.ShouldBindJSON(reqV[0].Interface())
}
if err != nil {
Log(ctx).Errorf("parse param err:%v", err)
panic(err)
}
Expand Down
24 changes: 23 additions & 1 deletion ginhelper/gin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (
"testing"

"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
"github.com/peakxie/utils/ginhelper"
"github.com/peakxie/utils/test_proto"
)

type TestReq struct {
Name string
Picture string
Picture []byte
}

type TestRsp struct {
Expand All @@ -35,10 +37,30 @@ func Control(c *gin.Context, req *TestReq) (*TestRsp, error) {
return &TestRsp{RequestId: req.Name}, nil
}

func GetReqPB() *test_proto.Test {
return &test_proto.Test{}
}

func GetRspPB(err error) *test_proto.Test {
return &test_proto.Test{
BusinessId: proto.Uint64(3),
ModuleId: proto.Uint64(4),
Picture: proto.String("ttttttttttttttttttttttttttttttttttttttttttttttttttt"),
RequestId: proto.String(err.Error()),
}
}
func ControlPB(c *gin.Context, req *test_proto.Test) (*test_proto.Test, error) {
if req.GetRequestId() == "test" {
return nil, errors.New("name test")
}
return &test_proto.Test{BusinessId: req.BusinessId, ModuleId: req.ModuleId, Picture: req.Picture, RequestId: req.RequestId}, nil
}

func TestMain(m *testing.M) {
//logs.Init("./log", 10, 10, 10, 6)
router := gin.Default()
router.POST("/test", ginhelper.WrapperJ(Control, GetReq, GetRsp))
router.POST("/testPB", ginhelper.WrapperJ(ControlPB, GetReqPB, GetRspPB))

router.Run(":8086")
}
64 changes: 60 additions & 4 deletions httpx/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package httpx_test

import (
"fmt"
"net/http"
"testing"
"time"

"github.com/gin-gonic/gin"
"github.com/golang/protobuf/proto"
"github.com/peakxie/utils/httpx"
"github.com/peakxie/utils/test_proto"
)

//test http接口

/*
type JsonTest struct {
Name string
Picture string
Expand Down Expand Up @@ -50,3 +49,60 @@ func TestHttpJ(t *testing.T) {
}
}
}
*/

type TestReq struct {
Name string
Picture []byte
}

type TestRsp struct {
RequestId string
Error string
}

func TestHttp(t *testing.T) {
//发请求,自动打印请求包和回包,不需要自己序列化和反序列化协议
req := &TestReq{
Name: "test",
Picture: []byte("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
}
rsp := &TestRsp{}
_, e := httpx.PostJ(nil, "http://localhost:8086/test", "", req, rsp)
if e != nil {
fmt.Println("err", e)
} else {
fmt.Printf("succ: %+v\n", rsp)
}

req.Name = "test1"
_, e = httpx.PostJ(nil, "http://localhost:8086/test", "", req, rsp)
if e != nil {
fmt.Println("err", e)
} else {
fmt.Printf("succ: %+v\n", rsp)
}

reqPB := &test_proto.Test{
BusinessId: proto.Uint64(1),
ModuleId: proto.Uint64(2),
Picture: proto.String("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"),
RequestId: proto.String("test"),
}
rspPB := &test_proto.Test{}
_, e = httpx.PostP(nil, "http://localhost:8086/testPB", "", reqPB, rspPB)
if e != nil {
fmt.Println("err", e)
} else {
fmt.Println("succ: ", rspPB.String())
}

reqPB.RequestId = proto.String("test1")
_, e = httpx.PostP(nil, "http://localhost:8086/testPB", "", reqPB, rspPB)
if e != nil {
fmt.Println("err", e)
} else {
fmt.Println("succ: ", rspPB.String())
}

}
2 changes: 1 addition & 1 deletion loghelper/print_proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

var (
// PrintProtoLen 设置打印pb报文的长度,超过这个长度将会对报文中的超长字段进行处理(截断)
PrintProtoLen = 1024
PrintProtoLen = 64

// PrintStringLen 设置打印报文中字符串的长度,超过这个长度将会对报文中的超长字段进行处理(截断)
PrintStringLen = 64
Expand Down
2 changes: 1 addition & 1 deletion loghelper/print_proto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"testing"

"github.com/golang/protobuf/proto"
"github.com/peakxie/utils/loghelper/test_proto"
"github.com/peakxie/utils/test_proto"
)

type Inner struct {
Expand Down
3 changes: 2 additions & 1 deletion loghelper/test_proto/test.pb.go → test_proto/test.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.

0 comments on commit 1d7e09c

Please sign in to comment.