res

package module
v0.0.0-...-d1ddda6 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2021 License: Apache-2.0 Imports: 7 Imported by: 4

README

说明

GIN + ENT/SQLX + CASBIN + WIRE + I18N

为了保持系统精简性(目前数据库处理只使用SQLX), 暂时移除了ENT相关的内容, 结构暂时保留

Documentation

Index

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: "请求发生错误"}}
	Err401Unauthorized     = &ErrorModel{Status: 401, ShowType: ShowWarn, ErrorMessage: &i18n.Message{ID: "ERR-UNAUTHORIZED", 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 GetAcceptLanguage

func GetAcceptLanguage(c Context) string

GetAcceptLanguage 获取浏览器语言

func GetCtxValue

func GetCtxValue(c Context, key string) (interface{}, bool)

GetCtxValue 获取令牌加密方式

func GetCtxValueToString

func GetCtxValueToString(c Context, key string) (string, bool)

GetCtxValueToString 获取令牌加密方式

func JSONMarshalToString

func JSONMarshalToString(v interface{}) string

JSONMarshalToString JSON编码为字符串

func JoinPath

func JoinPath(paths ...string) string

JoinPath 拼接路由

func JoinRouter

func JoinRouter(method, path string) string

JoinRouter 拼接路由

func SetCtxValue

func SetCtxValue(c Context, key string, value interface{})

SetCtxValue 配置令牌加密方式

func SkipHandler

func SkipHandler(c Context, skippers ...SkipperFunc) bool

SkipHandler 统一处理跳过函数

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 New0Error

func New0Error(ctx Context, showType int, emsg *i18n.Message) *ErrorInfo

New0Error 包装响应错误, 没有参数

func New0ErrorWithData

func New0ErrorWithData(ctx Context, showType int, emsg *i18n.Message, data interface{}) *ErrorInfo

New0ErrorWithData 包装响应错误, 没有参数

func NewError

func NewError(ctx Context, showType int, emsg *i18n.Message, args map[string]interface{}) *ErrorInfo

NewError 包装响应错误

func NewErrorWithData

func NewErrorWithData(ctx Context, showType int, emsg *i18n.Message, args map[string]interface{}, data interface{}) *ErrorInfo

NewErrorWithData 包装响应错误

func NewWrapError

func NewWrapError(ctx Context, em *ErrorModel) *ErrorInfo

NewWrapError 包装响应错误

func (*ErrorInfo) Error

func (e *ErrorInfo) Error() string

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 ErrorNone

type ErrorNone struct {
}

ErrorNone 返回值已经被处理,无返回值

func (*ErrorNone) Error

func (e *ErrorNone) 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 H

type H map[string]interface{}

H h -> map

type HandlerFunc

type HandlerFunc func(Context)

HandlerFunc ...

func EmptyMiddleware

func EmptyMiddleware() HandlerFunc

EmptyMiddleware 不执行业务处理的中间件

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 分页查询条件

func (*PageParam) First

func (p *PageParam) First() uint

func (*PageParam) Max

func (p *PageParam) Max() uint

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

type SkipperFunc func(Context) bool

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 令牌存储接口

type Success

type Success struct {
	Success bool        `json:"success"`        // 请求成功, false
	Data    interface{} `json:"data,omitempty"` // 响应数据
	TraceID string      `json:"traceId"`        // 方便进行后端故障排除:唯一的请求ID
}

Success 正常请求结构体

func NewSuccess

func NewSuccess(ctx Context, data interface{}) *Success

NewSuccess 包装响应结果

func (*Success) Error

func (e *Success) Error() string

Jump to

Keyboard shortcuts

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