box

package
v0.4.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2019 License: LGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	RouteIDMap   = make(map[string]string)
	RouteErrMap  = make(map[string]map[int]string)
	GlobalErrMap = make(map[int]string)
)
View Source
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.

View Source
var DisableErrHandler bool

DisableErrHandler disables error handler using errc.

View Source
var MustLogger = func(ctx *Ctx, code int, msg string, err error) {
	log.Info().
		Str("routeID", ctx.RouteID()).
		Str("routeSig", ctx.RouteSignature()).
		Int("code", code).
		Str("msg", msg).
		Str(zerolog.ErrorFieldName, fmt.Sprintf("%+v\n", err)).
		Msg("verify error")
}

MustLogger, override it to customize the log output of must

Functions

func CheckError

func CheckError(err error, log *log.Wrap) bool

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

func ContainsError(w http.ResponseWriter, RouteSignature string, err error, code int) bool

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

type Ctx struct {
	*restful.Request
	*restful.Response
	*restful.FilterChain
	ErrCatcher errc.Catcher
}

Ctx wrap *restful.Request and *restful.Response in one struct.

func (*Ctx) Bind

func (ctx *Ctx) Bind(obj interface{}) error

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) BindJSON

func (ctx *Ctx) BindJSON(obj interface{}) error

BindJSON is a shortcut for ctx.BindWith(obj, binding.JSON).

func (*Ctx) BindQuery

func (ctx *Ctx) BindQuery(obj interface{}) error

BindQuery is a shortcut for ctx.BindWith(obj, binding.Query).

func (*Ctx) BindWith

func (ctx *Ctx) BindWith(obj interface{}, b binding.Binding) error

BindWith binds the passed struct pointer using the specified binding engine. See the binding package.

func (*Ctx) BodyParameterValues

func (ctx *Ctx) BodyParameterValues(name string) ([]string, error)

BodyParameterValues returns the array of parameter in a POST form body.

func (*Ctx) ContainsError

func (ctx *Ctx) ContainsError(err error, code int, v ...interface{}) bool

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) ErrMsg

func (ctx *Ctx) ErrMsg(code int) string

ErrMsg returns the message of a error code in current route.

func (*Ctx) Form

func (ctx *Ctx) Form(name string) param.Parameter

Form reads form parameter with name.

func (*Ctx) Header

func (ctx *Ctx) Header(name string) param.Parameter

Header reads header parameter with name.

func (*Ctx) Host added in v0.4.1

func (ctx *Ctx) Host() string

Host returns the host of request.

func (*Ctx) IP

func (ctx *Ctx) IP() string

IP returns the IP address of request.

func (*Ctx) IsLogin

func (ctx *Ctx) IsLogin() (userID string, err error)

IsLogin gets JWT token in request by OAuth2Extractor, and parse it with CheckToken.

func (*Ctx) Must

func (ctx *Ctx) Must(err error, code int, v ...interface{})

Must causes a return from a function if err is not nil.

func (*Ctx) MustBind

func (ctx *Ctx) MustBind(obj interface{}, code int, v ...interface{})

MustBind is a shortcur for ctx.Must(ctx.Bind(obj), code, v...)

func (*Ctx) MustBindJSON

func (ctx *Ctx) MustBindJSON(obj interface{}, code int, v ...interface{})

MustBindJSON is a shortcur for ctx.Must(ctx.BindJSON(obj), code, v...)

func (*Ctx) MustBindQuery

func (ctx *Ctx) MustBindQuery(obj interface{}, code int, v ...interface{})

MustBindQuery is a shortcur for ctx.Must(ctx.BindQuery(obj), code, v...)

func (*Ctx) MustBindWith

func (ctx *Ctx) MustBindWith(obj interface{}, b binding.Binding, code int, v ...interface{})

MustBindWith is a shortcur for ctx.Must(ctx.BindWith(obj, b), code, v...)

func (*Ctx) Path

func (ctx *Ctx) Path(name string) param.Parameter

Path reads path parameter with name.

func (*Ctx) Proxy

func (ctx *Ctx) Proxy() []string

Proxy returns the proxy endpoints behind a request.

func (*Ctx) Query

func (ctx *Ctx) Query(name string) param.Parameter

Query reads query parameter with name.

func (*Ctx) Redirect

func (ctx *Ctx) Redirect(url string, code int)

Redirect replies to the request with a redirect to url.

func (Ctx) Req added in v0.4.4

func (ctx Ctx) Req() *http.Request

Req returns http.Request of ctx.

func (Ctx) Resp added in v0.4.4

func (ctx Ctx) Resp() http.ResponseWriter

Resp returns http.ResponseWriter of ctx.

func (*Ctx) ResponseError

func (ctx *Ctx) ResponseError(msg string, code int)

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

func (ctx *Ctx) ResponseStdErrCode(code int, v ...interface{})

ResponseStdErrCode is a convenience method response a code with msg in Code Desc.

func (*Ctx) RouteID

func (ctx *Ctx) RouteID() string

RouteID returns the RouteID of current route.

func (*Ctx) RouteSignature

func (ctx *Ctx) RouteSignature() string

RouteSignature returns the signature of current route. Example: /v1/user/login POST

func (*Ctx) UserID

func (ctx *Ctx) UserID() string

UserID returns UserID stored in attribute.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL