Documentation ¶
Index ¶
- Variables
- func Abort(c *gin.Context, e *Error)
- func Csrf(options *Options, name string) gin.HandlerFunc
- func CsrfHandler() gin.HandlerFunc
- func ErrHandler() gin.HandlerFunc
- func GetRealIP(c *gin.Context) string
- func GetReqID(c *gin.Context) string
- func GetToken(c *gin.Context, name string) string
- func GinZap(logger *zap.Logger) gin.HandlerFunc
- func NewStore(rdb *redis.Client, prefix string, keyPairs []byte) *store
- func OK(c *gin.Context, data interface{})
- func PromHandler(handler http.Handler) gin.HandlerFunc
- func PromMiddleware(promOpts *PromOpts) gin.HandlerFunc
- func RealIP(remoteIPHeaders []string) gin.HandlerFunc
- func RequestID() gin.HandlerFunc
- func Session(key string) gin.HandlerFunc
- type Error
- type GobSerializer
- type JSONSerializer
- type Options
- type PromOpts
- type RedisStore
- func (s *RedisStore) Get(r *http.Request, name string) (*sessions.Session, error)
- func (s *RedisStore) New(r *http.Request, name string) (*sessions.Session, error)
- func (s *RedisStore) Save(_ *http.Request, w http.ResponseWriter, session *sessions.Session) error
- func (s *RedisStore) SetKeyPrefix(p string)
- func (s *RedisStore) SetMaxAge(v int)
- func (s *RedisStore) SetMaxLength(l int)
- func (s *RedisStore) SetSerializer(ss SessionSerializer)
- type SessionSerializer
Constants ¶
This section is empty.
Variables ¶
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 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 ¶
NewStore 返回 redis store
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
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 封装接口错误信息
type GobSerializer ¶
type GobSerializer struct{}
GobSerializer gob 序列化、反序列化
type JSONSerializer ¶
type JSONSerializer struct{}
JSONSerializer json 序列化、反序列化
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 ¶
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 ¶
Get returns a session for the given name after adding it to the registry.
See gorilla/sessions FilesystemStore.Get().
func (*RedisStore) New ¶
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