Documentation ¶
Overview ¶
http handler library, Context is the main struct of this library.
default response data format like below
{"code": 0, "payload": ...}
parse request arguments:
Context.X(type): means the argument by name is required, if not passed the argument it will panic with MissingParam error, if passed argument type is wrong it will panic with BadParam error Context.O(type): means the argument by name is optional, if not passed the argument it will return default value, if passed argument type is wrong it will return the zero value of its type
response data:
Context.OK: the api handler processed ok and response data like below {"code": 0, "payload": ...} Context.ERR: the api handler processed failed and response data like below {"code": code, "msg": error message} Context.Body: response data in byte slice Context.BodyFrom: response data from io.Reader
http response:
Context.HTTPNotFound: response http_code=404 Context.HTTPServiceUnavailable: response http_code=503 Context.HTTPTimeout: response http_code=408 Context.HTTPConflict: response http_code=409 Context.HTTPForbidden: response http_code=403
common errors:
Context.NotFound: panic with NotFound error Context.Timeout: panic with Timeout error
Index ¶
- type BadParam
- type Context
- func (ctx *Context) AddValue(k, v interface{})
- func (ctx *Context) Body(data []byte)
- func (ctx *Context) BodyFrom(r io.Reader)
- func (ctx *Context) ERR(code int, msg string)
- func (ctx *Context) File(name string) (multipart.File, *multipart.FileHeader, error)
- func (ctx *Context) HTTPConflict(msg string)
- func (ctx *Context) HTTPForbidden(msg string)
- func (ctx *Context) HTTPNotFound(what string)
- func (ctx *Context) HTTPServiceUnavailable(msg string)
- func (ctx *Context) HTTPTimeout()
- func (ctx *Context) Method() string
- func (ctx *Context) NotFound(what string)
- func (ctx *Context) OBool(name string, def bool) bool
- func (ctx *Context) OCsv(name string, def []string) []string
- func (ctx *Context) OInt(name string, def int) int
- func (ctx *Context) OInt32(name string, def int32) int32
- func (ctx *Context) OInt64(name string, def int64) int64
- func (ctx *Context) OK(payload interface{})
- func (ctx *Context) OStr(name, def string) string
- func (ctx *Context) OUInt(name string, def uint) uint
- func (ctx *Context) OUInt32(name string, def uint32) uint32
- func (ctx *Context) OUInt64(name string, def uint64) uint64
- func (ctx *Context) RawCallback(cb func(http.ResponseWriter, *http.Request))
- func (ctx *Context) ServeFile(dir string)
- func (ctx *Context) SetContentDisposition(name string)
- func (ctx *Context) SetContentType(str string)
- func (ctx *Context) Timeout()
- func (ctx *Context) Token() string
- func (ctx *Context) URI() string
- func (ctx *Context) Value(k interface{}) interface{}
- func (ctx *Context) XBool(name string) bool
- func (ctx *Context) XCsv(name string) []string
- func (ctx *Context) XInt(name string) int
- func (ctx *Context) XInt32(name string) int32
- func (ctx *Context) XInt64(name string) int64
- func (ctx *Context) XStr(name string) string
- func (ctx *Context) XUInt(name string) uint
- func (ctx *Context) XUInt32(name string) uint32
- func (ctx *Context) XUInt64(name string) uint64
- type MissingParam
- type NotFound
- type Timeout
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context api handler callback context
func NewContext ¶
func NewContext(w http.ResponseWriter, r *http.Request) *Context
NewContext create handler callback context
func (*Context) AddValue ¶
func (ctx *Context) AddValue(k, v interface{})
AddValue add key/value to this context
func (*Context) HTTPConflict ¶
HTTPConflict response conflict data error with http_code=409
func (*Context) HTTPForbidden ¶
HTTPForbidden response data forbidden error with http_code=403
func (*Context) HTTPNotFound ¶
HTTPNotFound response not found error with http_code=404
func (*Context) HTTPServiceUnavailable ¶
HTTPServiceUnavailable response service unavailable error with http_code=503
func (*Context) HTTPTimeout ¶
func (ctx *Context) HTTPTimeout()
HTTPTimeout response timeout error with http_code=408
func (*Context) OBool ¶
OBool get unrequired bool argument by name
if not found argument from request, return default value by def argument if value by name is not bool, return zero value of bool value
func (*Context) OCsv ¶
OCsv get unrequired csv argument by name
if not found argument from request, return default value by def argument in each string value of "%2c%" will change to symbol ","
func (*Context) OInt ¶
OInt get unrequired int argument by name
if not found argument from request, return default value by def argument if value by name is not int, return zero value of int value
func (*Context) OInt32 ¶
OInt32 get unrequired int32 argument by name
if not found argument from request, return default value by def argument if value by name is not int32, return zero value of int32 value
func (*Context) OInt64 ¶
OInt64 get unrequired int64 argument by name
if not found argument from request, return default value by def argument if value by name is not int64, return zero value of int64 value
func (*Context) OStr ¶
OStr get unrequired string argument by name
if not found argument from request, return default value by def argument
func (*Context) OUInt ¶
OUint get unrequired uint argument by name
if not found argument from request, return default value by def argument if value by name is not uint, return zero value of uint value
func (*Context) OUInt32 ¶
OUint32 get unrequired uint32 argument by name
if not found argument from request, return default value by def argument if value by name is not uint32, return zero value of uint32 value
func (*Context) OUInt64 ¶
OUint64 get unrequired uint64 argument by name
if not found argument from request, return default value by def argument if value by name is not uint64, return zero value of uint64 value
func (*Context) RawCallback ¶
func (ctx *Context) RawCallback(cb func(http.ResponseWriter, *http.Request))
RawCallback callback function by this context
func (*Context) SetContentDisposition ¶
SetContentDisposition set response filename
func (*Context) SetContentType ¶
SetContentType set response Content-Type of data
func (*Context) Value ¶
func (ctx *Context) Value(k interface{}) interface{}
Value get value from key in this context
func (*Context) XBool ¶
XBool get required bool argument by name
if not found argument from request, panic with MissingParam error if value by name is not bool, panic with BadParam error
func (*Context) XCsv ¶
XCsv get required csv argument by name
if not found argument from request, panic with MissingParam error in each string value of "%2c%" will change to symbol ","
func (*Context) XInt ¶
XInt get required int argument by name
if not found argument from request, panic with MissingParam error if value by name is not int, panic with BadParam error
func (*Context) XInt32 ¶
XInt32 get required int32 argument by name
if not found argument from request, panic with MissingParam error if value by name is not int32, panic with BadParam error
func (*Context) XInt64 ¶
XInt get required int64 argument by name
if not found argument from request, panic with MissingParam error if value by name is not int64, panic with BadParam error
func (*Context) XStr ¶
XStr get required string argument by name
if not found argument from request, panic with MissingParam error
func (*Context) XUInt ¶
XUInt get required uint argument by name
if not found argument from request, panic with MissingParam error if value by name is not uint, panic with BadParam error
type MissingParam ¶
type MissingParam string
MissingParam missing param error
func (MissingParam) Error ¶
func (e MissingParam) Error() string
Error get missing param error info data, format: Missing [<data>]