Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( RequestKey = requestKey{} ResponseKey = responseKey{} GinContextKey = ginContextKey{} )
Functions ¶
func New ¶
func New(method interface{}) gin.HandlerFunc
New example:
func Hello(ctx context.Context, req *proto.HelloHandlerReq) (*proto.HelloHandlerResp, error) {
resp := proto.HelloHandlerResp{}
resp.Welcome = fmt.Sprintf("hello, %s!", req.Name)
return &resp, nil
}
func NewWithOptions ¶
func NewWithOptions(method interface{}, options ...Option) gin.HandlerFunc
Types ¶
type Option ¶
type Option func(config *config)
func WithContext ¶ added in v1.3.5
func WithContext() Option
WithContext handler func must in 3 params, and the third param must be *gin.Context
example:
func (h *HelloHandler) HelloWithGinCtx(ctx context.Context, req *proto.HelloHandlerReq, c *gin.Context) (*proto.HelloHandlerResp, error) {
resp := proto.HelloHandlerResp{}
resp.Welcome = fmt.Sprintf("hello, %s! the uri is %s", req.Name, c.Request.RequestURI)
return &resp, nil
}
func WithResult ¶
func WithResult() Option
WithResult handler func must return 1 param and the type must be *result.Result
example:
func HelloWithResult(ctx context.Context, req *proto.HelloHandlerReq) *result.Result {
resp := proto.HelloHandlerResp{}
resp.Welcome = fmt.Sprintf("hello, %s!", req.Name)
return result.Succeed(resp)
}
func WithoutResponse ¶ added in v1.3.5
func WithoutResponse() Option
WithoutResponse handler do not deal with the response, but deal with the error. you need do it by yourself, and sometimes use both "no response" and "ginCtx"
example:
func HelloWithGinCtxAndNoResponse(ctx context.Context, req *proto.HelloHandlerReq, c *gin.Context) error {
var err error
if err != nil {
return err
}
resp := proto.HelloHandlerResp{}
resp.Welcome = fmt.Sprintf("hello, %s! the uri is %s", req.Name, c.Request.RequestURI)
c.JSON(http.StatusOK, resp)
return nil
}
Click to show internal directories.
Click to hide internal directories.