gm

package
v0.0.0-...-2c6e621 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2021 License: MIT Imports: 24 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")
	ErrOauth2     = NewError(http.StatusBadRequest, 40004, "oauth2 error")

	ErrParam = NewError(http.StatusBadRequest, 40005, "invalid parameters")

	ErrNotFound = NewError(http.StatusNotFound, 40006, "resource not found")

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

Functions

func Abort

func Abort(c *gin.Context, e *Error)

Abort 错误终止,需配合错误处理中间件

func Csrf

func Csrf(options *Options, name string) gin.HandlerFunc

Csrf validates CSRF token.

func CsrfHandler

func CsrfHandler() gin.HandlerFunc

CsrfHandler 校验 csrf

func ErrHandler

func ErrHandler() gin.HandlerFunc

ErrHandler 错误处理中间件

func GetRealIP

func GetRealIP(c *gin.Context) string

GetRealIP 获取真实 IP

func GetReqID

func GetReqID(c *gin.Context) string

GetReqID 获取 request id

func GetToken

func GetToken(c *gin.Context, name string) string

GetToken returns a CSRF token.

func GinZap

func GinZap(logger *zap.Logger) gin.HandlerFunc

GinZap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

func NewStore

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

NewStore 返回 redis store

func OK

func OK(c *gin.Context, data interface{})

OK 正常返回

func PromHandler

func PromHandler(handler http.Handler) gin.HandlerFunc

PromHandler wrappers the standard http.Handler to gin.HandlerFunc metrics 接口

func PromMiddleware

func PromMiddleware(promOpts *PromOpts) gin.HandlerFunc

PromMiddleware returns a gin.HandlerFunc for exporting some Web metrics

func RealIP

func RealIP(remoteIPHeaders []string) gin.HandlerFunc

RealIP 由于 gin 1.7.x 的 bug 导致 c.ClientIP() 无法获取正确的真实 IP 由于安全因素,请确保 gin 是部署在 proxy(caddy,nginx) 后面, 且 proxy 设置了正确的 remoteIPHeaders:X-Real-IP,X-Forwarded-For 或自定义的header 预计 gin 1.8 会修复此 bug

func RequestID

func RequestID() gin.HandlerFunc

RequestID 获取并设置 request id

func Session

func Session(key string) gin.HandlerFunc

Session 校验 session

Types

type Error

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

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

type Options struct {
	Secret        string
	IgnoreMethods []string
	IgnoreRoutes  []string
	ErrorFunc     gin.HandlerFunc
	TokenGetter   func(c *gin.Context) string
}

Options stores configurations for a CSRF middleware.

type PromOpts

type PromOpts struct {
	Namespace       string
	IncludeEndpoint string
}

PromOpts represents the Prometheus middleware Options.

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 序列化、反序列化接口

Jump to

Keyboard shortcuts

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