Documentation
¶
Index ¶
- Variables
- func CheckError(err error, log *log.LogWrap) bool
- func ContainsError(w http.ResponseWriter, RouteSignature string, err error, code int) bool
- func ResponseError(w http.ResponseWriter, routeID string, msg string, code int)
- func ResponseJSON(w http.ResponseWriter, routeID string, v interface{})
- type CommonResp
- type Ctx
- func (ctx *Ctx) Bind(obj interface{}) error
- func (ctx *Ctx) BindJSON(obj interface{}) error
- func (ctx *Ctx) BindQuery(obj interface{}) error
- func (ctx *Ctx) BindWith(obj interface{}, b binding.Binding) error
- func (ctx *Ctx) BodyParameterValues(name string) ([]string, error)
- func (ctx *Ctx) ContainsError(err error, code int, v ...interface{}) bool
- func (ctx *Ctx) ErrMsg(code int) string
- func (ctx *Ctx) Form(name string) param.Parameter
- func (ctx *Ctx) Header(name string) param.Parameter
- func (ctx *Ctx) Host() string
- func (ctx *Ctx) IP() string
- func (ctx *Ctx) IsLogin() (userID string, err error)
- func (ctx *Ctx) Must(err error, code int, v ...interface{})
- func (ctx *Ctx) MustBind(obj interface{}, code int, v ...interface{})
- func (ctx *Ctx) MustBindJSON(obj interface{}, code int, v ...interface{})
- func (ctx *Ctx) MustBindQuery(obj interface{}, code int, v ...interface{})
- func (ctx *Ctx) MustBindWith(obj interface{}, b binding.Binding, code int, v ...interface{})
- func (ctx *Ctx) Path(name string) param.Parameter
- func (ctx *Ctx) Proxy() []string
- func (ctx *Ctx) Query(name string) param.Parameter
- func (ctx *Ctx) Redirect(url string, code int)
- func (ctx *Ctx) ResponseError(msg string, code int)
- func (ctx *Ctx) ResponseJSON(v interface{})
- func (ctx *Ctx) ResponseStdErrCode(code int, v ...interface{})
- func (ctx *Ctx) RouteID() string
- func (ctx *Ctx) RouteSignature() string
- func (ctx *Ctx) UserID() string
Constants ¶
This section is empty.
Variables ¶
var ( RouteIDMap = make(map[string]string) RouteErrMap = make(map[string]map[int]string) GlobalErrMap = make(map[int]string) )
var CommonResponse = func(w http.ResponseWriter, routeID string, code int, message string, data interface{}) { if err := writeJSON(w, http.StatusOK, CommonResp{ Code: code, Message: message, Data: data, RouteID: routeID, }); err != nil { log.Warn().Err(err).Msg("json encode") } }
CommonResponse is a response func. just replace it if you'd like to custom response.
var DisableErrHandler bool
DisableErrHandler disables error handler using errc.
Functions ¶
func CheckError ¶
CheckError is a convenience method to check error is nil. If error is nil, it will return true, else it will log the error and return false
func ContainsError ¶
ContainsError is a convenience method to check error is nil. If error is nil, it will return false, else it will log the error, make a CommonResp response and return true. if code is 0, it will use err.Error() as CommonResp.message.
func ResponseError ¶
func ResponseError(w http.ResponseWriter, routeID string, msg string, code int)
ResponseError is a convenience method to response an error code and message. It uses jsoniter for marshalling the value.
func ResponseJSON ¶
func ResponseJSON(w http.ResponseWriter, routeID string, v interface{})
ResponseJSON is a convenience method for writing a value wrap in CommonResp as JSON. It uses jsoniter for marshalling the value.
Types ¶
type CommonResp ¶
type CommonResp struct { Code int `json:"code"` Message string `json:"message"` Data interface{} `json:"data"` RouteID string `json:"route_id,omitempty"` }
CommonResp with code, message and data
type Ctx ¶
Ctx wrap *restful.Request and *restful.Response in one struct.
func (*Ctx) Bind ¶
Bind checks the Content-Type to select a binding engine automatically, Depending the "Content-Type" header different bindings are used:
"application/json" --> JSON binding "application/xml" --> XML binding
otherwise --> returns an error. It parses the request's body as JSON if Content-Type == "application/json" using JSON or XML as a JSON input. It decodes the json payload into the struct specified as a pointer. It writes a 400 error and sets Content-Type header "text/plain" in the response if input is not valid.
func (*Ctx) BindWith ¶
BindWith binds the passed struct pointer using the specified binding engine. See the binding package.
func (*Ctx) BodyParameterValues ¶
BodyParameterValues returns the array of parameter in a POST form body.
func (*Ctx) ContainsError ¶
ContainsError is a convenience method to check error is nil. If error is nil, it will return false, else it will log the error, make a CommonResp response and return true. if code is 0, it will use err.Error() as CommonResp.message.
func (*Ctx) IsLogin ¶
IsLogin gets JWT token in request by OAuth2Extractor, and parse it with CheckToken.
func (*Ctx) MustBindJSON ¶
MustBindJSON is a shortcur for ctx.Must(ctx.BindJSON(obj), code, v...)
func (*Ctx) MustBindQuery ¶
MustBindQuery is a shortcur for ctx.Must(ctx.BindQuery(obj), code, v...)
func (*Ctx) MustBindWith ¶
MustBindWith is a shortcur for ctx.Must(ctx.BindWith(obj, b), code, v...)
func (*Ctx) ResponseError ¶
ResponseError is a convenience method to response an error code and message. It uses jsoniter for marshalling the value.
func (*Ctx) ResponseJSON ¶
func (ctx *Ctx) ResponseJSON(v interface{})
ResponseJSON is a convenience method for writing a value wrap in CommonResp as JSON. It uses jsoniter for marshalling the value.
func (*Ctx) ResponseStdErrCode ¶
ResponseStdErrCode is a convenience method response a code with msg in Code Desc.
func (*Ctx) RouteSignature ¶
RouteSignature returns the signature of current route. Example: /v1/user/login POST