支持调用前后中间件 (ginrpc.WithBeforeAfter
)
go get -u github.com/xxjwxc/ginrpc@master
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
func(*gin.Context,req)(resp,error)
go mod init gmsec
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")
}
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 调用方式
_ "[mod]/routers" // debug模式需要添加[mod]/routers 注册注解路由
默认也会在项目根目录生成 gen_router.data
文件 (保留此文件,可以不用添加上面代码嵌入)
ginrpc.WithCtx : 设置自定义 context
ginrpc.WithDebug(true) : 设置 debug 模式
ginrpc.WithBigCamel(true) : 设置大驼峰标准 (false 为 web 模式,_,小写)
ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{}) : 设置调用前后执行中间件
ginrpc.Register
模式,支持文档导出tag
.default
)/docs/swagger/swagger.json
,/docs/markdown
)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
}