ginprc gin自动参数绑定中间件

xie1xiao1jun · · 2821 次点击 · 开始浏览    置顶
这是一个创建于 的主题,其中的信息可能已经有所发展或是发生改变。

## [ginprc](https://github.com/xxjwxc/ginrpc) 注解路由,自动参数绑定工具 ![ginrpc.gif](https://static.studygolang.com/200601/333606359c0a909403632754e923fee8.gif) ## golang gin 参数自动绑定工具 - 支持对象自动注册及注解路由 - 支持参数自动绑定 - 自带请求参数过滤及绑定实现 binding:"required" [validator](go-playground/validator.v8) - 支持 [grpc-go](https://github.com/grpc/grpc-go) 绑定模式 - 支持[swagger 文档](http://editor.swagger.io/)导出 [MORE](https://github.com/gmsec/gmsec) - 支持[markdown/mindoc 文档](https://www.iminho.me/)导出 [MORE](https://github.com/gmsec/gmsec) - 支持调用前后中间件(`ginrpc.WithBeforeAfter`) - [更多请看](https://github.com/gmsec/gmsec) ## 安装使用 - go mod: ``` go get -u github.com/xxjwxc/ginrpc@master ``` ### 支持多种接口模式 - func(*gin.Context) //go-gin 原始接口 func(*api.Context) //自定义的context类型 - func(*api.Context,req) //自定义的context类型,带request 请求参数 - func(*gin.Context,*req) //go-gin context类型,带request 请求参数 - func(*gin.Context,*req)(*resp,error) //go-gin context类型,带request 请求参数,带错误返回参数 ==> [grpc-go](https://github.com/grpc/grpc-go) func(*gin.Context,req)(resp,error) ## 一. 参数自动绑定/对象注册(注解路由) ### 初始化项目(本项目以gmsec 为名字) ` go mod init gmsec ` ### 代码 ```go package main import ( "fmt" "net/http" _ "gmsec/routers" // debug模式需要添加[mod]/routers 注册注解路由 "github.com/xxjwxc/public/mydoc/myswagger" // swagger 支持 "github.com/gin-gonic/gin" "github.com/xxjwxc/ginrpc" "github.com/xxjwxc/ginrpc/api" ) type ReqTest struct { AccessToken string `json:"access_token"` UserName string `json:"user_name" binding:"required"` // 带校验方式 Password string `json:"password"` } type Hello struct { } // Hello 带注解路由(参考beego形式) // @Router /block [post,get] func (s *Hello) Hello(c *api.Context, req *ReqTest) { fmt.Println(req) c.WriteJSON(req) // 返回结果 } // Hello2 不带注解路由(参数为2默认post) func (s *Hello) Hello2(c *gin.Context, req ReqTest) { fmt.Println(req) c.JSON(http.StatusOK, "ok") // gin 默认返回结果 } // Hello3 [grpc-go](https://github.com/grpc/grpc-go) 模式 func (s *Hello) Hello3(c *gin.Context, req ReqTest) (*ReqTest, error) { fmt.Println(req) return &req,nil } //TestFun6 带自定义context跟已解析的req参数回调方式,err,resp 返回模式 func TestFun6(c *gin.Context, req ReqTest) (*ReqTest, error) { fmt.Println(req) //c.JSON(http.StatusOK, req) return &req, nil } func main() { // swagger myswagger.SetHost("https://localhost:8080") myswagger.SetBasePath("gmsec") myswagger.SetSchemes(true, false) // -----end -- base := ginrpc.New()) router := gin.Default() group := router.Group("/xxjwxc") base.Register(router, new(Hello)) // 对象注册 like(go-micro) router.POST("/test6", base.HandlerFunc(TestFun6)) // 函数注册 base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun6) // 多种请求方式注册 router.Run(":8080") } ``` [更多>>](https://github.com/gmsec/gmsec) ### 执行curl,可以自动参数绑定。直接看结果 ``` curl 'http://127.0.0.1:8080/xxjwxc/block' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}' ``` ``` curl 'http://127.0.0.1:8080/xxjwxc/hello.hello2' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}' ``` ------------------------------------------------------ ### -注解路由相关说明 ``` // @Router /block [post,get] @Router 标记 /block 路由 [post,get] method 调用方式 ``` #### 说明:如果对象函数中不加注解路由,系统会默认添加注解路由。post方式:带req(2个参数(ctx,req)),get方式为一个参数(ctx) ### 1. 注解路由会自动创建[root]/routers/gen_router.go 文件 需要在调用时加: ``` _ "[mod]/routers" // debug模式需要添加[mod]/routers 注册注解路由 ``` 默认也会在项目根目录生成 `gen_router.data` 文件(保留此文件,可以不用添加上面代码嵌入) ### 2. 注册函数说明 ginrpc.WithCtx : 设置自定义context ginrpc.WithDebug(true) : 设置debug模式 ginrpc.WithBigCamel(true) : 设置大驼峰标准(false 为web模式,_,小写) ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{}) : 设置调用前后执行中间件 [更多>>](https://godoc.org/github.com/xxjwxc/ginrpc) ### 2. 注解路由调用demo:[gmsec](https://github.com/gmsec/gmsec) ### 3. 支持绑定grpc函数: [gmsec](https://github.com/gmsec/gmsec) ## 二. swagger/markdown/mindoc 文档生成说明 ### 1.对于对象注册`ginrpc.Register`模式,支持文档导出 ### 2.导出支持注解路由,支持参数注释,支持默认值(`tag`.`default`) ### 3.默认导出路径:(`/docs/swagger/swagger.json`,`/docs/markdown`) ### 4 struct demo ```go type ReqTest struct { AccessToken string `json:"access_token"` UserName string `json:"user_name" binding:"required"` // 带校验方式 Password string `json:"password"` } ``` - [更多 >>>](https://github.com/xxjwxc/gmsec) ## 三. 支持调用中间件 - (全局模式) 可通过 `ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{})`设置(全局) - (单个对象模式) 也可以在对象上实现函数(单个类型) ```go // GinBeforeAfter 对象调用前后执行中间件(支持总的跟对象单独添加) type GinBeforeAfter interface { GinBefore(req *GinBeforeAfterInfo) bool GinAfter(req *GinBeforeAfterInfo) bool } ``` ### 代码地址: [ginprc](https://github.com/xxjwxc/ginrpc) 如果喜欢请给星支持

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

2821 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传