api

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BadParam

type BadParam string

BadParam bad param error

func (BadParam) Error

func (e BadParam) Error() string

Error get bad param error info data, format: BadParam [<data>]

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

func (ctx *Context) Body(data []byte)

Body response data from byte slice

func (*Context) BodyFrom

func (ctx *Context) BodyFrom(r io.Reader)

BodyFrom response data from reader

func (*Context) ERR

func (ctx *Context) ERR(code int, msg string)

ERR response api handle failed

func (*Context) File

func (ctx *Context) File(name string) (multipart.File, *multipart.FileHeader, error)

File get upload file argument by name

func (*Context) HTTPConflict

func (ctx *Context) HTTPConflict(msg string)

HTTPConflict response conflict data error with http_code=409

func (*Context) HTTPForbidden

func (ctx *Context) HTTPForbidden(msg string)

HTTPForbidden response data forbidden error with http_code=403

func (*Context) HTTPNotFound

func (ctx *Context) HTTPNotFound(what string)

HTTPNotFound response not found error with http_code=404

func (*Context) HTTPServiceUnavailable

func (ctx *Context) HTTPServiceUnavailable(msg string)

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

func (ctx *Context) Method() string

Method get request method

func (*Context) NotFound

func (ctx *Context) NotFound(what string)

NotFound response not found error

func (*Context) OBool

func (ctx *Context) OBool(name string, def bool) bool

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

func (ctx *Context) OCsv(name string, def []string) []string

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

func (ctx *Context) OInt(name string, def int) int

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

func (ctx *Context) OInt32(name string, def int32) int32

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

func (ctx *Context) OInt64(name string, def int64) int64

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

func (ctx *Context) OK(payload interface{})

OK response api handle ok

func (*Context) OStr

func (ctx *Context) OStr(name, def string) string

OStr get unrequired string argument by name

if not found argument from request, return default value by def argument

func (*Context) OUInt

func (ctx *Context) OUInt(name string, def uint) uint

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

func (ctx *Context) OUInt32(name string, def uint32) uint32

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

func (ctx *Context) OUInt64(name string, def uint64) uint64

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

func (ctx *Context) ServeFile(dir string)

ServeFile response file data from dir

func (*Context) SetContentDisposition

func (ctx *Context) SetContentDisposition(name string)

SetContentDisposition set response filename

func (*Context) SetContentType

func (ctx *Context) SetContentType(str string)

SetContentType set response Content-Type of data

func (*Context) Timeout

func (ctx *Context) Timeout()

Timeout response timeout error

func (*Context) Token

func (ctx *Context) Token() string

Token get token from request header by X-Token field

func (*Context) URI

func (ctx *Context) URI() string

URI get request uri

func (*Context) Value

func (ctx *Context) Value(k interface{}) interface{}

Value get value from key in this context

func (*Context) XBool

func (ctx *Context) XBool(name string) bool

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

func (ctx *Context) XCsv(name string) []string

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

func (ctx *Context) XInt(name string) int

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

func (ctx *Context) XInt32(name string) int32

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

func (ctx *Context) XInt64(name string) int64

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

func (ctx *Context) XStr(name string) string

XStr get required string argument by name

if not found argument from request, panic with MissingParam error

func (*Context) XUInt

func (ctx *Context) XUInt(name string) uint

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

func (*Context) XUInt32

func (ctx *Context) XUInt32(name string) uint32

XUint32 get required uint32 argument by name

if not found argument from request, panic with MissingParam error
if value by name is not uint32, panic with BadParam error

func (*Context) XUInt64

func (ctx *Context) XUInt64(name string) uint64

XUint64 get required uint64 argument by name

if not found argument from request, panic with MissingParam error
if value by name is not uint64, 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>]

type NotFound

type NotFound string

NotFound not found error

func (NotFound) Error

func (e NotFound) Error() string

Error get not found error info data, format: <data> not found

type Timeout

type Timeout struct{}

Timeout timeout error

func (Timeout) Error

func (e Timeout) Error() string

Error get timeout error info data, format: timeout

Jump to

Keyboard shortcuts

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