ginprc
golang gin 參數(shù)自動(dòng)綁定工具
- 支持rpc自動(dòng)映射
- 支持對(duì)象注冊(cè)
- 支持注解路由
- 基于 go-gin 的 json restful 風(fēng)格的golang基礎(chǔ)庫
- 自帶請(qǐng)求參數(shù)過濾及綁定實(shí)現(xiàn) binding:"required" validator
- 代碼注冊(cè)簡(jiǎn)單且支持多種注冊(cè)方式
api接口說明
支持3種接口模式
- func(gin.Context) //go-gin 原始接口
func(api.Context) //自定義的context類型 - func(api.Context,req) //自定義的context類型,帶request 請(qǐng)求參數(shù)
func(api.Context,*req) - func(gin.Context,req) //go-gin context類型,帶request 請(qǐng)求參數(shù)
func(*gin.Context,req)
示例代碼
初始化項(xiàng)目(本項(xiàng)目以ginweb 為名字)
``` go mod init ginweb ```
代碼 (詳細(xì)地址:https://github.com/xxjwxc/ginrpc/tree/master/sample/ginweb)
package main
import (
"fmt"
"net/http"
_ "ginweb/routers" // debug模式需要添加[mod]/routers 注冊(cè)注解路由
"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"` // 帶校驗(yàn)方式
Password string `json:"password"`
}
// Hello ...
type Hello struct {
Index int
}
// 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 不帶注解路由(參數(shù)為2默認(rèn)post)
func (s *Hello) Hello2(c *gin.Context, req ReqTest) {
fmt.Println(req)
fmt.Println(s.Index)
c.JSON(http.StatusOK, "ok")
}
//TestFun1 gin 默認(rèn)的函數(shù)回調(diào)地址
func TestFun1(c *gin.Context) {
fmt.Println(c.Params)
c.String(200, "ok")
}
//TestFun2 自定義context的函數(shù)回調(diào)地址
func TestFun2(c *api.Context) {
fmt.Println(c.Params)
c.JSON(http.StatusOK, "ok")
}
//TestFun3 帶自定義context跟已解析的req參數(shù)回調(diào)方式
func TestFun3(c *api.Context, req *ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, "ok")
}
//TestFun4 帶自定義context跟已解析的req參數(shù)回調(diào)方式
func TestFun4(c *gin.Context, req ReqTest) {
fmt.Println(c.Params)
fmt.Println(req)
c.JSON(http.StatusOK, req)
}
func main() {
base := ginrpc.New(ginrpc.WithCtx(func(c *gin.Context) interface{} {
return api.NewCtx(c)
}), ginrpc.WithDebug(true), ginrpc.WithGroup("xxjwxc"))
router := gin.Default()
h := new(Hello)
h.Index = 123
base.Register(router, h) // 對(duì)象注冊(cè)
router.POST("/test1", base.HandlerFunc(TestFun1)) // 函數(shù)注冊(cè)
router.POST("/test2", base.HandlerFunc(TestFun2))
router.POST("/test3", base.HandlerFunc(TestFun3))
router.POST("/test4", base.HandlerFunc(TestFun4))
base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun1) // 多種請(qǐng)求方式注冊(cè)
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"}'
注解路由
-
1.注解路由會(huì)自動(dòng)創(chuàng)建[mod]/routers/gen_router.go 文件 需要在調(diào)用時(shí)加:
_ "[mod]/routers" // debug模式需要添加[mod]/routers 注冊(cè)注解路由
默認(rèn)也會(huì)在項(xiàng)目根目錄生成[gen_router.data]文件(保留次文件矫钓,可以不用添加上面代碼嵌入)
-
2.注解路由調(diào)用方式:
base := ginrpc.New(ginrpc.WithCtx(func(c *gin.Context) interface{} { return api.NewCtx(c) }), ginrpc.WithDebug(true), ginrpc.WithGroup("xxjwxc")) base.Register(router, new(Hello)) // 對(duì)象注冊(cè) router.Run(":8080")
詳細(xì)請(qǐng)看demo ginweb
-
3.執(zhí)行curl,可以自動(dòng)參數(shù)綁定交胚。直接看結(jié)果
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"}'
-
4 參數(shù)說明
ginrpc.WithCtx : 設(shè)置自定義context
ginrpc.WithDebug(true) : 設(shè)置debug模式
ginrpc.WithGroup("xxjwxc") : 添加路由前綴 (也可以使用gin.Group 分組)
ginrpc.WithBigCamel(true) : 設(shè)置大駝峰標(biāo)準(zhǔn)(false 為web模式份汗,_,小寫)
下一步
1.導(dǎo)出api文檔
2.導(dǎo)出postman測(cè)試配置
代碼地址: ginprc 如果喜歡請(qǐng)給星支持
如果你喜歡,請(qǐng)'star'