context

package
v0.0.0-...-e43fadc Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2021 License: Apache-2.0 Imports: 31 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CT_DEF = iota
	CT_JSON
	CT_XML
	CT_YMAL
	CT_HTML
	CT_PLAIN
	CT_OTHER
)
View Source
const ERR_ACCEPTED = 202
View Source
const ERR_BAD_GATEWAY = 502
View Source
const ERR_BAD_REQUEST = 400 //参数错误

4请求错误(4字头)

View Source
const ERR_BANDWIDTH_LIMIT_EXCEEDED = 509
View Source
const ERR_CONFLICT = 409
View Source
const ERR_CONTINUE = 100

1消息(1字头)

View Source
const ERR_CREATED = 201
View Source
const ERR_EXPECTATION_FAILED = 417
View Source
const ERR_FAILED_DEPENDENCY = 424
View Source
const ERR_FORBIDDEN = 403
View Source
const ERR_GATEWAY_TIMEOUT = 504
View Source
const ERR_GONE = 410
View Source
const ERR_HTTP_VERSION_NOT_SUPPORTED = 505
View Source
const ERR_INSUFFICIENT_STORAGE = 507
View Source
const ERR_LENGTH_REQUIRED = 411
View Source
const ERR_LOCKED = 423
View Source
const ERR_METHOD_NOT_ALLOWED = 405
View Source
const ERR_MOVED_PERMANENTLY = 301
View Source
const ERR_MOVE_TEMPORARILY = 302
View Source
const ERR_MULTIPLE_CHOICES = 300

3重定向(3字头)

View Source
const ERR_MULTI_STATUS = 207
View Source
const ERR_NON_AUTHORITATIVE_INFORMATION = 203
View Source
const ERR_NOT_ACCEPTABLE = 406 //缺少必须参数
View Source
const ERR_NOT_EXTENDED = 510
View Source
const ERR_NOT_FOUND = 404
View Source
const ERR_NOT_IMPLEMENTED = 501
View Source
const ERR_NOT_MODIFIED = 304
View Source
const ERR_NO_CONTENT = 204
View Source
const ERR_OK = 200

2成功(2字头)

View Source
const ERR_PARTIAL_CONTENT = 206
View Source
const ERR_PAYMENT_REQUIRED = 402
View Source
const ERR_PRECONDITION_FAILED = 412
View Source
const ERR_PROCESSING = 102
View Source
const ERR_PROXY_AUTHENTICATION_REQUIRED = 407
View Source
const ERR_REQUESTED_RANGE_NOT_SATISFIABLE = 416
View Source
const ERR_REQUEST_ENTITY_TOO_LARGE = 413
View Source
const ERR_REQUEST_TIMEOUT = 408
View Source
const ERR_REQUEST_URI_TOO_LONG = 414
View Source
const ERR_RESET_CONTENT = 205
View Source
const ERR_RETRY_WITH = 449
View Source
const ERR_SEE_OTHER = 303
View Source
const ERR_SERVER_ERROR = 500 //系统繁忙

5服务器错误(5、6字头)

View Source
const ERR_SERVICE_UNAVAILABLE = 503
View Source
const ERR_SWITCHING_PROTOCOLS = 101
View Source
const ERR_SWITCH_PROXY = 306
View Source
const ERR_TEMPORARY_REDIRECT = 307
View Source
const ERR_UNAUTHORIZED = 401
View Source
const ERR_UNAVAILABLE_FOR_LEGAL_REASONS = 451
View Source
const ERR_UNORDERED_COLLECTION = 425
View Source
const ERR_UNPARSEABLE_RESPONSE_HEADERS = 600
View Source
const ERR_UNPROCESSABLE_ENTITY = 422
View Source
const ERR_UNSUPPORTED_MEDIA_TYPE = 415
View Source
const ERR_UPGRADE_REQUIRED = 426
View Source
const ERR_USE_PROXY = 305
View Source
const ERR_constIANT_ALSO_NEGOTIATES = 506
View Source
const FAILURE = 900 //明确失败

自定义状态码

Variables

View Source
var ContentTypes = map[int]string{
	CT_JSON:  "application/json; charset=UTF-8",
	CT_XML:   "text/xml; charset=UTF-8",
	CT_YMAL:  "text/ymal; charset=UTF-8",
	CT_HTML:  "text/html; charset=UTF-8",
	CT_PLAIN: "text/plain; charset=UTF-8",
}

ContentTypes content type map

View Source
var ERR_DataNotExist = errors.New("查询的数据不存在")
View Source
var WSExchange = NewExchange()

WSExchange web socket exchange

Functions

func GetCode

func GetCode(err interface{}) int

GetCode 获取错误码

func MakeSign

func MakeSign(input map[string]interface{}, key string) string

MakeSign 检查签名原串

Types

type Context

type Context struct {
	Request  *Request
	Response *Response
	RPC      *ContextRPC

	Component  interface{}
	Meta       *Meta
	Log        logger.ILogger
	Name       string
	Engine     string
	Service    string
	GinContext *gin.Context
	// contains filtered or unexported fields
}

Context 引擎执行上下文

func GetContext

func GetContext(g *gin.Context, component interface{}, name string, engine string, service string, container IContainer, queryString IData, form IData, param IData, setting IData, ext map[string]interface{}, logger *logger.Logger) *Context

GetContext 从缓存池中获取一个context

func (*Context) BuildJwt

func (c *Context) BuildJwt(data interface{}) (string, error)

BuildJwt 构建JWT

func (*Context) Close

func (c *Context) Close()

Close 回收context

func (*Context) GetContainer

func (c *Context) GetContainer() IContainer

GetContainer 获取当前容器

func (*Context) NewDLock

func (c *Context) NewDLock(name string) (lk *dlock.DLock)

NewDLock 基于当前注册中心创建分布式锁

func (*Context) NewDLockByRegistry

func (c *Context) NewDLockByRegistry(name string, registry string) (lk *dlock.DLock, err error)

NewDLockByRegistry 指定注册中心地址创建分布式锁

func (*Context) SetRPC

func (c *Context) SetRPC(rpc RPCInvoker)

SetRPC 根据输入的context创建插件的上下文对象

type ContextRPC

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

ContextRPC rpc操作实例

func (*ContextRPC) AsyncRequest

func (cr *ContextRPC) AsyncRequest(service string, header map[string]string, form map[string]interface{}, failFast bool) rpc.IRPCResponse

AsyncRequest 异步请求

func (*ContextRPC) PreInit

func (cr *ContextRPC) PreInit(services ...string) error

PreInit 预加载服务

func (*ContextRPC) Request

func (cr *ContextRPC) Request(service string, header map[string]string, form map[string]interface{}, failFast bool) (status int, r string, param map[string]string, err error)

Request RPC请求

func (*ContextRPC) RequestFailRetry

func (cr *ContextRPC) RequestFailRetry(service string, header map[string]string, form map[string]interface{}, times int) (status int, r string, param map[string]string, err error)

RequestFailRetry RPC请求

func (*ContextRPC) RequestMap

func (cr *ContextRPC) RequestMap(service string, header map[string]string, form map[string]interface{}, failFast bool) (status int, r map[string]interface{}, param map[string]string, err error)

RequestMap RPC请求返回结果转换为map

type Error

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

func NewError

func NewError(code int, err interface{}) *Error

NewError 创建错误对象

func NewErrorf

func NewErrorf(code int, f string, args ...interface{}) *Error

NewErrorf 创建错误对象

func NewIgnoreError

func NewIgnoreError(code int, err interface{}) *Error

NewIgnoreError 当前一个可忽略的错误

func (*Error) CanIgnore

func (a *Error) CanIgnore() bool

CanIgnore 是否可以忽略错误

func (*Error) GetCode

func (a *Error) GetCode() int

func (*Error) GetError

func (a *Error) GetError() error

func (*Error) String

func (a *Error) String() string

type Exchange

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

Exchange 数据交换中心

func NewExchange

func NewExchange() *Exchange

NewExchange 构建数据交换中心

func (*Exchange) Broadcast

func (e *Exchange) Broadcast(v ...interface{}) error

Broadcast 发送广播消息

func (*Exchange) Clear

func (e *Exchange) Clear()

Clear 清除所有订阅者

func (*Exchange) Notify

func (e *Exchange) Notify(name string, i ...interface{}) error

Notify 消息通知

func (*Exchange) Relate

func (e *Exchange) Relate(uuid string, name string)

Relate 关联别名

func (*Exchange) Subscribe

func (e *Exchange) Subscribe(uuid string, f func(...interface{}) error) error

Subscribe 订阅消息通知

func (*Exchange) Unsubscribe

func (e *Exchange) Unsubscribe(uuid string)

Unsubscribe 取消订阅

type IContainer

type IContainer interface {
	RPCInvoker

	conf.ISystemConf
	conf.IVarConf
	conf.IMainConf
	GetRegistry() registry.IRegistry

	GetRegularCache(names ...string) (c cache.ICache)
	GetCache(names ...string) (c cache.ICache, err error)
	GetCacheBy(tpName string, name string) (c cache.ICache, err error)
	SaveCacheObject(tpName string, name string, f func(c conf.IConf) (cache.ICache, error)) (bool, cache.ICache, error)

	GetRegularDB(names ...string) (d db.IDB)
	GetDB(names ...string) (d db.IDB, err error)
	GetDBBy(tpName string, name string) (c db.IDB, err error)
	SaveDBObject(tpName string, name string, f func(c conf.IConf) (db.IDB, error)) (bool, db.IDB, error)

	GetRegularInflux(names ...string) (c influxdb.IInfluxClient)
	GetInflux(names ...string) (d influxdb.IInfluxClient, err error)
	GetInfluxBy(tpName string, name string) (c influxdb.IInfluxClient, err error)
	SaveInfluxObject(tpName string, name string, f func(c conf.IConf) (influxdb.IInfluxClient, error)) (bool, influxdb.IInfluxClient, error)

	GetRegularQueue(names ...string) (c queue.IQueue)
	GetQueue(names ...string) (q queue.IQueue, err error)
	GetQueueBy(tpName string, name string) (c queue.IQueue, err error)
	SaveQueueObject(tpName string, name string, f func(c conf.IConf) (queue.IQueue, error)) (bool, queue.IQueue, error)

	GetGlobalObject(tpName string, name string) (c interface{}, err error)
	SaveGlobalObject(tpName string, name string, f func(c conf.IConf) (interface{}, error)) (bool, interface{}, error)
	Close() error
}

type IContextRPC

type IContextRPC interface {
	PreInit(services ...string) error
	RequestFailRetry(service string, input map[string]interface{}, times int) (status int, r string, param map[string]string, err error)
	Request(service string, input map[string]interface{}, failFast bool) (status int, r string, param map[string]string, err error)
	RequestMap(service string, input map[string]interface{}, failFast bool) (status int, r map[string]interface{}, param map[string]string, err error)
}

IContextRPC rpc基础操作

type IData

type IData interface {
	Get(string) (interface{}, bool)
	Keys() []string
}

type IError

type IError interface {
	GetError() error
	GetCode() int
	Error() string
	CanIgnore() bool
}

type IResponse

type IResponse interface {
	GetContent() interface{}
	GetCode(interface{}) int
	GetStatus() int
	SetStatus(int)
	GetView() string
	GetParams() map[string]interface{}
	SetParams(v map[string]interface{})
	SetParam(key string, v interface{})
	GetHTMLRenderContent() (int, interface{}, error)
	GetJSONRenderContent() (int, interface{}, error)
	ShouldContent(content interface{})
	MustContent(status int, content interface{})
	IsRedirect() (string, bool)
	GetHeaders() map[string]string
	SetHeader(name string, value string)
	SetHeaders(map[string]string)
	SetJWT(data interface{})
	ShouldStatus(status int)
	HasError(v interface{}) bool
	GetError() error
	IsSuccess() bool
	JSON(content interface{})
	XML(content interface{})
	Text(content interface{})
	HTML(content interface{})
}

IResponse 响应

type IResult

type IResult interface {
	GetResult() interface{}
	GetCode() int
}

type Meta

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

Meta 元数据

func NewMeta

func NewMeta() *Meta

NewMeta 创建meta

func (*Meta) Check

func (i *Meta) Check(names ...string) error

Check 检查是否包含指定的参数

func (*Meta) Clear

func (i *Meta) Clear()

func (*Meta) Get

func (m *Meta) Get(name string) (interface{}, bool)

Get 获取元数据

func (*Meta) GetDataTime

func (i *Meta) GetDataTime(name string, p ...time.Time) (time.Time, error)

func (*Meta) GetDataTimeByFormat

func (i *Meta) GetDataTimeByFormat(name string, format string, p ...time.Time) (time.Time, error)

GetFloat64 获取float64数字

func (*Meta) GetFloat64

func (i *Meta) GetFloat64(name string, p ...float64) float64

GetFloat64 获取float64数字

func (*Meta) GetInt

func (i *Meta) GetInt(name string, p ...int) int

GetInt 获取int数字

func (*Meta) GetInt64

func (i *Meta) GetInt64(name string, p ...int64) int64

GetInt64 获取int64数字

func (*Meta) GetString

func (i *Meta) GetString(name string, p ...string) string

func (*Meta) Keys

func (i *Meta) Keys() []string

func (*Meta) Set

func (m *Meta) Set(key string, value interface{})

Set 设置元数据

func (*Meta) Translate

func (i *Meta) Translate(format string, a bool) (string, int)

Translate 翻译带有@变量的字符串

type RPCInvoker

type RPCInvoker interface {
	PreInit(services ...string) (err error)
	RequestFailRetry(service string, method string, header map[string]string, input map[string]interface{}, times int) (status int, result string, params map[string]string, err error)
	Request(service string, method string, header map[string]string, input map[string]interface{}, failFast bool) (status int, result string, param map[string]string, err error)
	AsyncRequest(service string, method string, header map[string]string, input map[string]interface{}, failFast bool) rpc.IRPCResponse
	WaitWithFailFast(callback func(string, int, string, error), timeout time.Duration, rs ...rpc.IRPCResponse) error
}

type Request

type Request struct {
	Form           *inputParams
	QueryString    *inputParams
	Param          *inputParams
	Setting        *inputParams
	CircuitBreaker *circuitBreakerParam //熔断处理
	Http           *httpRequest
	// contains filtered or unexported fields
}

Request 输入参数

func (*Request) Bind

func (r *Request) Bind(obj interface{}) error

Bind 根据输入参数绑定对象

func (*Request) BindWith

func (r *Request) BindWith(obj interface{}, contentType string) error

BindWith 根据输入参数绑定对象

func (*Request) Body2Input

func (r *Request) Body2Input(encoding ...string) (map[string]string, error)

Body2Input 根据编码格式解码body参数,并更新input参数

func (*Request) Check

func (r *Request) Check(field ...string) error

Check 检查输入参数和配置参数是否为空

func (*Request) CheckSign

func (r *Request) CheckSign(secret string, f ...string) (bool, error)

CheckSign 检查签名是否正确(只值为空不参与签名,键值及每个串之前使用空进行连接)

func (*Request) CheckSignAll

func (r *Request) CheckSignAll(secret string, all bool, a string, b string, f ...string) (bool, error)

CheckSignAll 检查签名是否正确

func (Request) Clear

func (w Request) Clear()

func (*Request) Get

func (r *Request) Get(name string) (result string, b bool)

Get 获取请求数据值

func (Request) GetBindWithFunc

func (w Request) GetBindWithFunc() func(i interface{}, c string) error

func (Request) GetBindingFunc

func (w Request) GetBindingFunc() func(i interface{}) error

func (Request) GetBody

func (w Request) GetBody(encoding ...string) (string, error)

func (Request) GetBodyMap

func (w Request) GetBodyMap(encoding ...string) (map[string]interface{}, error)

func (*Request) GetDataTime

func (r *Request) GetDataTime(name string, p ...time.Time) (time.Time, error)

GetDataTime 获取日期时间

func (*Request) GetDataTimeByFormat

func (r *Request) GetDataTimeByFormat(name string, format string, p ...time.Time) (time.Time, error)

GetDataTimeByFormat 获取日期时间

func (*Request) GetFloat32

func (r *Request) GetFloat32(name string, p ...float32) float32

GetFloat32 获取float64数字

func (*Request) GetFloat64

func (r *Request) GetFloat64(name string, p ...float64) float64

GetFloat64 获取float64数字

func (Request) GetHeader

func (w Request) GetHeader() map[string]string

func (*Request) GetInt

func (r *Request) GetInt(name string, p ...int) int

GetInt 获取int数字

func (*Request) GetInt64

func (r *Request) GetInt64(name string, p ...int64) int64

GetInt64 获取int64数字

func (Request) GetJWT

func (w Request) GetJWT(out interface{}) error

GetJWTBody 获取jwt存储数据

func (Request) GetJWTBody

func (w Request) GetJWTBody() interface{}

GetJWTBody 获取jwt存储数据

func (Request) GetJWTConfig

func (w Request) GetJWTConfig() (*conf.Auth, error)

GetJWTConfig 获取jwt配置信息

func (*Request) GetKeys

func (r *Request) GetKeys() []string

func (Request) GetMethod

func (w Request) GetMethod() string
func (w *extParams) Get(name string) (interface{}, bool) {
	v, ok := w.ext[name]
	return v, ok
}

func (Request) GetRequestMap

func (w Request) GetRequestMap(encoding ...string) map[string]interface{}

func (Request) GetSharding

func (w Request) GetSharding() (int, int)

GetSharding 获取任务分片信息(分片索引[从1开始],分片总数)

func (*Request) GetSignRaw

func (r *Request) GetSignRaw(all bool, a string, b string, f ...string) (string, string)

GetSignRaw 检查签名原串

func (*Request) GetString

func (r *Request) GetString(name string, p ...string) string

func (Request) GetUUID

func (w Request) GetUUID() string

GetUUID

func (Request) SkipJWTExclude

func (w Request) SkipJWTExclude() (bool, error)

func (*Request) Translate

func (r *Request) Translate(format string, a bool) string

Translate 根据输入参数[Param,Form,QueryString,Setting]

type Response

type Response struct {
	Status int

	Content    interface{}
	Params     map[string]interface{}
	SkipHandle bool
	// contains filtered or unexported fields
}

func NewResponse

func NewResponse() *Response

func (*Response) GetCode

func (r *Response) GetCode(c interface{}) int

func (*Response) GetContent

func (r *Response) GetContent() interface{}

func (*Response) GetError

func (r *Response) GetError() error

func (*Response) GetHTMLRenderContent

func (r *Response) GetHTMLRenderContent() (int, interface{}, error)

func (*Response) GetHeaders

func (r *Response) GetHeaders() map[string]string

GetHeaders 获取http头配置

func (*Response) GetJSONRenderContent

func (r *Response) GetJSONRenderContent() (int, interface{}, error)

func (*Response) GetParams

func (r *Response) GetParams() map[string]interface{}

func (*Response) GetStatus

func (r *Response) GetStatus() int

func (*Response) GetView

func (r *Response) GetView() string

GetView 获取view

func (*Response) HTML

func (r *Response) HTML(content interface{})

HTML 按text/HTML

func (*Response) HasError

func (r *Response) HasError(v interface{}) bool

func (*Response) IsRedirect

func (r *Response) IsRedirect() (string, bool)

IsRedirect 是否是URL转跳

func (*Response) IsSuccess

func (r *Response) IsSuccess() bool

func (*Response) JSON

func (r *Response) JSON(content interface{})

JSON 按json格式输入

func (*Response) MakeSign

func (r *Response) MakeSign(input map[string]interface{}, key string) (string, string)

MakeSign 生成签名串

func (*Response) MakeSignAll

func (r *Response) MakeSignAll(input map[string]interface{}, key string, a string, b string) (string, string)

MakeSignAll 生成签名串,排序,并将key放到原串最后

func (*Response) MustContent

func (r *Response) MustContent(status int, content interface{})

func (*Response) NoView

func (r *Response) NoView()

NoView 设置view

func (*Response) Redirect

func (r *Response) Redirect(code int, url string)

Redirect 设置页面转跳

func (*Response) SetContentType

func (r *Response) SetContentType(p string)

func (*Response) SetCookie

func (r *Response) SetCookie(name string, value string, timeout int, domain string)

SetCookie 设置cookie

func (*Response) SetHTML

func (r *Response) SetHTML()

SetTextHTML 将content type设置为text/html; charset=UTF-8

func (*Response) SetHeader

func (r *Response) SetHeader(name string, value string)

SetHeader 设置http头

func (*Response) SetHeaders

func (r *Response) SetHeaders(h map[string]string)

func (*Response) SetJSON

func (r *Response) SetJSON()

SeTextJSON 将content type设置为application/json; charset=UTF-8

func (*Response) SetJWT

func (r *Response) SetJWT(data interface{})

func (*Response) SetParam

func (r *Response) SetParam(key string, v interface{})

func (*Response) SetParams

func (r *Response) SetParams(v map[string]interface{})

func (*Response) SetPlain

func (r *Response) SetPlain()

SetTextPlain 将content type设置为text/plain; charset=UTF-8

func (*Response) SetStatus

func (r *Response) SetStatus(status int)

func (*Response) SetView

func (r *Response) SetView(name string)

SetView 设置view

func (*Response) SetXML

func (r *Response) SetXML()

SetTextXML 将content type设置为text/xml; charset=UTF-8

func (*Response) ShouldContent

func (r *Response) ShouldContent(content interface{})

func (*Response) ShouldStatus

func (r *Response) ShouldStatus(status int)

func (*Response) Success

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

func (*Response) Text

func (r *Response) Text(content interface{})

Text 按text/plain格式输入

func (*Response) XML

func (r *Response) XML(content interface{})

XML 按xml格式输入

type Result

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

func NewResult

func NewResult(code int, content interface{}) *Result

NewResult 创建

func NewResultf

func NewResultf(code int, f string, args ...interface{}) *Result

NewResultf 创建带状态码的返回对象

func (*Result) GetCode

func (a *Result) GetCode() int

func (*Result) GetResult

func (a *Result) GetResult() interface{}

Jump to

Keyboard shortcuts

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