ex

package
v0.0.0-...-2668dd0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrBadRequest = NewError(http.StatusBadRequest, 40000, "bad request")
	ErrAuth       = NewError(http.StatusUnauthorized, 40001, "unauthorized")
	ErrForbidden  = NewError(http.StatusForbidden, 40002, "forbidden")
	ErrReachLimit = NewError(http.StatusTooManyRequests, 40003, "too many requests")
	ErrParam      = NewError(http.StatusBadRequest, 40005, "invalid parameters")
	ErrNotFound   = NewError(http.StatusNotFound, 40006, "resource not found")
	ErrConflict   = NewError(http.StatusConflict, 40007, "resource exists")

	ErrInternal = NewError(http.StatusInternalServerError, 50000, "internal server error")
	ErrDB       = NewError(http.StatusInternalServerError, 50001, "db error")
	ErrRedis    = NewError(http.StatusInternalServerError, 50002, "redis error")

	ErrThirdAPI = NewError(http.StatusServiceUnavailable, 60000, "third api error")
)

global err

View Source
var (
	// DefaultRecoverConfig is the default Recover middleware config.
	DefaultRecoverConfig = RecoverConfig{
		StackSize:         4 << 10,
		DisableStackAll:   false,
		DisablePrintStack: false,
	}
)

Functions

func ActivityType

func ActivityType(fl validator.FieldLevel) bool

ActivityType validate activity type

func AuthRequired

func AuthRequired() echo.MiddlewareFunc

AuthRequired middleware

func ErrHandler

func ErrHandler(e *echo.Echo) func(err error, c echo.Context)

ErrHandler handler

func IPRateLimit

func IPRateLimit(store limiter.Store, rate limiter.Rate) echo.MiddlewareFunc

IPRateLimit by ip

func Logger

func Logger(logger *zap.Logger) echo.MiddlewareFunc

Logger for types access

func NewStore

func NewStore(rdb *redis.Client, prefix string, keyPairs []byte) *store

NewStore 返回 redis store

func NewTraceCtx

func NewTraceCtx(c echo.Context) context.Context

func OK

func OK(c echo.Context, data interface{}) error

OK 正常返回

func Recover

func Recover() echo.MiddlewareFunc

Recover returns a middleware which recovers from panics anywhere in the chain and handles the control to the centralized HTTPErrorHandler.

func RecoverWithConfig

func RecoverWithConfig(config RecoverConfig) echo.MiddlewareFunc

RecoverWithConfig returns a Recover middleware with config. See: `Recover()`.

func SetRequestID

func SetRequestID() echo.MiddlewareFunc

RequestID 获取并设置 request id

func StatsField

func StatsField(fl validator.FieldLevel) bool

StatsField validate stats field

func StatsFreq

func StatsFreq(fl validator.FieldLevel) bool

StatsFreq validate stats freq

func StatsMethod

func StatsMethod(fl validator.FieldLevel) bool

StatsMethod validate stats method

Types

type CustomValidator

type CustomValidator struct {
	Validator *validator.Validate
}

func NewValidator

func NewValidator() *CustomValidator

func (*CustomValidator) Validate

func (cv *CustomValidator) Validate(i interface{}) error

type Error

type Error struct {
	Status       int    `json:"-"`               // Status http 状态码
	Code         int    `json:"code"`            // Code 错误状态码
	Message      string `json:"message"`         // Message 错误信息
	ErrorMessage string `json:"error,omitempty"` // ErrorMessage 只有在非 gin.ReleaseMode 模式下才会在接口中展示,但会在日志中展示
}

Error 封装接口错误信息

func Bind

func Bind(c echo.Context, target interface{}) *Error

func NewError

func NewError(status, code int, message string) *Error

NewError return a error wrap

func (*Error) Error

func (e *Error) Error() string

Error 返回错误信息

func (*Error) Msg

func (e *Error) Msg(msg string) *Error

Msg wrap msg

func (*Error) Wrap

func (e *Error) Wrap(err error) *Error

Wrap 包装 err

type GobSerializer

type GobSerializer struct{}

GobSerializer gob 序列化、反序列化

func (GobSerializer) Deserialize

func (s GobSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize 反序列化

func (GobSerializer) Serialize

func (s GobSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize 序列化

type JSONSerializer

type JSONSerializer struct{}

JSONSerializer json 序列化、反序列化

func (JSONSerializer) Deserialize

func (s JSONSerializer) Deserialize(d []byte, ss *sessions.Session) error

Deserialize 反序列化

func (JSONSerializer) Serialize

func (s JSONSerializer) Serialize(ss *sessions.Session) ([]byte, error)

Serialize 序列化

type RecoverConfig

type RecoverConfig struct {
	// Size of the stack to be printed.
	// Optional. Default value 4KB.
	StackSize int `yaml:"stack_size"`

	// DisableStackAll disables formatting stack traces of all other goroutines
	// into buffer after the trace for the current goroutine.
	// Optional. Default value false.
	DisableStackAll bool `yaml:"disable_stack_all"`

	// DisablePrintStack disables printing stack trace.
	// Optional. Default value as false.
	DisablePrintStack bool `yaml:"disable_print_stack"`
}

RecoverConfig defines the config for Recover middleware.

type RedisStore

type RedisStore struct {
	Codecs  []securecookie.Codec
	Options *sessions.Options // default configuration
	// contains filtered or unexported fields
}

RedisStore session redis 存储

func NewRedisStore

func NewRedisStore(rdb *redis.Client, prefix string, keyPairs []byte) *RedisStore

NewRedisStore instantiates a RedisStore

func (*RedisStore) Get

func (s *RedisStore) Get(r *http.Request, name string) (*sessions.Session, error)

Get returns a session for the given name after adding it to the registry.

See gorilla/sessions FilesystemStore.Get().

func (*RedisStore) New

func (s *RedisStore) New(r *http.Request, name string) (*sessions.Session, error)

New returns a session for the given name without adding it to the registry.

See gorilla/sessions FilesystemStore.New().

func (*RedisStore) Save

func (s *RedisStore) Save(_ *http.Request, w http.ResponseWriter, session *sessions.Session) error

Save adds a single session to the response.

func (*RedisStore) SetKeyPrefix

func (s *RedisStore) SetKeyPrefix(p string)

SetKeyPrefix set the prefix

func (*RedisStore) SetMaxAge

func (s *RedisStore) SetMaxAge(v int)

SetMaxAge restricts the maximum age, in seconds, of the session record both in database and a browser. This is to change session storage configuration. If you want just to remove session use your session `s` object and change it's `Options.MaxAge` to -1, as specified in

http://godoc.org/github.com/gorilla/sessions#Options

Default is the one provided by this package value - `sessionExpire`. Set it to 0 for no restriction. Because we use `MaxAge` also in SecureCookie crypting algorithm you should use this function to change `MaxAge` value.

func (*RedisStore) SetMaxLength

func (s *RedisStore) SetMaxLength(l int)

SetMaxLength sets RedisStore.maxLen if the `l` argument is greater or equal 0 maxLen restricts the maximum length of new sessions to l. If l is 0 there is no limit to the size of a session, use with caution. The default for a new RedisStore is 4096. Redis allows for max. value sizes of up to 512MB (http://redis.io/topics/data-types) Default: 4096,

func (*RedisStore) SetSerializer

func (s *RedisStore) SetSerializer(ss SessionSerializer)

SetSerializer sets the serializer

type SessionSerializer

type SessionSerializer interface {
	Deserialize(d []byte, ss *sessions.Session) error
	Serialize(ss *sessions.Session) ([]byte, error)
}

SessionSerializer 序列化、反序列化接口

type User

type User struct {
	ID       int64
	SourceID int64
	Source   string
	Email    string
}

func GetUser

func GetUser(c echo.Context) User

Jump to

Keyboard shortcuts

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