controllers

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2017 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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
)
View Source
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"
)
View Source
var (
	// QueryFormatDateTime is the formatter to parse the datetime parameter.
	QueryFormatDateTime = "2006-01-02T15:04:05"
)

Functions

This section is empty.

Types

type Base

type Base struct {
	L *logrush.Logger
	// contains filtered or unexported fields
}

Base controller packages the common methods.

func New

func New() *Base

New instances a new Base controller object.

func (*Base) BindForm

func (c *Base) BindForm(ctx *iris.Context, obj interface{}) (err error)

BindForm binds application/x-www-form-urlencode content-type body and validates it

func (*Base) BindJSON

func (c *Base) BindJSON(ctx *iris.Context, obj interface{}) (err error)

BindJSON binds application/json content-type body and validates it

func (*Base) BindXML

func (c *Base) BindXML(ctx *iris.Context, obj interface{}) (err error)

BindXML binds application/xml content-type body and validates it

func (*Base) Copy

func (c *Base) Copy(name string) *Base

Copy replicates a new Base controller with given name.

func (*Base) LogRunningTime

func (c *Base) LogRunningTime(from time.Time, message string)

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

func (c *Base) LogWithCtx(ctx *iris.Context) *logrush.Entry

LogWithCtx returns *log.Entry with fields converted from context json format body.

func (*Base) Pagination

func (c *Base) Pagination(ctx *iris.Context) (page, skip, limit int)

Pagination parses the pagination info from query params.

func (*Base) ParamBool

func (c *Base) ParamBool(ctx *iris.Context, key string, defaultVal bool) bool

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

func (c *Base) ParamBools(ctx *iris.Context, key string, defaultVal []bool) []bool

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

func (c *Base) ParamFloat(ctx *iris.Context, key string, defaultVal float64) float64

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

func (c *Base) ParamFloats(ctx *iris.Context, key string, defaultVal []float64) []float64

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

func (c *Base) ParamInt(ctx *iris.Context, key string, defaultVal int) int

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

func (c *Base) ParamInts(ctx *iris.Context, key string, defaultVal []int) []int

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

func (c *Base) ParamString(ctx *iris.Context, key string, defaultVal string) string

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

func (c *Base) ParamStrings(ctx *iris.Context, key string, defaultVal []string) []string

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

func (c *Base) QueryBool(ctx *iris.Context, key string, defaultVal bool) bool

QueryBool parses query parameter as boolean if exist, or returns defaultVal.

func (*Base) QueryBools

func (c *Base) QueryBools(ctx *iris.Context, key string, defaultVal []bool) []bool

QueryBools parses query parameter as boolean array if exist, or returns defaultVal.

func (*Base) QueryFloat

func (c *Base) QueryFloat(ctx *iris.Context, key string, defaultVal float64) float64

QueryFloat parses query parameter as float64 if exist, or returns defaultVal.

func (*Base) QueryFloats

func (c *Base) QueryFloats(ctx *iris.Context, key string, defaultVal []float64) []float64

QueryFloats parses query parameter as float64 array if exist, or returns defaultVal.

func (*Base) QueryInt

func (c *Base) QueryInt(ctx *iris.Context, key string, defaultVal int) int

QueryInt parses query parameter as int if key exist, or returns defaultVal.

func (*Base) QueryInts

func (c *Base) QueryInts(ctx *iris.Context, key string, defaultVal []int) []int

QueryInts parses query parameter as int array if key exist, or returns defaultVal.

func (*Base) QueryString

func (c *Base) QueryString(ctx *iris.Context, key string, defaultVal string) string

QueryString parses query parameter as string if key exist, or returns defaultVal.

func (*Base) QueryStrings

func (c *Base) QueryStrings(ctx *iris.Context, key string, defaultVal []string) []string

QueryStrings parses query parameter as string array if key exist, or returns defaultVal.

func (*Base) Response

func (c *Base) Response(ctx *iris.Context) *Response

Response makes a response instance by context ctx provided.

func (*Base) SetLogger

func (c *Base) SetLogger(logger *logrush.Logger) *Base

SetLogger sets logger of base controller.

func (*Base) SetValidateFunc

func (c *Base) SetValidateFunc(v func(interface{}) error) *Base

SetValidateFunc allows to set a validate function for request binding validation. You can set it to nil to disable validation.

func (*Base) Sort

func (c *Base) Sort(ctx *iris.Context) (sort, order string)

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

func (c *Base) SortAsMultiple(ctx *iris.Context) (sorts []string)

SortAsMultiple parses the sort info from query params and convert to mgo style. The rules:

  1. If the sort parameter is empty, return an empty array.
  2. Elseif the sort parameter can be splited by comma, split it and return.
  3. 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.

func (*Base) Validate

func (c *Base) Validate(obj interface{}) (err error)

Validate validates struct type obj.

type Code

type Code interface {
	Code() int
	Humanize() string
}

Code could be a user defined number with specific meaning.

type Response

type Response struct {
	// contains filtered or unexported fields
}

Response can make a response with given datas and status.

func (*Response) BadRequest

func (r *Response) BadRequest(message ...string)

BadRequest render json format response with iris.StatusBadRequest and given message.

func (*Response) Conflict

func (r *Response) Conflict(message ...string)

Conflict render json format response with iris.StatusConflict and given message.

func (*Response) Created

func (r *Response) Created(message ...string)

Created render json format response with iris.Created and given message.

func (*Response) CustomCode

func (r *Response) CustomCode(code int, message ...string)

CustomCode render json format response with given http code and message.

func (*Response) Forbidden

func (r *Response) Forbidden(message ...string)

Forbidden render json format response with iris.StatusForbidden and given message.

func (*Response) InternalServerError

func (r *Response) InternalServerError(message ...string)

InternalServerError render json format response with iris.InternalServerError and given message.

func (*Response) NotFound

func (r *Response) NotFound(message ...string)

NotFound render json format response with iris.StatusNotFound and given message.

func (*Response) Ok

func (r *Response) Ok(message ...string)

Ok render json format response with iris.OK and given message.

func (*Response) Redirect

func (r *Response) Redirect(urlRedirect string)

Redirect redirects the client to the specific url using 302 http code. The data attached will be encoded as url parameters.

func (*Response) Unauthorized

func (r *Response) Unauthorized(message ...string)

Unauthorized render json format response with iris.StatusUnauthorized and given message.

func (*Response) WithCode

func (r *Response) WithCode(code Code) *Response

WithCode sets biz code of the response.

func (*Response) WithData

func (r *Response) WithData(key string, value interface{}) *Response

WithData sets the given key-value pair to the response json body. It will overwrites the value with same key.

func (*Response) WithDatas

func (r *Response) WithDatas(datas iris.Map) *Response

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

func (r *Response) WithStruct(v interface{}) *Response

WithStruct converts the struct `v` to map and does the same things as what `WithDatas` does.

Jump to

Keyboard shortcuts

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