gin auto binding,grpc, and annotated route,gin 注解路由, grpc,自动参数绑定工具
Support call before and after deal(
ginrpc.WithBeforeAfter)
go get -u github.com/xxjwxc/[email protected]
func(*api.Context) // Custom context type
func(api.Context,req)
func(*gin.Context,req)
func(*gin.Context,req)(resp,error)
gmsec)
``` go mod init gmsec ```
package mainimport ( "fmt" "net/http"
_ "gmsec/routers" // Debug mode requires adding [mod] / routes to register annotation routes.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 stringjson:"user_name" binding:"required"
// With verification mode Password stringjson:"password"
}// Hello ... type Hello struct { }
// Hello Annotated route (bese on beego way) // @Router /block [post,get] func (s *Hello) Hello(c *api.Context, req *ReqTest) { fmt.Println(req) c.JSON(http.StatusOK, "ok") }
// Hello2 Route without annotation (the parameter is 2 default post) func (s *Hello) Hello2(c *gin.Context, req ReqTest) { fmt.Println(req) c.JSON(http.StatusOK, "ok") }
// grpc-go // with request,return parameter and error // TestFun6 Route without annotation (the parameter is 2 default post) 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() // or router := gin.Default().Group("/xxjwxc") base.Register(router, new(Hello)) // object register like(go-micro) router.POST("/test6", base.HandlerFunc(TestFun6)) // function register base.RegisterHandlerFunc(router, []string{"post", "get"}, "/test", TestFun6) router.Run(":8080")
}
// @Router /block [post,get] @Router tag /block router [post,get] method
#### Note: if there is no annotation route in the object function, the system will add annotation route by default. Post mode: with req (2 parameters (CTX, req)), get mode is a parameter (CTX)
[mod]/routes/gen_router.gofile, which needs to be added when calling:
``` _ "[mod]/routers" // Debug mode requires adding [mod] / routes to register annotation routes```
By default, the [gen_router. Data] file will also be generated in the root directory of the project (keep this file, and you can embed it without adding the above code)
more to saying [gmsec](https://github.com/gmsec/gmsec)
ginrpc.WithCtx : Set custom contextginrpc.WithDebug(true) : Set debug mode
ginrpc.WithOutDoc(true) : output markdown/swagger api doc
ginrpc.WithBigCamel(true) : Set big camel standard (false is web mode, _, lowercase)
ginrpc.WithBeforeAfter(&ginrpc.DefaultGinBeforeAfter{}) : Before After call
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"}'
ginrpc.WithOutDoc(true) : output markdown/swagger
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{})
go // GinBeforeAfter Execute middleware before and after the object call (support adding the object separately from the object in total) type GinBeforeAfter interface { GinBefore(req *GinBeforeAfterInfo) bool GinAfter(req *GinBeforeAfterInfo) bool }