ginprc
golang gin 參數自動綁定工具
- 支持rpc自動映射
- 支持對象註冊
- 支持註解路由
- 基於 go-gin 的 json restful 風格的golang基礎庫
- 自帶請求參數過濾及綁定實現 binding:“required” validator
- 代碼註冊簡單且支持多種註冊方式
api接口說明
支持3種接口模式
-
func(*gin.Context) //go-gin 原始接口
func(*api.Context) //自定義的context類型
-
func(*api.Context,req) //自定義的context類型,帶request 請求參數
func(*api.Context,*req)
-
func(*gin.Context,*req) //go-gin context類型,帶request 請求參數
func(*gin.Context,req)
一,參數自動綁定
package main
import (
"fmt"
"net/http"
_ "ginweb/routers" // debug模式需要添加[mod]/routers 註冊註解路由
"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"`
}
//TestFun4 帶自定義context跟已解析的req參數回調方式
func TestFun4(c *gin.Context, req ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, req)
}
func main() {
base := ginrpc.New()
router := gin.Default()
router.POST("/test4", base.HandlerFunc(TestFun4))
base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun4) // 多種請求方式註冊
router.Run(":8080")
}
-
curl
curl 'http://127.0.0.1:8080/test4' -H 'Content-Type: application/json' -d '{"access_token":"111", "user_name":"222", "password":"333"}'
二,對象註冊(註解路由)
初始化項目(本項目以ginweb 爲名字)
```go mod init ginweb ```
代碼 詳細地址>>
package main
import (
"fmt"
"net/http"
_ "ginweb/routers" // debug模式需要添加[mod]/routers 註冊註解路由
"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)
fmt.Println(s.Index)
c.JSON(http.StatusOK, "ok")
}
// Hello2 不帶註解路由(參數爲2默認post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
fmt.Println(req)
fmt.Println(s.Index)
c.JSON(http.StatusOK, "ok")
}
func main() {
base := ginrpc.New(ginrpc.WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}), ginrpc.WithDebug(true))
router := gin.Default()
group := router.Group("/xxjwxc")
base.Register(group, new(Hello)) // 對象註冊 like(go-micro)
// or base.Register(router, new(Hello))
router.Run(":8080")
}
-註解路由相關說明
// @router /block [post,get]
@router 標記
/block 路由
[post,get] method 調用方式
1. 註解路由會自動創建[mod]/routers/gen_router.go 文件 需要在調用時加:
```
_ "[mod]/routers" // debug模式需要添加[mod]/routers 註冊註解路由
```
默認也會在項目根目錄生成[gen_router.data]文件(保留此文件,可以不用添加上面代碼嵌入)
2. 註解路由調用方式:
詳細請看demo [ginweb](/sample/ginweb)
3. 相關參數說明
ginrpc.WithCtx : 設置自定義context
ginrpc.WithDebug(true) : 設置debug模式
ginrpc.WithGroup("xxjwxc") : 添加路由前綴 (也可以使用gin.Group 分組)
ginrpc.WithBigCamel(true) : 設置大駝峯標準(false 爲web模式,_,小寫)
[更多](https://godoc.org/github.com/xxjwxc/ginrpc)
4. 執行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.導出api文檔
2.導出postman測試配置