Documentation
¶
Index ¶
- Constants
- Variables
- func GetAcceptLanguage(c Context) string
- func GetCtxValue(c Context, key string) (interface{}, bool)
- func GetCtxValueToString(c Context, key string) (string, bool)
- func JSONMarshalToString(v interface{}) string
- func JoinPath(paths ...string) string
- func JoinRouter(method, path string) string
- func SetCtxValue(c Context, key string, value interface{})
- func SkipHandler(c Context, skippers ...SkipperFunc) bool
- type Context
- type ErrorInfo
- func New0Error(ctx Context, showType int, emsg *i18n.Message) *ErrorInfo
- func New0ErrorWithData(ctx Context, showType int, emsg *i18n.Message, data interface{}) *ErrorInfo
- func NewError(ctx Context, showType int, emsg *i18n.Message, args map[string]interface{}) *ErrorInfo
- func NewErrorWithData(ctx Context, showType int, emsg *i18n.Message, args map[string]interface{}, ...) *ErrorInfo
- func NewWrapError(ctx Context, em *ErrorModel) *ErrorInfo
- type ErrorModel
- type ErrorNone
- type ErrorRedirect
- type H
- type HandlerFunc
- type PageParam
- type PageResult
- type SkipperFunc
- type Storer
- type Success
Constants ¶
View Source
const ( UserInfoKey = prefix + ":user-info" // user info TraceIDKey = prefix + ":tract-id" // trace id ReqBodyKey = prefix + ":req-body" // request body ResBodyKey = prefix + ":res-body" // response body ResJwtKey = prefix + ":res-jwt-kid" // jwt kid ResO2cKey = prefix + ":res-o2c-kid" // o2c kid ResS2cKey = prefix + ":res-s2c-kid" // s2c kid, 子母令牌, 标记母令牌ID XReqOriginHostKey = "X-Request-Origin-Host" XReqOriginPathKey = "X-Request-Origin-Path" XReqOriginMethodKey = "X-Request-Origin-Method" )
定义上下文中的键
View Source
const ( // ShowNone 静音 ShowNone = 0 // ShowWarn 消息警告 ShowWarn = 1 // ShowError 消息错误 ShowError = 2 // ShowNotify 通知; ShowNotify = 4 // ShowPage 页 ShowPage = 9 )
Variables ¶
View Source
var ( Err400BadRequest = &ErrorModel{Status: 400, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-BAD-REQUEST", Other: "请求发生错误"}} Err403Forbidden = &ErrorModel{Status: 403, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-FORBIDDEN", Other: "用户未得到授权,访问是被禁止的"}} Err404NotFound = &ErrorModel{Status: 404, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-NOT-FOUND", Other: "发出的请求针对的是不存在的记录,服务器没有进行操作"}} Err405MethodNotAllowed = &ErrorModel{Status: 405, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-METHOD-NOT-ALLOWED", Other: "请求的方法不允许"}} Err406NotAcceptable = &ErrorModel{Status: 406, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-NOT-ACCEPTABLE", Other: "请求的格式不可得"}} Err429TooManyRequests = &ErrorModel{Status: 429, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-TOO-MANY-REQUESTS", Other: "请求次数过多"}} Err456TokenExpired = &ErrorModel{Status: 456, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-TOKEN-EXPIRED", Other: "请求令牌已过期"}} Err500InternalServer = &ErrorModel{Status: 500, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-INTERNAL-SERVER", Other: "服务器发生错误"}} )
定义错误 https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/405
View Source
var ( JSONMarshal = json.Marshal JSONUnmarshal = json.Unmarshal JSONMarshalIndent = json.MarshalIndent JSONNewDecoder = json.NewDecoder JSONNewEncoder = json.NewEncoder )
定义JSON操作
View Source
var ( // Response of request ResponseTypeJSON = "application/json; charset=utf-8" ResponseTypeTEXT = "text/plain; charset=utf-8" )
定义Type
Functions ¶
func GetCtxValueToString ¶
GetCtxValueToString 获取令牌加密方式
func JSONMarshalToString ¶
func JSONMarshalToString(v interface{}) string
JSONMarshalToString JSON编码为字符串
Types ¶
type Context ¶
type Context interface { context.Context GetTraceID() string FormatMessage(emsg *i18n.Message, args map[string]interface{}) string GetRequest() *http.Request // 请求头 GetHeader(string) string // 获取请求头 Next() // 执行下一个 Abort() // 终止执行 Get(key string) (interface{}, bool) Set(key string, value interface{}) }
type ErrorInfo ¶
type ErrorInfo struct { Success bool `json:"success"` // 请求成功, false Data interface{} `json:"data,omitempty"` // 响应数据 ErrorCode string `json:"errorCode"` // 错误代码 ErrorMessage string `json:"errorMessage"` // 向用户显示消息 ShowType int `json:"showType"` // 错误显示类型:0静音; 1条消息警告; 2消息错误; 4通知; 9页 TraceID string `json:"traceId"` // 方便进行后端故障排除:唯一的请求ID }
ErrorInfo 异常的请求结果体
func New0ErrorWithData ¶
New0ErrorWithData 包装响应错误, 没有参数
func NewError ¶
func NewError(ctx Context, showType int, emsg *i18n.Message, args map[string]interface{}) *ErrorInfo
NewError 包装响应错误
type ErrorModel ¶
type ErrorModel struct { Status int ShowType int ErrorMessage *i18n.Message ErrorArgs map[string]interface{} }
ErrorModel 异常模型
func Wrap400Response ¶
func Wrap400Response(ctx Context, err error) *ErrorModel
Wrap400Response 无法解析异常
func (*ErrorModel) Error ¶
func (a *ErrorModel) Error() string
type ErrorRedirect ¶
type ErrorRedirect struct { Status int // http.StatusSeeOther State string // 状态, 用户还原缓存现场 Location string }
ErrorRedirect 重定向
func (*ErrorRedirect) Error ¶
func (e *ErrorRedirect) Error() string
type PageParam ¶
type PageParam struct { PageSign string `form:"pageSign"` // 请求参数, total | list | both PageNo uint `form:"pageNo,default=1"` // 当前页 PageSize uint `form:"pageSize,default=20"` // 页大小 binding:"max=100" Total uint `form:"total"` // 上次统计的数据条数 }
PageParam 分页查询条件
type PageResult ¶
type PageResult struct { PageSign string `json:"pageSign,default=both,omitempty"` // 请求参数, total | list | both PageNo uint `json:"pageNo,omitempty"` // 页索引 PageSize uint `json:"pageSize,omitempty"` // 页条数 Total uint `json:"total,omitempty"` // 总条数 List interface{} `json:"list,omitempty"` // 数据 }
PageResult 分页数据
type SkipperFunc ¶
SkipperFunc 定义中间件跳过函数
func AllowMethodAndPathPrefixSkipper ¶
func AllowMethodAndPathPrefixSkipper(prefixes ...string) SkipperFunc
AllowMethodAndPathPrefixSkipper 检查请求方法和路径是否包含指定的前缀,如果不包含则跳过
func AllowPathPrefixNoSkipper ¶
func AllowPathPrefixNoSkipper(prefixes ...string) SkipperFunc
AllowPathPrefixNoSkipper 检查请求路径是否包含指定的前缀,如果包含则不跳过
func AllowPathPrefixSkipper ¶
func AllowPathPrefixSkipper(prefixes ...string) SkipperFunc
AllowPathPrefixSkipper 检查请求路径是否包含指定的前缀,如果包含则跳过
type Storer ¶
type Storer interface { // 存储令牌数据,并指定到期时间 Set(ctx context.Context, key, value string, expiration time.Duration) error // 获取存储数据 Get(ctx context.Context, key string) (string, bool, error) // 检查令牌是否存在 Check(ctx context.Context, key string) (bool, error) // 存放一个键值, 只用来确定是否存在 Set1(ctx context.Context, key string, expiration time.Duration) error // 删除存储的令牌 Delete(ctx context.Context, key string) error // 关闭存储 Close() error }
Storer 令牌存储接口
Source Files
¶
Click to show internal directories.
Click to hide internal directories.