box

package
v0.6.5 Latest Latest
Warning

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

Go to latest
Published: Apr 16, 2024 License: LGPL-3.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BiuAttrErr        = "__BIU_ERROR__"
	BiuAttrErrLine    = "__BIU_ERROR_LINE__"
	BiuAttrErrCode    = "__BIU_ERROR_CODE__"
	BiuAttrErrMsg     = "__BIU_ERROR_MESSAGE__"
	BiuAttrErrArgs    = "__BIU_ERROR_ARGS__"
	BiuAttrRouteID    = "__BIU_ROUTE_ID__"
	BiuAttrAuthUserID = "__BIU_AUTH_USER_ID__"
	BiuAttrEntities   = "__BIU_ENTITIES__"
)
View Source
const CtxSignature = "github.com/tuotoo/biu/box.Ctx"
View Source
const FileSignature = "github.com/tuotoo/biu/box.File"

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonResp

type CommonResp struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Data    any    `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
	Logger     *slog.Logger
}

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

func (*Ctx) Bind

func (ctx *Ctx) Bind(obj any) 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 any) error

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

func (*Ctx) BindQuery

func (ctx *Ctx) BindQuery(obj any) error

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

func (*Ctx) BindWith

func (ctx *Ctx) BindWith(obj any, 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 ...any) 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) 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(ck *auth.Instance) (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 ...any)

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

func (*Ctx) MustBind

func (ctx *Ctx) MustBind(obj any, code int, v ...any)

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

func (*Ctx) MustBindJSON

func (ctx *Ctx) MustBindJSON(obj any, code int, v ...any)

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

func (*Ctx) MustBindQuery

func (ctx *Ctx) MustBindQuery(obj any, code int, v ...any)

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

func (*Ctx) MustBindWith

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

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

func (*Ctx) Next added in v0.5.0

func (ctx *Ctx) Next()

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(code int, msg string)

ResponseError is a convenience method to response an error code and message.

func (*Ctx) ResponseJSON

func (ctx *Ctx) ResponseJSON(v ...any)

ResponseJSON is a convenience method for writing a value wrap in CommonResp as JSON.

func (*Ctx) ResponseStdErrCode

func (ctx *Ctx) ResponseStdErrCode(code int, v ...any)

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) Transform added in v0.5.0

func (ctx *Ctx) Transform(f func(...any) []any)

func (*Ctx) UserID

func (ctx *Ctx) UserID() string

UserID returns UserID stored in attribute.

type File added in v0.5.0

type File struct {
	multipart.File
	Header *multipart.FileHeader
}

Jump to

Keyboard shortcuts

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