golang Gin参数自动绑定与RPC工具插件库Ginrpc的使用
Golang Gin 参数自动绑定与 RPC 工具插件库 Ginrpc 的使用
Ginrpc 是一个基于 Go-Gin 框架的自动参数绑定和 RPC 工具库,支持多种接口模式和注解路由等功能。
主要特性
- 支持 RPC 自动映射
- 支持对象注册
- 支持注解路由
- 基于 Go-Gin 框架的 JSON RESTful 风格
- 实现参数过滤和请求绑定
- 代码注册简单,支持多种注册方式
- 支持 grpc-go 绑定
- 支持 Swagger 文档生成
- 支持 Markdown/Mindoc 文档生成
- 支持前后调用中间件
安装
使用 go mod 安装:
go get -u github.com/xxjwxc/ginrpc@master
API 详情
支持的三种接口模式
-
原始 Gin 接口模式:
func(*gin.Context)
-
自定义上下文类型:
func(*api.Context) func(*api.Context, req) func(*api.Context, *req)
-
Gin 上下文与请求参数:
func(*gin.Context, *req) func(*gin.Context, req) func(*gin.Context, *req)(*resp, error) // grpc-go 风格 func(*gin.Context, req)(resp, error)
示例代码
1. 参数自动绑定与对象注册(注解路由)
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 {
Access_token string `json:"access_token"`
UserName string `json:"user_name" binding:"required"` // 带校验模式
Password string `json:"password"`
}
// Hello ...
type Hello struct {
}
// Hello 注解路由 (基于 beego 方式)
// @Router /block [post,get]
func (s *Hello) Hello(c *api.Context, req *ReqTest) {
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
// Hello2 不带注解的路由 (参数为 2 默认 post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
// [grpc-go](https://github.com/grpc/grpc-go)
// 带请求参数、返回值和错误
// TestFun6 不带注解的路由 (参数为 2 默认 post)
func TestFun6(c *gin.Context, req ReqTest) (*ReqTest, error) {
fmt.Println(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() // 或 router := gin.Default().Group("/xxjwxc")
// 对象注册 (类似 go-micro)
base.Register(router, new(Hello))
// 函数注册
router.POST("/test6", base.HandlerFunc(TestFun6))
base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun6)
router.Run(":8080")
}
注解路由说明
// @Router /block [post,get]
@Router
标签/block
路由路径[post,get]
支持的 HTTP 方法
注意:如果对象函数中没有注解路由,系统会默认添加注解路由。POST 模式需要 2 个参数 (ctx, req),GET 模式只需要 1 个参数 (ctx)。
参数说明
ginrpc.WithCtx // 设置自定义上下文
ginrpc.WithDebug(true) // 设置调试模式
ginrpc.WithOutDoc(true) // 输出 Markdown/Swagger API 文档
ginrpc.WithBigCamel(true) // 设置大驼峰标准 (false 为 web 模式,_,小写)
ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{}) // 前后调用
测试 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"}'
文档生成
1. 支持对象注册 ginrpc.Register
模式的文档导出
2. 导出支持注解路由、参数注解和默认值 (tag 'default'
)
3. 默认导出路径
/docs/swagger/swagger.json
/docs/markdown
4. 结构体示例
type ReqTest struct {
AccessToken string `json:"access_token"`
UserName string `json:"user_name" binding:"required"` // 带校验方式
Password string `json:"password"`
}
中间件支持
使用 ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{})
也可以实现对象上的函数(单一类型):
// GinBeforeAfter 在对象调用前后执行中间件(支持从对象中单独添加对象)
type GinBeforeAfter interface {
GinBefore(req *GinBeforeAfterInfo) bool
GinAfter(req *GinBeforeAfterInfo) bool
}
这是一个功能强大的 Gin 扩展库,可以大大简化 API 开发流程,提高开发效率。
更多关于golang Gin参数自动绑定与RPC工具插件库Ginrpc的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复