Documentation ¶
Index ¶
- Variables
- type Base
- func (c *Base) BindForm(ctx *iris.Context, obj interface{}) (err error)
- func (c *Base) BindJSON(ctx *iris.Context, obj interface{}) (err error)
- func (c *Base) BindXML(ctx *iris.Context, obj interface{}) (err error)
- func (c *Base) Copy(name string) *Base
- func (c *Base) LogRunningTime(from time.Time, message string)
- func (c *Base) LogWithCtx(ctx *iris.Context) *logrush.Entry
- func (c *Base) Pagination(ctx *iris.Context) (page, skip, limit int)
- func (c *Base) ParamBool(ctx *iris.Context, key string, defaultVal bool) bool
- func (c *Base) ParamBools(ctx *iris.Context, key string, defaultVal []bool) []bool
- func (c *Base) ParamFloat(ctx *iris.Context, key string, defaultVal float64) float64
- func (c *Base) ParamFloats(ctx *iris.Context, key string, defaultVal []float64) []float64
- func (c *Base) ParamInt(ctx *iris.Context, key string, defaultVal int) int
- func (c *Base) ParamInts(ctx *iris.Context, key string, defaultVal []int) []int
- func (c *Base) ParamString(ctx *iris.Context, key string, defaultVal string) string
- func (c *Base) ParamStrings(ctx *iris.Context, key string, defaultVal []string) []string
- func (c *Base) QueryBSON(ctx *iris.Context, convertors ...func(key, value string) interface{}) bson.M
- func (c *Base) QueryBool(ctx *iris.Context, key string, defaultVal bool) bool
- func (c *Base) QueryBools(ctx *iris.Context, key string, defaultVal []bool) []bool
- func (c *Base) QueryFloat(ctx *iris.Context, key string, defaultVal float64) float64
- func (c *Base) QueryFloats(ctx *iris.Context, key string, defaultVal []float64) []float64
- func (c *Base) QueryInt(ctx *iris.Context, key string, defaultVal int) int
- func (c *Base) QueryInts(ctx *iris.Context, key string, defaultVal []int) []int
- func (c *Base) QueryString(ctx *iris.Context, key string, defaultVal string) string
- func (c *Base) QueryStrings(ctx *iris.Context, key string, defaultVal []string) []string
- func (c *Base) Response(ctx *iris.Context) *Response
- func (c *Base) SetLogger(logger *logrush.Logger) *Base
- func (c *Base) SetValidateFunc(v func(interface{}) error) *Base
- func (c *Base) Sort(ctx *iris.Context) (sort, order string)
- func (c *Base) SortAsMultiple(ctx *iris.Context) (sorts []string)
- func (c *Base) Validate(obj interface{}) (err error)
- type Code
- type Response
- func (r *Response) BadRequest(message ...string)
- func (r *Response) Conflict(message ...string)
- func (r *Response) Created(message ...string)
- func (r *Response) CustomCode(code int, message ...string)
- func (r *Response) Forbidden(message ...string)
- func (r *Response) InternalServerError(message ...string)
- func (r *Response) NotFound(message ...string)
- func (r *Response) Ok(message ...string)
- func (r *Response) Redirect(urlRedirect string)
- func (r *Response) Unauthorized(message ...string)
- func (r *Response) WithCode(code Code) *Response
- func (r *Response) WithData(key string, value interface{}) *Response
- func (r *Response) WithDatas(datas iris.Map) *Response
- func (r *Response) WithStruct(v interface{}) *Response
Constants ¶
This section is empty.
Variables ¶
var ( // QueryKeyPage is the key of pagination parameter ‘page’. QueryKeyPage = "_page" // QueryKeyLimit is the key of pagination paramter 'limit' or 'per page'. QueryKeyLimit = "_limit" // DefaultPaginationPage is the default page if not provide. DefaultPaginationPage = 1 // DefaultPaginationLimit is the default page if not provide. DefaultPaginationLimit = 20 )
var ( // QueryKeySort is the key of sort parameter which defines what field should be sorted on. QueryKeySort = "_sort" // QueryKeyOrder is the key of order paramter which affects the order of query result. QueryKeyOrder = "_order" // OrderAscending represents ascending order. OrderAscending = "asc" // OrderDescending represents descending order. OrderDescending = "desc" )
var (
// QueryFormatDateTime is the formatter to parse the datetime parameter.
QueryFormatDateTime = "2006-01-02T15:04:05"
)
Functions ¶
This section is empty.
Types ¶
type Base ¶
Base controller packages the common methods.
func (*Base) BindForm ¶
BindForm binds application/x-www-form-urlencode content-type body and validates it
func (*Base) LogRunningTime ¶
LogRunningTime logs the running time from paramter now to the time this function called. So this function should always be called as defer form, like: defer c.LogRunningTime(time.Now(), message)
func (*Base) LogWithCtx ¶
LogWithCtx returns *log.Entry with fields converted from context json format body.
func (*Base) Pagination ¶
Pagination parses the pagination info from query params.
func (*Base) ParamBool ¶
ParamBool takes first value for the named component of the query and parses it to boolean. POST, PUT and PATCH body parameters take precedence over URL query string values. returns the default value if key is not present.
func (*Base) ParamBools ¶
ParamBools takes values for the named component of the query and body (if POST, PUT or PATCH). returns the default value if key is not present.
func (*Base) ParamFloat ¶
ParamFloat takes first value for the named component of the query and parses it to float64. POST, PUT and PATCH body parameters take precedence over URL query string values. returns the default value if key is not present.
func (*Base) ParamFloats ¶
ParamFloats takes values for the named component of the query and body (if POST, PUT or PATCH). returns the default value if key is not present.
func (*Base) ParamInt ¶
ParamInt takes first value for the named component of the query and parses it to int. POST, PUT and PATCH body parameters take precedence over URL query string values. returns the default value if key is not present.
func (*Base) ParamInts ¶
ParamInts takes values for the named component of the query and body (if POST, PUT or PATCH). returns the default value if key is not present.
func (*Base) ParamString ¶
ParamString takes first value for the named component of the query. POST, PUT and PATCH body parameters take precedence over URL query string values. returns the default value if key is not present.
func (*Base) ParamStrings ¶
ParamStrings takes values for the named component of the query and body (if POST, PUT or PATCH). returns the default value if key is not present.
func (*Base) QueryBSON ¶
func (c *Base) QueryBSON(ctx *iris.Context, convertors ...func(key, value string) interface{}) bson.M
QueryBSON parses query paramter as bson.M follow the same practice as the JSON-Server is. JSON-Server's Home: https://github.com/typicode/json-server Glance: - Add `_gt`, `_gte` or `_lt`, `_lte` for getting a range - Add `_ne` to exclude a value - Add `_like` to filter (RegExp supported) - Use `.` to access deep properties
func (*Base) QueryBool ¶
QueryBool parses query parameter as boolean if exist, or returns defaultVal.
func (*Base) QueryBools ¶
QueryBools parses query parameter as boolean array if exist, or returns defaultVal.
func (*Base) QueryFloat ¶
QueryFloat parses query parameter as float64 if exist, or returns defaultVal.
func (*Base) QueryFloats ¶
QueryFloats parses query parameter as float64 array if exist, or returns defaultVal.
func (*Base) QueryInts ¶
QueryInts parses query parameter as int array if key exist, or returns defaultVal.
func (*Base) QueryString ¶
QueryString parses query parameter as string if key exist, or returns defaultVal.
func (*Base) QueryStrings ¶
QueryStrings parses query parameter as string array if key exist, or returns defaultVal.
func (*Base) SetValidateFunc ¶
SetValidateFunc allows to set a validate function for request binding validation. You can set it to nil to disable validation.
func (*Base) Sort ¶
Sort parses the sort info from query params. Suppose the sort settings are all defaults, there are two valid forms: - json-server style: ?_sort=filed&order=DESC - mgo style: ?_sort=-field The order parameter is case-insensitive.
func (*Base) SortAsMultiple ¶
SortAsMultiple parses the sort info from query params and convert to mgo style. The rules:
- If the sort parameter is empty, return an empty array.
- Elseif the sort parameter can be splited by comma, split it and return.
- Else the order will be considered: 3.1 If the order is `DESC`, prepend a minus to the sort parameter. 3.2 return an array contains the (may be changed)sort parameter.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
Response can make a response with given datas and status.
func (*Response) BadRequest ¶
BadRequest render json format response with iris.StatusBadRequest and given message.
func (*Response) Conflict ¶
Conflict render json format response with iris.StatusConflict and given message.
func (*Response) CustomCode ¶
CustomCode render json format response with given http code and message.
func (*Response) Forbidden ¶
Forbidden render json format response with iris.StatusForbidden and given message.
func (*Response) InternalServerError ¶
InternalServerError render json format response with iris.InternalServerError and given message.
func (*Response) NotFound ¶
NotFound render json format response with iris.StatusNotFound and given message.
func (*Response) Redirect ¶
Redirect redirects the client to the specific url using 302 http code. The data attached will be encoded as url parameters.
func (*Response) Unauthorized ¶
Unauthorized render json format response with iris.StatusUnauthorized and given message.
func (*Response) WithData ¶
WithData sets the given key-value pair to the response json body. It will overwrites the value with same key.
func (*Response) WithDatas ¶
WithDatas sets multiple key-value pairs to the response json body. All values has the same key as the `datas` has will be overwrited.
func (*Response) WithStruct ¶
WithStruct converts the struct `v` to map and does the same things as what `WithDatas` does.