Documentation ¶
Overview ¶
Package middleware defines multiple gin middlewares
Index ¶
- Constants
- Variables
- func Context() gin.HandlerFunc
- func Cors() gin.HandlerFunc
- func GetDefaultLogFormatterWithRequestID() gin.LogFormatter
- func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig
- func GetRequestIDFromContext(c *gin.Context) string
- func GetRequestIDFromHeaders(c *gin.Context) string
- func Limit(maxEventsPerSec float64, maxBurstSize int) gin.HandlerFunc
- func Logger() gin.HandlerFunc
- func LoggerWithConfig(conf gin.LoggerConfig) gin.HandlerFunc
- func LoggerWithFormatter(f gin.LogFormatter) gin.HandlerFunc
- func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc
- func NoCache(c *gin.Context)
- func Options(c *gin.Context)
- func RequestID() gin.HandlerFunc
- func Secure(c *gin.Context)
- func Validation() gin.HandlerFunc
- type AuthOperator
- type AuthStrategy
Constants ¶
const UsernameKey = "username"
UsernameKey defines the key in gin context which represents the owner of the secret.
const (
// XRequestIDKey defines X-Request-ID key string.
XRequestIDKey = "X-Request-ID"
)
Variables ¶
var ErrorLimitExceeded = errors.New("Limit exceeded")
ErrorLimitExceeded defines Limit exceeded error.
var Middlewares = defaultMiddlewares()
Middlewares store registered middlewares.
Functions ¶
func Context ¶
func Context() gin.HandlerFunc
Context is a middleware that injects common prefix fields to gin.Context. Context 中间件,用来在 gin.Context 中设置 requestID和 username键 在打印日志时,将 gin.Context 类型的变量传递给 log.L() 函数,log.L() 函数会在日志输出中输出 requestID和 username域
func GetDefaultLogFormatterWithRequestID ¶
func GetDefaultLogFormatterWithRequestID() gin.LogFormatter
GetDefaultLogFormatterWithRequestID returns gin.LogFormatter with 'RequestID'.
func GetLoggerConfig ¶
func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig
GetLoggerConfig return gin.LoggerConfig which will write the logs to specified io.Writer with given gin.LogFormatter. By default gin.DefaultWriter = os.Stdout reference: https://github.com/gin-gonic/gin#custom-log-format
func GetRequestIDFromContext ¶
GetRequestIDFromContext returns 'RequestID' from the given context if present.
func GetRequestIDFromHeaders ¶
GetRequestIDFromHeaders returns 'RequestID' from the headers if present.
func Limit ¶
func Limit(maxEventsPerSec float64, maxBurstSize int) gin.HandlerFunc
Limit drops (HTTP status 429) the request if the limit is reached.
func Logger ¶
func Logger() gin.HandlerFunc
Logger instances a Logger middleware that will write the logs to gin.DefaultWriter. By default gin.DefaultWriter = os.Stdout.
func LoggerWithConfig ¶
func LoggerWithConfig(conf gin.LoggerConfig) gin.HandlerFunc
LoggerWithConfig instance a Logger middleware with config.
func LoggerWithFormatter ¶
func LoggerWithFormatter(f gin.LogFormatter) gin.HandlerFunc
LoggerWithFormatter instance a Logger middleware with the specified log format function.
func LoggerWithWriter ¶
func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc
LoggerWithWriter instance a Logger middleware with the specified writer buffer. Example: os.Stdout, a file opened in write mode, a socket...
func NoCache ¶
NoCache is a middleware function that appends headers to prevent the client from caching the HTTP response. 禁止客户端缓存HTTP请求的返回结果
func Options ¶
Options is a middleware function that appends headers for options requests and aborts then exits the middleware chain and ends the request.
func RequestID ¶
func RequestID() gin.HandlerFunc
RequestID is a middleware that injects a 'X-Request-ID' into the context and request/response header of each request. RequestID 中间件,主要用来在 HTTP 请求头和返回头中设置 X-Request-ID Header。 如果 HTTP 请求头中没有 X-Request-ID HTTP 头,则创建 64 位的 UUID,如果有就复用 UUID 是调用 github.com/satori/go.uuid包提供的 NewV4().String()方法来生成的
func Secure ¶
Secure is a middleware function that appends security and resource access headers. 添加一些安全和资源访问相关的 HTTP 头
func Validation ¶
func Validation() gin.HandlerFunc
Validation make sure users have the right resource permission and operation.
Types ¶
type AuthOperator ¶
type AuthOperator struct {
// contains filtered or unexported fields
}
AuthOperator used to switch between different authentication strategy.
func (*AuthOperator) AuthFunc ¶
func (operator *AuthOperator) AuthFunc() gin.HandlerFunc
AuthFunc execute resource authentication.
func (*AuthOperator) SetStrategy ¶
func (operator *AuthOperator) SetStrategy(strategy AuthStrategy)
SetStrategy used to set to another authentication strategy.
type AuthStrategy ¶
type AuthStrategy interface {
AuthFunc() gin.HandlerFunc
}
AuthStrategy defines the set of methods used to do resource authentication.